-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathshared-workers.js
More file actions
128 lines (102 loc) · 2.85 KB
/
shared-workers.js
File metadata and controls
128 lines (102 loc) · 2.85 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import events from 'node:events';
import {pathToFileURL} from 'node:url';
import {Worker} from 'node:worker_threads';
import serializeError from '../serialize-error.js';
const LOADER = new URL('shared-worker-loader.js', import.meta.url);
const launchedWorkers = new Map();
const waitForAvailable = async worker => {
for await (const [message] of events.on(worker, 'message')) {
if (message.type === 'available') {
return;
}
}
};
function launchWorker(filename, initialData) {
if (launchedWorkers.has(filename)) {
return launchedWorkers.get(filename);
}
const worker = new Worker(LOADER, {
// Ensure the worker crashes for unhandled rejections, rather than allowing undefined behavior.
execArgv: ['--unhandled-rejections=strict'],
workerData: {
filename,
initialData,
},
});
worker.setMaxListeners(0);
const launched = {
statePromises: {
available: waitForAvailable(worker),
error: events.once(worker, 'error').then(([error]) => error),
},
exited: false,
worker,
};
launchedWorkers.set(filename, launched);
worker.once('exit', () => {
launched.exited = true;
});
return launched;
}
export async function observeWorkerProcess(fork, runStatus) {
let signalDone;
const done = new Promise(resolve => {
signalDone = () => {
resolve();
};
});
const activeInstances = new Set();
const removeInstance = instance => {
instance.worker.unref();
activeInstances.delete(instance);
if (activeInstances.size === 0) {
signalDone();
}
};
const removeAllInstances = () => {
if (activeInstances.size === 0) {
signalDone();
return;
}
for (const instance of activeInstances) {
removeInstance(instance);
}
};
fork.promise.finally(() => {
removeAllInstances();
});
fork.onConnectSharedWorker(async ({filename, initialData, port, signalError}) => {
const launched = launchWorker(filename, initialData);
activeInstances.add(launched);
const handleWorkerMessage = async message => {
if (message.type === 'deregistered-test-worker' && message.id === fork.threadId) {
launched.worker.off('message', handleWorkerMessage);
removeInstance(launched);
}
};
launched.statePromises.error.then(error => {
launched.worker.off('message', handleWorkerMessage);
removeAllInstances();
runStatus.emitStateChange({type: 'shared-worker-error', err: serializeError('Shared worker error', true, error)});
signalError();
});
try {
await launched.statePromises.available;
port.postMessage({type: 'ready'});
launched.worker.postMessage({
type: 'register-test-worker',
id: fork.threadId,
file: pathToFileURL(fork.file).toString(),
port,
}, [port]);
fork.promise.finally(() => {
launched.worker.postMessage({
type: 'deregister-test-worker',
id: fork.threadId,
});
});
launched.worker.on('message', handleWorkerMessage);
} catch {}
});
return done;
}