Schedule copy snapshot preparation by time and byte budgets
This commit is contained in:
@@ -4,22 +4,25 @@ import type { DuplicatePlan } from '../canvas-core/duplicate.ts';
|
||||
import { clone, equal, fault, validJson } from '../canvas-core/values.ts';
|
||||
import type { ImportJob, ImportLine, ImportUnit } from './imports.ts';
|
||||
|
||||
import {createCopyBudget} from './copy-budget.ts';
|
||||
import type {ImportScheduling} from './import-budget.ts';
|
||||
|
||||
type ExternalElement = {id: Id; incarnation: number; ownerCanvasId: Id};
|
||||
type ExternalCanvas = {id: Id; parent: CanvasRecord['parent']; entryIncarnation?: number};
|
||||
type CopyLine = {key: string; change: Exclude<Change,{kind:'field'}>; dependencies: string[]; externalElements: ExternalElement[]; externalCanvases: ExternalCanvas[]};
|
||||
|
||||
/** Internal source: already mapped records, with explicit guards for every retained external reference. */
|
||||
export async function encodeDuplicateSource(document: Document, registry: Registry, plan: DuplicatePlan, signal: AbortSignal): Promise<Blob> {
|
||||
export async function encodeDuplicateSource(document: Document, registry: Registry, plan: DuplicatePlan, signal: AbortSignal, scheduling:ImportScheduling={}): Promise<Blob> {
|
||||
const keys = new Map<Id,string>(),parts: BlobPart[] = [];
|
||||
for (const [id,mapped] of Object.entries(plan.elementIds)) keys.set(mapped,`element:${id}`);
|
||||
for (const [id,mapped] of Object.entries(plan.canvasIds)) keys.set(mapped,`canvas:${id}`);
|
||||
let bytes=0,count=0;
|
||||
const budget=createCopyBudget(signal,scheduling);let bytes=0;
|
||||
for (const change of plan.changes) {
|
||||
if(signal.aborted)throw fault('copy_aborted');
|
||||
if(change.kind==='field'||!change.after||change.before!==null)throw fault('invalid_duplicate_plan');
|
||||
const line=duplicateImportLine(document,registry,change,keys);
|
||||
const text=JSON.stringify(line)+'\n',size=new TextEncoder().encode(text).byteLength;if(size>256*1024)throw fault('record_too_large');bytes+=size;if(bytes>64*1024*1024)throw fault('invalid_import_source');parts.push(text);
|
||||
if(++count%32===0)await new Promise<void>(resolve=>setTimeout(resolve,0));
|
||||
await budget.step(Math.max(1,Math.ceil(size/1024)));
|
||||
}
|
||||
if(signal.aborted)throw fault('copy_aborted');return new Blob(parts,{type:'application/x-ndjson'});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user