import { PluginError } from './errors'; import type { OperationOptions } from './types'; /** * Small promise-tail queue: operations run strictly one at a time, in * submission order, and failures never poison later operations. The shared * serializer for per-document mutation/dispatch pipelines. * * Direction law for nested queues: an operation running on queue A may * enqueue into queue B and await it, but never the reverse on the same pair — * a B-operation enqueueing back into A self-deadlocks behind its caller. */ export interface SerialQueue { /** * Run `operation` once everything queued before it has finished. With * `{ signal }`, an operation whose signal fired while it waited never * starts: it rejects `operation-cancelled`. Once it has started, cancelling * is up to the operation (`ctx.cancellable`). */ (operation: () => Promise, options?: OperationOptions): Promise; /** Resolves once every operation queued so far has finished, however it ended. */ idle(): Promise; } /** `capability` names the plugin in the error a skipped operation rejects with. */ export function createSerialQueue(capability: string): SerialQueue { let tail: Promise = Promise.resolve(); const queue = (operation: () => Promise, options?: OperationOptions): Promise => { const start = (): Promise => options?.signal?.aborted ? Promise.reject( new PluginError('operation-cancelled', capability, 'cancelled before it started', { cause: options.signal.reason, }), ) : operation(); const result = tail.then(start, start); tail = result.then( () => undefined, () => undefined, ); return result; }; return Object.assign(queue, { idle: () => tail }); }