/
Mutex.ts
75 lines (69 loc) · 1.77 KB
/
Mutex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
enum MutexLockStatus {
LOCKED,
UNLOCKED
}
type Callback = (...args: any[]) => any;
/**
* An async mutex lock.
*
* @export
* @class Mutex
*/
export class Mutex {
/**
* Lock for a specific key. If the lock has been acquired by another customer, then
* will wait until getting the lock.
*
* @static
* @param {string} key lock key
* @returns {Promise<void>}
* @memberof Mutex
*/
public static async lock(key: string): Promise<void> {
return new Promise<void>((resolve) => {
if (this.keys[key] === undefined || this.keys[key] === MutexLockStatus.UNLOCKED) {
this.keys[key] = MutexLockStatus.LOCKED;
resolve();
} else {
this.onUnlockEvent(key, () => {
this.keys[key] = MutexLockStatus.LOCKED;
resolve();
});
}
});
}
/**
* Unlock a key.
*
* @static
* @param {string} key
* @returns {Promise<void>}
* @memberof Mutex
*/
public static async unlock(key: string): Promise<void> {
return new Promise<void>((resolve) => {
if (this.keys[key] === MutexLockStatus.LOCKED) {
this.emitUnlockEvent(key);
}
delete this.keys[key];
resolve();
});
}
private static keys: { [key: string]: MutexLockStatus } = {};
private static listeners: { [key: string]: Callback[] } = {};
private static onUnlockEvent(key: string, handler: Callback) {
if (this.listeners[key] === undefined) {
this.listeners[key] = [handler];
} else {
this.listeners[key].push(handler);
}
}
private static emitUnlockEvent(key: string) {
if (this.listeners[key] !== undefined && this.listeners[key].length > 0) {
const handler = this.listeners[key].shift();
setImmediate(() => {
handler!.call(this);
});
}
}
}