MCPcopy Create free account
hub / github.com/decaporg/decap-cms / asyncLock

Function asyncLock

packages/decap-cms-lib-util/src/asyncLock.ts:5–43  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3export type AsyncLock = { release: () => void; acquire: () => Promise<boolean> };
4
5export function asyncLock(): AsyncLock {
6 let lock = semaphore(1);
7
8 function acquire(timeout = 15000) {
9 const promise = new Promise<boolean>(resolve => {
10 // this makes sure a caller doesn't gets stuck forever awaiting on the lock
11 const timeoutId = setTimeout(() => {
12 // we reset the lock in that case to allow future consumers to use it without being blocked
13 lock = semaphore(1);
14 resolve(false);
15 }, timeout);
16
17 lock.take(() => {
18 clearTimeout(timeoutId);
19 resolve(true);
20 });
21 });
22
23 return promise;
24 }
25
26 function release() {
27 try {
28 // suppress too many calls to leave error
29 lock.leave();
30 } catch (e) {
31 // calling 'leave' too many times might not be good behavior
32 // but there is no reason to completely fail on it
33 if (e.message !== 'leave called too many times.') {
34 throw e;
35 } else {
36 console.warn('leave called too many times.');
37 lock = semaphore(1);
38 }
39 }
40 }
41
42 return { acquire, release };
43}

Callers 9

constructorMethod · 0.90
requestWithBackoffFunction · 0.90
asyncLock.spec.jsFile · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected