Schedule copy snapshot preparation by time and byte budgets

This commit is contained in:
2026-11-10 18:11:25 +08:00
parent afccb8c95c
commit 2452fd6ae9
6 changed files with 91 additions and 27 deletions
+13
View File
@@ -0,0 +1,13 @@
import {fault} from '../canvas-core/values.ts';
import {createImportBudget,type ImportScheduling} from './import-budget.ts';
/** Share import scheduling while retaining the copy cancellation/error contract. */
export function createCopyBudget(signal:AbortSignal,options:ImportScheduling={}){
if(signal.aborted)throw fault('copy_aborted');
if(options.frameBudgetMs!==undefined&&(!Number.isFinite(options.frameBudgetMs)||options.frameBudgetMs<=0))throw fault('invalid_copy_budget');
const budget=createImportBudget(signal,options);
const fail=(error:unknown):never=>{if(signal.aborted)throw fault('copy_aborted');throw error;};
return Object.freeze({step(units=1):Promise<void>|undefined{
if(signal.aborted)throw fault('copy_aborted');
try{return budget.step(units)?.catch(fail);}catch(error){return fail(error);}
}});
}