-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathrpc-worker-loader.js
More file actions
35 lines (32 loc) · 979 Bytes
/
rpc-worker-loader.js
File metadata and controls
35 lines (32 loc) · 979 Bytes
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
/* global __webpack_exports__ */
function workerSetup() {
addEventListener('message', (e) => {
let { type, method, id, params } = e.data, f, p;
if (type==='RPC' && method) {
if ((f = __webpack_exports__[method])) {
p = Promise.resolve().then( () => f.apply(__webpack_exports__, params) );
}
else {
p = Promise.reject('No such method');
}
p.then(result => {
postMessage({ type: 'RPC', id, result });
})
.catch(e => {
let error = { message: e };
if (e.stack) {
error.message = e.message;
error.stack = e.stack;
error.name = e.name;
}
postMessage({ type: 'RPC', id, error });
});
}
});
postMessage({ type: 'RPC', method: 'ready' });
}
const workerScript = '\n' + Function.prototype.toString.call(workerSetup).replace(/(^.*\{|\}.*$|\n\s*)/g, '');
export default function rpcWorkerLoader(content, sourceMap) {
const callback = this.async();
callback(null, content + workerScript, sourceMap);
}