export function runSingleFlight( pendingRef: { current: Promise | null }, task: () => Promise, ): Promise { if (pendingRef.current) { return pendingRef.current; } const taskPromise = task(); const pendingState: { promise: Promise | null } = { promise: null, }; pendingState.promise = taskPromise.finally(() => { if (pendingRef.current === pendingState.promise) { pendingRef.current = null; } }); pendingRef.current = pendingState.promise; return pendingState.promise; }