-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (43 loc) · 1.25 KB
/
index.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReentrantLock = void 0;
class ReentrantLock {
constructor() {
this.chainNoop = () => void (0);
this.chainNext = () => {
if (this.current === this.last) {
this.current = this.last = undefined;
}
else {
this.current.next();
}
};
}
acquire() {
if (!this.last) {
let noop = this.current = this.last = this.chainNoop;
return Promise.resolve(() => {
if (noop) {
noop = undefined;
this.chainNext();
}
});
}
else {
return new Promise((resolve) => {
let lockChainFn = () => {
if (lockChainFn) {
this.current = lockChainFn;
lockChainFn = undefined;
resolve(this.chainNext);
}
};
this.last = this.last.next = lockChainFn;
});
}
}
lock(op) {
return this.acquire().then(unlock => op().finally(unlock));
}
}
exports.ReentrantLock = ReentrantLock;