-
Notifications
You must be signed in to change notification settings - Fork 1
/
lock.js
45 lines (35 loc) · 983 Bytes
/
lock.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
import con from "/console.js";
import {remote} from "/message.js";
const LIMIT = 10000;
const sorts = new Map();
export async function notify() {
try {
await remote.sortInProgress(sorts.size > 0);
} catch (_e) {
// Ignore; popup frame might not exist
}
}
// Wait for a previous sort to complete
export async function wait(id) {
let promise = sorts.get(id);
if (promise) {
con.log("Waiting on:", id, promise);
await promise;
promise = sorts.get(id);
}
return Boolean(promise);
}
export async function run(id, asyncFunc) {
if (sorts.has(id)) throw new Error(`Already sorting ${id}`);
// Safety valve
if (sorts.size >= LIMIT) throw new Error("Too many concurrent sorts");
const promise = asyncFunc();
sorts.set(id, promise);
try {
if (sorts.size === 1) notify();
await promise;
} finally {
sorts.delete(id);
if (sorts.size === 0) notify();
}
}