1var c;
2
3function handler(e, reply) {
4  if (e.data.ping) {
5    c.postMessage(e.data.ping);
6    return;
7  }
8  if (e.data.blob) {
9    (() => {
10      c.postMessage({blob: new Blob(e.data.blob)});
11    })();
12    // TODO(https://github.com/web-platform-tests/wpt/issues/7899): Change to
13    // some sort of cross-browser GC trigger.
14    if (self.gc) self.gc();
15  }
16  c = new BroadcastChannel(e.data.channel);
17  let messages = [];
18  c.onmessage = e => {
19      if (e.data === 'ready') {
20        // Ignore any 'ready' messages from the other thread since there could
21        // be some race conditions between this BroadcastChannel instance
22        // being created / ready to receive messages and the message being sent.
23        return;
24      }
25      messages.push(e.data);
26      if (e.data == 'done')
27        reply(messages);
28    };
29  c.postMessage('from worker');
30}
31
32onmessage = e => handler(e, postMessage);
33
34onconnect = e => {
35  let port = e.ports[0];
36  port.onmessage = e => handler(e, msg => port.postMessage(msg));
37};
38