-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtwoslasher.ts
31 lines (26 loc) · 913 Bytes
/
twoslasher.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
import { fork } from 'child_process';
import {
TwoSlashReturn,
twoslasher as _twoslasher,
} from '@typescript/twoslash';
const childProcess = fork(require.resolve('./twoslasherWorker'));
const resolveMap = new Map<
number,
[(value: TwoSlashReturn) => void, (value: any) => void]
>();
childProcess.on('message', message => {
if (!(message instanceof Array)) return;
const [id, error, value] = message as [number, any, TwoSlashReturn | null];
const [resolve, reject] = resolveMap.get(id) ?? [undefined, undefined];
if (!resolve || !reject) throw new Error(`Missing id ${id}`);
if (error || !value) reject(error);
else resolve(value);
resolveMap.delete(id);
});
let idN = 0;
export const twoslasher = (...args: Parameters<typeof _twoslasher>) =>
new Promise<TwoSlashReturn>((resolve, reject) => {
const id = idN++;
resolveMap.set(id, [resolve, reject]);
childProcess.send([id, ...args]);
});