function onConnect()

in packages-ext/recoil-devtools/src/pages/Background/Background.js [33:90]


function onConnect(port: ConnectionPort): void {
  const connectionId = getConnectionId(port);
  const displayName = getConnectionName(port);
  let isPopupConnection = false;
  const chunksBuffer = new Map<number, string>();

  const msgHandler = (msg: BackgroundPostMessage) => {
    // ignore invalid message formats
    if (msg?.action == null) {
      return;
    }
    if (msg.action === RecoilDevToolsActions.UPDATE) {
      store.processMessage(msg, connectionId);
    } else if (msg.action === RecoilDevToolsActions.INIT) {
      store.connect(
        connectionId,
        msg.data?.persistenceLimit,
        msg.data?.initialValues,
        displayName,
        msg.data?.devMode,
        port,
      );
      debug('CONNECT', connectionId);
      // This is only needed if we want to display a popup banner
      // in addition to the devpanel.
      // chrome.pageAction.show(connectionId);
    } else if (msg.action === RecoilDevToolsActions.SUBSCRIBE_POPUP) {
      isPopupConnection = true;
      store.subscribe(port);
    } else if (msg.action === RecoilDevToolsActions.UPLOAD_CHUNK) {
      const chunkSoFar = (chunksBuffer.get(msg.txID) ?? '') + (msg.chunk ?? '');
      chunksBuffer.set(msg.txID, chunkSoFar);
      if (Boolean(msg.isFinalChunk)) {
        try {
          const data = JSON.parse(chunkSoFar);
          msgHandler(data);
        } catch (e) {
          warn('Recoil DevTools: Message failed due to "`${e.message}`"');
        } finally {
          chunksBuffer.delete(msg.txID);
        }
      }
    }
  };

  // $FlowFixMe
  port.onMessage.addListener(msgHandler);

  // $FlowFixMe
  port.onDisconnect.addListener(() => {
    debug('DISCONNECT', connectionId);
    if (isPopupConnection) {
      store.unsubscribe(port);
    } else {
      store.disconnect(connectionId);
    }
  });
}