-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
61 lines (59 loc) · 1.38 KB
/
worker.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
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
let __func = () => {};
onmessage = function (event) {
const { type, id, data } = event.data;
switch (type) {
case "set":
__func = new Function(`return ${data};`)();
break;
case "call":
Promise.resolve(data)
.then((v) => __func.apply(null, v))
.then(
(resolved) =>
self.postMessage({
type: "resolve",
id,
data: resolved,
}),
(rejected) =>
self.postMessage({
type: "reject",
id,
data: rejected,
}),
);
break;
case "run":
const r = new Function(`return ${data.func};`)();
Promise.resolve(data.params)
.then((v) => r.apply(null, v))
.then(
(resolved) => {
self.postMessage({
type: "resolve",
id,
data: resolved,
});
},
(rejected) =>
self.postMessage({
type: "reject",
id,
data: rejected,
}),
);
break;
case "declare":
self[data.ident] = data.value;
break;
case "define":
self[data.ident] = new Function(`return ${data.func};`)();
break;
default:
self.postMessage({
type: "error",
data: "Unknown message type",
});
break;
}
};