function __initWorkerMessageHandler() {

  let ports = {}; // all "worker" ports currently alive, keyed by ID.

  function messageHandler(event) {
    // We will ignore all messages destined for otherType.
    let data = event.data;
    let portid = data.portId;
    let port;
    if (!data.portFromType || data.portFromType === "worker") {
      // this is a message posted by ourself so ignore it.
      return;
    }
    switch (data.portTopic) {
      case "port-create":
        // a new port was created on the "client" side - create a new worker
        // port and store it in the map
        port = new WorkerPort(portid);
        ports[portid] = port;
        // and call the "onconnect" handler.
        onconnect({ports: [port]});
        break;

      case "port-close":
        // the client side of the port was closed, so close this side too.
        port = ports[portid];
        if (!port) {
          // port already closed (which will happen when we call port.close()
          // below - the client side will send us this message but we've
          // already closed it.)
          return;
        }
        delete ports[portid];
        port.close();
        break;

      case "port-message":
        // the client posted a message to this worker port.
        port = ports[portid];
        if (!port) {
          // port must be closed - this shouldn't happen!
          return;
        }
        port._onmessage(data.data);
        break;

      default:
        break;
    }
  }
  // addEventListener is injected into the sandbox.
  _addEventListener('message', messageHandler);
}
// Thus, it is only ever dealing with "worker" ports.
Thus, it is only ever dealing with "worker" ports.
Thus it is only ever dealing with " worker " ports .
function __initWorkerMessageHandler() {
  let ports = {}; 
  function messageHandler(event) {
    let data = event.data;
    let portid = data.portId;
    let port;
    if (!data.portFromType || data.portFromType === "worker") {
      return;
    }
    switch (data.portTopic) {
      case "port-create":
        port = new WorkerPort(portid);
        ports[portid] = port;
        onconnect({ports: [port]});
        break;
      case "port-close":
        port = ports[portid];
        if (!port) {
          return;
        }
        delete ports[portid];
        port.close();
        break;
      case "port-message":
        port = ports[portid];
        if (!port) {
          return;
        }
        port._onmessage(data.data);
        break;
      default:
        break;
    }
  }
  _addEventListener('message', messageHandler);
}
function __initWorkerMessageHandler ( ) { let ports = { } ; function messageHandler ( event ) { let data = event . data ; let portid = data . portId ; let port ; if ( ! data . portFromType || data . portFromType === "worker" ) { return ; } switch ( data . portTopic ) { case "port-create" : port = new WorkerPort ( portid ) ; ports [ portid ] = port ; onconnect ( { ports : [ port ] } ) ; break ; case "port-close" : port = ports [ portid ] ; if ( ! port ) { return ; } delete ports [ portid ] ; port . close ( ) ; break ; case "port-message" : port = ports [ portid ] ; if ( ! port ) { return ; } port . _onmessage ( data . data ) ; break ; default : break ; } } _addEventListener ( 'message' , messageHandler ) ; }
Thus, it is only ever dealing with "worker" ports.
Thus it is only ever dealing with " worker " ports .
// all "worker" ports currently alive, keyed by ID. // We will ignore all messages destined for otherType. // this is a message posted by ourself so ignore it. // a new port was created on the "client" side - create a new worker // port and store it in the map // and call the "onconnect" handler. // the client side of the port was closed, so close this side too. // port already closed (which will happen when we call port.close() // below - the client side will send us this message but we've // already closed it.) // the client posted a message to this worker port. // port must be closed - this shouldn't happen! // addEventListener is injected into the sandbox.