mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-09-30 15:03:12 +00:00
21 lines
784 B
TypeScript
21 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;
|
||
|
|
};
|
||
|
|
}
|