MCPcopy Index your code
hub / github.com/simstudioai/sim / coalesceLocally

Function coalesceLocally

apps/sim/lib/concurrency/singleflight.ts:30–67  ·  view source on GitHub ↗
(
  key: string,
  fn: () => Promise<T>,
  settleTimeoutMs: number = DEFAULT_SETTLE_TIMEOUT_MS
)

Source from the content-addressed store, hash-verified

28 * caller will join it.
29 */
30export function coalesceLocally<T>(
31 key: string,
32 fn: () => Promise<T>,
33 settleTimeoutMs: number = DEFAULT_SETTLE_TIMEOUT_MS
34): Promise<T> {
35 const existing = inflight.get(key) as Promise<T> | undefined
36 if (existing) return existing
37
38 let timer: ReturnType<typeof setTimeout> | undefined
39 const evict = () => {
40 if (inflight.get(key) === guarded) inflight.delete(key)
41 }
42
43 const guarded: Promise<T> = Promise.race([
44 (async () => {
45 try {
46 // Defer fn() to a microtask so a synchronous throw surfaces as a
47 // rejection after `guarded` and the timer are initialized. Calling it
48 // inline would run the finally below during construction, touching
49 // `guarded` in its temporal dead zone and masking fn's real error.
50 return await Promise.resolve().then(fn)
51 } finally {
52 clearTimeout(timer)
53 evict()
54 }
55 })(),
56 new Promise<never>((_, reject) => {
57 timer = setTimeout(() => {
58 evict()
59 reject(new CoalesceSettleTimeoutError(key, settleTimeoutMs))
60 }, settleTimeoutMs)
61 timer.unref?.()
62 }),
63 ])
64
65 inflight.set(key, guarded)
66 return guarded
67}
68
69export function __resetCoalesceLocallyForTests(): void {
70 inflight.clear()

Callers 2

performCoalescedRefreshFunction · 0.90

Calls 4

evictFunction · 0.85
getMethod · 0.65
resolveMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected