/** * Serializes async work per work-order id so concurrent patches (e.g. a completion-doc * status save firing alongside a completed-date save) never race two overlapping * get-then-update round trips against the same record. Each call for a given id waits * for the previous one to settle before starting its own fetch-and-update cycle. */ export function createWorkOrderPatchQueue() { const chains = new Map>(); return function enqueue(id: string | number, run: () => Promise): Promise { const key = String(id); const previous = chains.get(key) ?? Promise.resolve(); const settled = previous.catch(() => undefined).then(run); chains.set( key, settled.catch(() => undefined), ); return settled; }; }