MCPcopy
hub / github.com/callstack/agent-device / withKeyedLock

Function withKeyedLock

src/utils/keyed-lock.ts:10–29  ·  view source on GitHub ↗
(
  locks: Map<string, Promise<unknown>>,
  key: string,
  task: () => Promise<T>,
)

Source from the content-addressed store, hash-verified

8const keyedLockStorage = new AsyncLocalStorage<LockFrame[]>();
9
10export async function withKeyedLock<T>(
11 locks: Map<string, Promise<unknown>>,
12 key: string,
13 task: () => Promise<T>,
14): Promise<T> {
15 const activeLocks = keyedLockStorage.getStore() ?? [];
16 if (activeLocks.some((entry) => entry.locks === locks && entry.key === key)) {
17 return await task();
18 }
19 const previous = locks.get(key) ?? Promise.resolve();
20 const current = previous
21 .catch(() => {})
22 .then(() => keyedLockStorage.run([...activeLocks, { locks, key }], task));
23 locks.set(key, current);
24 return current.finally(() => {
25 if (locks.get(key) === current) {
26 locks.delete(key);
27 }
28 });
29}

Callers 5

keyed-lock.test.tsFile · 0.90
handleOpenCommandFunction · 0.90
ensureXctestrunArtifactFunction · 0.90
withRunnerSessionLockFunction · 0.90

Calls 3

getMethod · 0.80
setMethod · 0.80
deleteMethod · 0.80

Tested by

no test coverage detected