lib/index.js
/**
 * Bind a Redux store to handle events generated by a port.
 *
 * @param {object} store - The Redux store to expose.
 * @param {object} port - The port that will generate events.
 * @param {Function} disconnectCallback - A function that will be called when
 *     the port disconnects with the disconnected port and the store. This is
 *     useful for doing state cleanup if necessary.
 */
Bind a Redux store to handle events generated by a port.
@param {object} store - The Redux store to expose.
@param {object} port - The port that will generate events.
@param {Function} disconnectCallback - A function that will be called when
the port disconnects with the disconnected port and the store. This is
useful for doing state cleanup if necessary.
Bind a Redux store to handle events generated by a port . @param { object } store - The Redux store to expose . @param { object } port - The port that will generate events . @param { Function } disconnectCallback - A function that will be called when the port disconnects with the disconnected port and the store . This is useful for doing state cleanup if necessary .
function registerPortListeners(store, port, disconnectCallback) {
  function sendStateSync() {
    port.postMessage({
      type: "stateSync",
      payload: store.getState()
    });
  }
  function respond(id, dispatchResult) {
    dispatchResult.then(() => {
      port.postMessage({ type: "dispatchResolved", payload: { id } });
    }).catch(() => {
      port.postMessage({ type: "dispatchRejected", payload: { id } });
    });
  }
  function messageListener(message) {
    switch (message.type) {
      case "dispatch":
        try {
          respond(message.id, Promise.resolve(store.dispatch(message.payload)));
        } catch (err) {
          respond(message.id, Promise.reject(err));
        }
        break;
      case "requestStateSync":
        sendStateSync();
        break;
      default:
        throw new Error(`Unknown message type: ${message.type}`);
    }
  }
  function handleDisconnect(disconnectedPort) {
    unsubscribe();
    if (disconnectCallback) {
      disconnectCallback(disconnectedPort, store);
    }
  }
  let unsubscribe = store.subscribe(sendStateSync);
  port.onMessage.addListener(messageListener);
  port.onDisconnect.addListener(handleDisconnect);
}
function registerPortListeners ( store , port , disconnectCallback ) { function sendStateSync ( ) { port . postMessage ( { type : "stateSync" , payload : store . getState ( ) } ) ; } function respond ( id , dispatchResult ) { dispatchResult . then ( ( ) => { port . postMessage ( { type : "dispatchResolved" , payload : { id } } ) ; } ) . catch ( ( ) => { port . postMessage ( { type : "dispatchRejected" , payload : { id } } ) ; } ) ; } function messageListener ( message ) { switch ( message . type ) { case "dispatch" : try { respond ( message . id , Promise . resolve ( store . dispatch ( message . payload ) ) ) ; } catch ( err ) { respond ( message . id , Promise . reject ( err ) ) ; } break ; case "requestStateSync" : sendStateSync ( ) ; break ; default : throw new Error ( ` ${ message . type } ` ) ; } } function handleDisconnect ( disconnectedPort ) { unsubscribe ( ) ; if ( disconnectCallback ) { disconnectCallback ( disconnectedPort , store ) ; } } let unsubscribe = store . subscribe ( sendStateSync ) ; port . onMessage . addListener ( messageListener ) ; port . onDisconnect . addListener ( handleDisconnect ) ; }
Bind a Redux store to handle events generated by a port.
Bind a Redux store to handle events generated by a port .