Files
backnotprop__plannotator/packages/shared/single-flight.ts
Michael Ramos 56df64c751 Add modern GitButler review support (#1067)
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>
2026-07-17 07:37:50 -07:00

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;
}
}