import { AsyncLocalStorage } from "node:async_hooks"; // One reentrant queue for power, backup, restore and Compose operations. const state = globalThis as unknown as { __craftdeckOperation?: { tail: Promise; context: AsyncLocalStorage }; }; const queue = state.__craftdeckOperation ??= { tail: Promise.resolve(), context: new AsyncLocalStorage(), }; export function withServerOperation(operation: () => Promise): Promise { if (queue.context.getStore()) return operation(); const result = queue.tail.then(() => queue.context.run(true, operation)); queue.tail = result.catch(() => undefined); return result; }