summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/postmessage-msgport-to-client-worker.js
blob: 7af935f4f8fc01756980e46dcce5018ddc620e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
self.onmessage = function(e) {
  e.waitUntil(self.clients.matchAll().then(function(clients) {
      clients.forEach(function(client) {
          var messageChannel = new MessageChannel();
          messageChannel.port1.onmessage =
            onMessageViaMessagePort.bind(null, messageChannel.port1);
          client.postMessage(undefined, [messageChannel.port2]);
        });
    }));
};

function onMessageViaMessagePort(port, e) {
  var message = e.data;
  if ('value' in message) {
    port.postMessage({ack: 'Acking value: ' + message.value});
  } else if ('done' in message) {
    port.postMessage({done: true});
  }
}