mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-10-27 07:30:34 +08:00
24 lines
628 B
TypeScript
24 lines
628 B
TypeScript
// Runs edits one at a time in submission order and refreshes once the queue
|
|
// drains, so quick successive toggles neither race nor refresh per edit.
|
|
export function serialEdits(
|
|
errorMessage: string,
|
|
refresh: () => Promise<void>,
|
|
): (run: () => Promise<unknown>) => void {
|
|
let queue: Promise<void> = Promise.resolve();
|
|
let pending = 0;
|
|
|
|
return run => {
|
|
pending += 1;
|
|
queue = queue.then(async () => {
|
|
try {
|
|
await run();
|
|
} catch (error) {
|
|
console.error(errorMessage, error);
|
|
} finally {
|
|
pending -= 1;
|
|
if (pending === 0) await refresh();
|
|
}
|
|
});
|
|
};
|
|
}
|