1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export function wrap(smc, id = null) {
|
14 | const { port1, port2 } = new MessageChannel();
|
15 | hookup(port2, smc, id);
|
16 | return port1;
|
17 | }
|
18 | function hookup(internalPort, smc, id = null) {
|
19 | internalPort.onmessage = (event) => {
|
20 | if (!id)
|
21 | id = generateUID();
|
22 | const msg = event.data;
|
23 | const messageChannels = Array.from(findMessageChannels(event.data));
|
24 | for (const messageChannel of messageChannels) {
|
25 | const id = generateUID();
|
26 | const channel = replaceProperty(msg, messageChannel, id);
|
27 | hookup(channel, smc, id);
|
28 | }
|
29 | const payload = JSON.stringify({ id, msg, messageChannels });
|
30 | smc.send(payload);
|
31 | };
|
32 | smc.addEventListener("message", (event) => {
|
33 | let data = {};
|
34 | try {
|
35 | data = JSON.parse(event.data);
|
36 | }
|
37 | catch (e) {
|
38 | return;
|
39 | }
|
40 | if (!id)
|
41 | id = data.id;
|
42 | if (id !== data.id)
|
43 | return;
|
44 | const mcs = data.messageChannels.map(messageChannel => {
|
45 | const id = messageChannel.reduce((obj, key) => obj[key], data.msg);
|
46 | const port = wrap(smc, id);
|
47 | replaceProperty(data.msg, messageChannel, port);
|
48 | return port;
|
49 | });
|
50 | internalPort.postMessage(data.msg, mcs);
|
51 | });
|
52 | }
|
53 | function replaceProperty(obj, path, newVal) {
|
54 | for (const key of path.slice(0, -1))
|
55 | obj = obj[key];
|
56 | const key = path[path.length - 1];
|
57 | const orig = obj[key];
|
58 | obj[key] = newVal;
|
59 | return orig;
|
60 | }
|
61 | function* findMessageChannels(obj, path = []) {
|
62 | if (!obj)
|
63 | return;
|
64 | if (typeof obj === "string")
|
65 | return;
|
66 | if (obj instanceof MessagePort) {
|
67 | yield path.slice();
|
68 | return;
|
69 | }
|
70 | for (const key of Object.keys(obj)) {
|
71 | path.push(key);
|
72 | yield* findMessageChannels(obj[key], path);
|
73 | path.pop();
|
74 | }
|
75 | }
|
76 | function hex4() {
|
77 | return Math.floor((1 + Math.random()) * 0x10000)
|
78 | .toString(16)
|
79 | .substring(1);
|
80 | }
|
81 | const bits = 128;
|
82 | function generateUID() {
|
83 | return new Array(bits / 16)
|
84 | .fill(0)
|
85 | .map(_ => hex4())
|
86 | .join("");
|
87 | }
|