Collect unreferenced local resource bytes atomically with recovery cuts
This commit is contained in:
@@ -21,6 +21,8 @@ export type EditorOptions<Runtime = undefined> = {
|
||||
reduceRuntime?: (previous: Runtime, next: CoreSnapshot, commit: Commit) => Runtime;
|
||||
/** Engine may attach transport semantics without changing identity, order or document changes. */
|
||||
prepareIntent?: (intent: OperationIntent, runtime: Runtime) => OperationIntent;
|
||||
/** Pure coordinator hook for all publication paths; may replace runtime only or reject. */
|
||||
prepareRuntime?: (previous: Snapshot<Runtime>, next: Snapshot<Runtime>) => Runtime;
|
||||
nextOperationSequence?: (runtime: Runtime, fallback: number) => number;
|
||||
};
|
||||
export type Transaction = { dispatch(command: Command): Availability; getDocument(): Document };
|
||||
@@ -119,6 +121,12 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
if (disposed) throw fault('editor_disposed');
|
||||
if (entered) throw fault('reentrant_dispatch');
|
||||
}
|
||||
function prepareState(next: Snapshot<Runtime>): Snapshot<Runtime> {
|
||||
if(!options.prepareRuntime)return next;
|
||||
const runtime=options.prepareRuntime(state,next);
|
||||
if(isThenable(runtime))throw fault('async_transaction');
|
||||
return runtime===next.runtime?next:Object.freeze({...next,runtime});
|
||||
}
|
||||
function emit(commit: Commit) {
|
||||
for (const listener of [...listeners]) try { listener(commit, state); } catch (error) { try { options.onListenerError?.(error); } catch { /* Observers cannot roll back a committed state. */ } }
|
||||
}
|
||||
@@ -181,7 +189,7 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
const commit = freeze({ revision: next.revision, origin, changes, operationIds });
|
||||
const runtime = options.reduceRuntime ? options.reduceRuntime(state.runtime, next, commit) : state.runtime;
|
||||
if (isThenable(runtime)) throw fault('async_transaction');
|
||||
const nextState=Object.freeze({ ...next, rootRevision: state.rootRevision + 1, runtime }),nextQueries=advanceDocumentQueries(queries,document,options.registry,next.revision,changes);
|
||||
const nextState=prepareState(Object.freeze({ ...next, rootRevision: state.rootRevision + 1, runtime })),nextQueries=advanceDocumentQueries(queries,document,options.registry,next.revision,changes);
|
||||
state=nextState;queries=nextQueries;queriesByDocument.set(document,queries);
|
||||
if (outbound) sequence = nextSequence;
|
||||
emit(commit);
|
||||
@@ -270,7 +278,7 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
const runtime = reduce(state.runtime);
|
||||
if (isThenable(runtime)) throw fault('async_transaction');
|
||||
if (runtime !== state.runtime) {
|
||||
state = Object.freeze({ ...state, rootRevision: state.rootRevision + 1, runtime });
|
||||
state = prepareState(Object.freeze({ ...state, rootRevision: state.rootRevision + 1, runtime }));
|
||||
emit(freeze({ revision: state.revision, origin: 'ack', changes: [], operationIds: [] }));
|
||||
}
|
||||
return Object.freeze({ status: 'applied', revision: state.revision, operationIds: Object.freeze([]) });
|
||||
@@ -295,7 +303,7 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
for(const [id,entry]of history.entries)if(entry.id!==id||entry.operationIds.some(operationId=>!operations.has(operationId)))throw fault('invalid_history');
|
||||
}
|
||||
const changed = !documentsEqual(next, state.document);
|
||||
const nextState=Object.freeze({ ...state, ...(replacement?{history:replacement.history,operations:replacement.operations}:{}), document: changed ? next : state.document, runtime, revision: state.revision + Number(changed), rootRevision: state.rootRevision + 1 }),nextQueries=changed?advanceDocumentQueries(queries,next,options.registry,state.revision+1):queries;
|
||||
const nextState=prepareState(Object.freeze({ ...state, ...(replacement?{history:replacement.history,operations:replacement.operations}:{}), document: changed ? next : state.document, runtime, revision: state.revision + Number(changed), rootRevision: state.rootRevision + 1 })),nextQueries=changed?advanceDocumentQueries(queries,next,options.registry,state.revision+1):queries;
|
||||
state=nextState;queries=nextQueries;if(changed)queriesByDocument.set(next,queries);
|
||||
sequence=nextSequence;
|
||||
emit(freeze({ revision: state.revision, origin: 'ack', changes: [], operationIds: [] }));
|
||||
|
||||
Reference in New Issue
Block a user