MCPcopy Create free account
hub / github.com/TanStack/ai / withLock

Method withLock

packages/ai-sandbox/src/store.ts:69–82  ·  view source on GitHub ↗
(key: string, fn: () => Promise<T>)

Source from the content-addressed store, hash-verified

67 private readonly chains = new Map<string, Promise<unknown>>()
68
69 withLock<T>(key: string, fn: () => Promise<T>): Promise<T> {
70 const prior = this.chains.get(key) ?? Promise.resolve()
71 // Chain after the prior holder regardless of how it settled.
72 const run = prior.then(fn, fn)
73 // Keep the chain alive but swallow rejections so one failure doesn't poison the lock.
74 this.chains.set(
75 key,
76 run.then(
77 () => undefined,
78 () => undefined,
79 ),
80 )
81 return run
82 }
83}

Callers 3

ensureFunction · 0.80
slowFunction · 0.80
store.test.tsFile · 0.80

Calls 3

resolveMethod · 0.80
getMethod · 0.65
setMethod · 0.65

Tested by 1

slowFunction · 0.64