src/index.js
function workerMessageHandler(e) {
  const msg = e.data;
  var cb;

  SHOULD_LOG && console.log("Worker received message: " + JSON.stringify(msg));

  switch (msg.event) {
    case "connInit":
      workerInitialized = true;
      workerQueue.forEach(workerInvoke);
      workerQueue = [];
      break;

    case "connStatus":
      const response = msg.data || {};
      const statusCode = response.status;
      if (connStatus[msg.conn]) {
        switch (statusCode) {
          case 200:
            connStatus[msg.conn].ready = true;
            break;
          case 401: // authorization error, need to log in
            connStatus[msg.conn].ready = false;
            connStatus[msg.conn].user = null;
            connStatus[msg.conn].anonymous = true;
            break;
          default:
            console.warn("Invalid connection response status: " + JSON.stringify(response));
            break;
        }
      }
      break;

    case "connClosed":
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    case "connLogout":
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    case "setState":
      const comp = componentIdx[msg.ref];
      if (comp) {
        comp.setState(msg.data);
      } else {
        SHOULD_LOG && console.warn("Component no longer registered: " + msg.ref);
      }
      break;

    case "remoteInvoke":
      // check for a callback
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    case "login":
      // if login successful, update conn's connStatus
      if (msg.data.status === 200) {
        connStatus[msg.conn].token = msg.data.body.token;
        connStatus[msg.conn].user = msg.data.body.user;
        connStatus[msg.conn].anonymous = msg.data.body.anonymous;
      }
      // if there was a callback passed to login(), execute
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    default:
      SHOULD_LOG && console.warn("Unreconized event from worker: " + msg.event + ". Full message: " + JSON.stringify(msg));
      break;
  }
  return;
}
function workerMessageHandler(e) {
  const msg = e.data;
  var cb;

  SHOULD_LOG && console.log("Worker received message: " + JSON.stringify(msg));

  switch (msg.event) {
    case "connInit":
      workerInitialized = true;
      workerQueue.forEach(workerInvoke);
      workerQueue = [];
      break;

    case "connStatus":
      const response = msg.data || {};
      const statusCode = response.status;
      if (connStatus[msg.conn]) {
        switch (statusCode) {
          case 200:
            connStatus[msg.conn].ready = true;
            break;
          case 401: // authorization error, need to log in
            connStatus[msg.conn].ready = false;
            connStatus[msg.conn].user = null;
            connStatus[msg.conn].anonymous = true;
            break;
          default:
            console.warn("Invalid connection response status: " + JSON.stringify(response));
            break;
        }
      }
      break;

    case "connClosed":
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    case "connLogout":
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    case "setState":
      const comp = componentIdx[msg.ref];
      if (comp) {
        comp.setState(msg.data);
      } else {
        SHOULD_LOG && console.warn("Component no longer registered: " + msg.ref);
      }
      break;

    case "remoteInvoke":
      // check for a callback
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    case "login":
      // if login successful, update conn's connStatus
      if (msg.data.status === 200) {
        connStatus[msg.conn].token = msg.data.body.token;
        connStatus[msg.conn].user = msg.data.body.user;
        connStatus[msg.conn].anonymous = msg.data.body.anonymous;
      }
      // if there was a callback passed to login(), execute
      cb = callbackRegistry[msg.ref];
      if (cb) {
        delete callbackRegistry[msg.ref];
        cb(msg.data);
      }
      break;

    default:
      SHOULD_LOG && console.warn("Unreconized event from worker: " + msg.event + ". Full message: " + JSON.stringify(msg));
      break;
  }
  return;
}
function workerMessageHandler ( e ) { const msg = e . data ; var cb ; SHOULD_LOG && console . log ( "Worker received message: " + JSON . stringify ( msg ) ) ; switch ( msg . event ) { case "connInit" : workerInitialized = true ; workerQueue . forEach ( workerInvoke ) ; workerQueue = [ ] ; break ; case "connStatus" : const response = msg . data || { } ; const statusCode = response . status ; if ( connStatus [ msg . conn ] ) { switch ( statusCode ) { case 200 : connStatus [ msg . conn ] . ready = true ; break ; case 401 : // authorization error, need to log in connStatus [ msg . conn ] . ready = false ; connStatus [ msg . conn ] . user = null ; connStatus [ msg . conn ] . anonymous = true ; break ; default : console . warn ( "Invalid connection response status: " + JSON . stringify ( response ) ) ; break ; } } break ; case "connClosed" : cb = callbackRegistry [ msg . ref ] ; if ( cb ) { delete callbackRegistry [ msg . ref ] ; cb ( msg . data ) ; } break ; case "connLogout" : cb = callbackRegistry [ msg . ref ] ; if ( cb ) { delete callbackRegistry [ msg . ref ] ; cb ( msg . data ) ; } break ; case "setState" : const comp = componentIdx [ msg . ref ] ; if ( comp ) { comp . setState ( msg . data ) ; } else { SHOULD_LOG && console . warn ( "Component no longer registered: " + msg . ref ) ; } break ; case "remoteInvoke" : // check for a callback cb = callbackRegistry [ msg . ref ] ; if ( cb ) { delete callbackRegistry [ msg . ref ] ; cb ( msg . data ) ; } break ; case "login" : // if login successful, update conn's connStatus if ( msg . data . status === 200 ) { connStatus [ msg . conn ] . token = msg . data . body . token ; connStatus [ msg . conn ] . user = msg . data . body . user ; connStatus [ msg . conn ] . anonymous = msg . data . body . anonymous ; } // if there was a callback passed to login(), execute cb = callbackRegistry [ msg . ref ] ; if ( cb ) { delete callbackRegistry [ msg . ref ] ; cb ( msg . data ) ; } break ; default : SHOULD_LOG && console . warn ( "Unreconized event from worker: " + msg . event + ". Full message: " + JSON . stringify ( msg ) ) ; break ; } return ; }
worker.onmessage handler
worker . onmessage handler