shoc-frontend-new/src/domain/work-orders/utils/work-order-patch-queue.ts
Arthur Bassi b7056f569e fix(work-orders): persist completion doc fields without race
Map docStatus through the real update payload, serialize concurrent
table patches per work order, and split CompDoc UI for governance.
2026-07-28 12:49:51 -03:00

20 lines
784 B
TypeScript

/**
* 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<string, Promise<unknown>>();
return function enqueue<T>(id: string | number, run: () => Promise<T>): Promise<T> {
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;
};
}