-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscriptWorkerSet.ts
37 lines (36 loc) · 1.42 KB
/
scriptWorkerSet.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
import { TextlintWorker } from "./textlint";
import { keyOfScript, Script } from "./database";
import { logger } from "../utils/logger";
// worker: Set<url>
const _workerRunningUrlMap = new Map<TextlintWorker, Set<string>>();
const _scriptRunningWorkerMap = new Map<string, TextlintWorker>();
export const scriptWorkerSet = {
get(script: Script) {
return _scriptRunningWorkerMap.get(keyOfScript(script));
},
has(script: Script) {
return _scriptRunningWorkerMap.has(keyOfScript(script));
},
add({ script, worker, url }: { script: Script; worker: TextlintWorker; url: string }) {
const set = _workerRunningUrlMap.get(worker) ?? new Set<string>();
set.add(url);
_workerRunningUrlMap.set(worker, set);
return _scriptRunningWorkerMap.set(keyOfScript(script), worker);
},
delete({ script, url }: { script: Script; url: string }) {
for (const [worker, urlSet] of _workerRunningUrlMap.entries()) {
if (urlSet.has(url)) {
urlSet.delete(url);
}
if (urlSet.size === 0) {
worker.dispose();
_workerRunningUrlMap.delete(worker);
}
}
return _scriptRunningWorkerMap.delete(keyOfScript(script));
},
dump() {
logger.log("Running Scripts: ", _scriptRunningWorkerMap.keys());
logger.log("Running Workers x URLs: ", _workerRunningUrlMap.entries());
}
};