forked from Carroted/Bimulo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsedition
41 lines (36 loc) · 959 Bytes
/
tsedition
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
// main.js
const obj = { foo: 'bar', baz: 42 };
const keys = Object.keys(obj);
const worker = new Worker('worker.js');
worker.postMessage(keys);
worker.onmessage = function (event) {
if (event.data.type === 'get') {
}
};
//------------------------------------------------
// worker.js
onmessage = function (event) {
const keys = event.data;
const proxy = new Proxy({}, {
get(target, prop) {
return new Promise(resolve => {
postMessage({ type: 'get', key: prop });
onmessage = function (event) {
if (event.data.type === 'get' && event.data.key === prop) {
resolve(event.data.value);
}
};
});
}
});
postMessage({ type: 'ready' });
onmessage = function (event) {
if (event.data.type === 'get' && keys.includes(event.data.key)) {
postMessage({
type: 'response',
key: event.data.key,
value: obj[event.data.key]
});
}
};
};