-
Notifications
You must be signed in to change notification settings - Fork 30.2k
/
Copy pathpolyfillNestedWorker.ts
133 lines (107 loc) · 3.79 KB
/
polyfillNestedWorker.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
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
129
130
131
132
133
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { NewWorkerMessage, TerminateWorkerMessage } from '../common/polyfillNestedWorker.protocol.js';
declare function postMessage(data: any, transferables?: Transferable[]): void;
declare type MessageEventHandler = ((ev: MessageEvent<any>) => any) | null;
const _bootstrapFnSource = (function _bootstrapFn(workerUrl: string) {
const listener: EventListener = (event: Event): void => {
// uninstall handler
globalThis.removeEventListener('message', listener);
// get data
const port = <MessagePort>(<MessageEvent>event).data;
// postMessage
// onmessage
Object.defineProperties(globalThis, {
'postMessage': {
value(data: any, transferOrOptions?: any) {
port.postMessage(data, transferOrOptions);
}
},
'onmessage': {
get() {
return port.onmessage;
},
set(value: MessageEventHandler) {
port.onmessage = value;
}
}
// todo onerror
});
port.addEventListener('message', msg => {
globalThis.dispatchEvent(new MessageEvent('message', { data: msg.data, ports: msg.ports ? [...msg.ports] : undefined }));
});
port.start();
// fake recursively nested worker
globalThis.Worker = <any>class { constructor() { throw new TypeError('Nested workers from within nested worker are NOT supported.'); } };
// load module
importScripts(workerUrl);
};
globalThis.addEventListener('message', listener);
}).toString();
export class NestedWorker extends EventTarget implements Worker {
onmessage: ((this: Worker, ev: MessageEvent<any>) => any) | null = null;
onmessageerror: ((this: Worker, ev: MessageEvent<any>) => any) | null = null;
onerror: ((this: AbstractWorker, ev: ErrorEvent) => any) | null = null;
readonly terminate: () => void;
readonly postMessage: (message: any, options?: any) => void;
constructor(nativePostMessage: typeof postMessage, stringOrUrl: string | URL, options?: WorkerOptions) {
super();
// create bootstrap script
const bootstrap = `((${_bootstrapFnSource})('${stringOrUrl}'))`;
const blob = new Blob([bootstrap], { type: 'application/javascript' });
const blobUrl = URL.createObjectURL(blob);
const channel = new MessageChannel();
const id = blobUrl; // works because blob url is unique, needs ID pool otherwise
const msg: NewWorkerMessage = {
type: '_newWorker',
id,
port: channel.port2,
url: blobUrl,
options,
};
nativePostMessage(msg, [channel.port2]);
// worker-impl: functions
this.postMessage = channel.port1.postMessage.bind(channel.port1);
this.terminate = () => {
const msg: TerminateWorkerMessage = {
type: '_terminateWorker',
id
};
nativePostMessage(msg);
URL.revokeObjectURL(blobUrl);
channel.port1.close();
channel.port2.close();
};
// worker-impl: events
Object.defineProperties(this, {
'onmessage': {
get() {
return channel.port1.onmessage;
},
set(value: MessageEventHandler) {
channel.port1.onmessage = value;
}
},
'onmessageerror': {
get() {
return channel.port1.onmessageerror;
},
set(value: MessageEventHandler) {
channel.port1.onmessageerror = value;
}
},
// todo onerror
});
channel.port1.addEventListener('messageerror', evt => {
const msgEvent = new MessageEvent('messageerror', { data: evt.data });
this.dispatchEvent(msgEvent);
});
channel.port1.addEventListener('message', evt => {
const msgEvent = new MessageEvent('message', { data: evt.data });
this.dispatchEvent(msgEvent);
});
channel.port1.start();
}
}