import { Message , MessageHandler , MessageType , deserializeMessage , } from '../communication' ; const subscriptions = new Set < MessageHandler > ( ) ; chrome . runtime . onMessage . addListener ( ( message , sender ) => { deserializeMessage ( message ) ; const cannotRespond = ( ) => { throw new Error ( 'You cannot respond through MessageHandler' ) ; } ; subscriptions . forEach ( handler => handler ( message , cannotRespond ) ) ; return true ; } ) ; export const subscribe = ( handler ) => { subscriptions . add ( handler ) ; return { unsubscribe : ( ) => subscriptions . delete ( handler ) } ; } ; export const send = < T > ( message ) => { if ( message . messageType === MessageType . CompleteTree || message . messageType === MessageType . TreeDiff || message . messageType === MessageType . DispatchWrapper ) { const description = MessageType [ message . messageType ] ; throw new Error ( `A ${ description } message should never be posted through the communication port` ) ; } return new Promise ( ( resolve , reject ) => { chrome . runtime . sendMessage ( message , response => { if ( response ) { resolve ( response ) ; } else { reject ( chrome . runtime . lastError ) ; } } ) ; } ) ; } ;