-
Notifications
You must be signed in to change notification settings - Fork 0
/
self.js
27 lines (22 loc) · 972 Bytes
/
self.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
module.exports = globalThis.self = globalThis;
const { parentPort } = require("node:worker_threads");
if (parentPort !== null) {
const target = new EventTarget();
self.onmessage = null;
self.onmessageerror = null;
parentPort.on("message", (data) => {
const event = new MessageEvent("message", { data });
self.onmessage?.(event);
target.dispatchEvent(event);
});
parentPort.on("messageerror", (data) => {
const event = new MessageEvent("message", { data });
self.onmessageerror?.(event);
target.dispatchEvent(event);
});
self.addEventListener = (type, listener, options) => target.addEventListener(type, listener, options);
self.removeEventListener = (type, listener, options) => target.removeEventListener(type, listener, options);
self.dispatchEvent = (event) => target.dispatchEvent(event);
self.close = () => parentPort.close();
self.postMessage = (message, transfer) => parentPort.postMessage(message, transfer);
}