mirror of
https://github.com/backnotprop/plannotator.git
synced 2026-09-14 14:17:26 +08:00
56df64c751
Adds current-architecture GitButler workspace, stack, and branch review support across Bun and Pi while preserving the existing Git, JJ, and P4 paths. Co-authored-by: Dan Susman <56033661+dansusman@users.noreply.github.com>
27 lines
854 B
TypeScript
27 lines
854 B
TypeScript
/**
|
|
* Coalesce concurrent async work for the same key without caching its result.
|
|
* A different key starts independently, and a settled operation is never reused.
|
|
*/
|
|
export class SingleFlight<T> {
|
|
private active: { key: string; promise: Promise<T> } | null = null;
|
|
|
|
/** Run an operation or join the currently running operation for the same key. */
|
|
async run(key: string, operation: () => Promise<T>): Promise<T> {
|
|
const existing = this.active;
|
|
if (existing?.key === key) return existing.promise;
|
|
|
|
const promise = operation();
|
|
this.active = { key, promise };
|
|
try {
|
|
return await promise;
|
|
} finally {
|
|
if (this.active?.promise === promise) this.active = null;
|
|
}
|
|
}
|
|
|
|
/** Stop new callers from joining the current operation. The operation itself continues. */
|
|
clear(): void {
|
|
this.active = null;
|
|
}
|
|
}
|