This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AsyncLock.js
114 lines (98 loc) · 2.66 KB
/
AsyncLock.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { getDeferred, sleep } from './promises.js';
const locks = new WeakMap();
export class AsyncLock extends EventTarget {
constructor(name) {
super();
locks.set(this, { name });
}
get locked() {
return 'lock' in locks.get(this);
}
get mode() {
if (this.locked) {
return locks.get(this).lock.mode;
} else {
return null;
}
}
get name() {
return locks.get(this).name;
}
get supported() {
return 'locks' in navigator && navigator.locks.request instanceof Function;
}
get whenLocked() {
return new Promise(resolve => {
if (this.locked) {
resolve();
} else {
this.addEventListener('locked', () => resolve(), { once: true });
}
});
}
get whenUnlocked() {
return new Promise(resolve => {
if (this.locked) {
this.addEventListener('unlocked', () => resolve(), { once: true });
} else {
resolve();
}
});
}
expireIn(ms, { signal } = {}) {
sleep(ms, { signal }).then(() => {
if (this.locked) {
this.unlock(new DOMException('Lock expired'));
}
});
}
async lock({ mode = 'exclusive', ifAvailable = false, steal = false, signal, expires } = {}) {
if (this.locked) {
throw new DOMException('Already locked');
} else if (! this.supported) {
throw new DOMException('WebLocks not supported');
} else {
const deferred = getDeferred({ signal });
const name = this.name;
navigator.locks.request(name, { mode, ifAvailable, steal, signal }, async lock => {
if (! lock) {
deferred.reject(new DOMException('Error acquiring lock'));
} else {
const { resolve, reject, promise } = getDeferred({ signal });
locks.set(this, { name, resolve, reject, promise, lock });
deferred.resolve(lock);
this.dispatchEvent(new CustomEvent('locked', { detail: lock }));
if (Number.isSafeInteger(expires)) {
this.expireIn(expires, { signal });
}
return await promise.then(detail => {
locks.set(this, { name });
resolve(detail);
this.dispatchEvent(new CustomEvent('unlocked', { detail }));
return detail;
}).catch(error => {
locks.set(this, { name });
reject(error);
this.dispatchEvent(new CustomEvent('error', { detail: error }));
this.dispatchEvent(new CustomEvent('unlocked', { detail: { error }}));
});
}
}).catch(deferred.reject);
return await deferred.promise;
}
}
async unlock(reason) {
if (! this.locked) {
throw new DOMException('Attempting to unlock a lock which is not locked');
} else {
const { resolve, reject, lock, name } = locks.get(this);
locks.set(this, { name });
if (reason instanceof Error) {
reject(reason);
} else {
resolve(reason);
}
return lock;
}
}
}