import { createGlobalMessenger , isUndefined } from './global' ; import { GlobalMessenger , Listener , MessengerType } from './types' ; let id = 0 ; function getId ( ) { id ++ ; return id . toString ( ) ; } let globalMessenger ; type ListenersMap < U > = { [ K in keyof U ] : Listener ; } ; export function createMainThreadMessenger < MessagesToSend extends ListenersMap < MessagesToSend > , MessagesToListen extends ListenersMap < MessagesToListen > > ( name ? ) { if ( isUndefined ( figma ) ) { throw Error ( 'Attempted to create a messanger on wrong side.' ) ; } return createMessenger < MessagesToSend , MessagesToListen > ( MessengerType . main , name ) ; } export function createIframeMessenger < MessagesToSend extends ListenersMap < MessagesToSend > , MessagesToListen extends ListenersMap < MessagesToListen > > ( name ? ) { return createMessenger < MessagesToSend , MessagesToListen > ( MessengerType . iframe , name ) ; } function createMessenger < MessagesToSend extends ListenersMap < MessagesToSend > , MessagesToListen extends ListenersMap < MessagesToListen > > ( type , name ? ) { /**
     * IMPORTANT.
     * All messenger instances share same Global Messenger –
     * store for all listeners and original subscriber and receiver
     * to the messages from the other side (iframe/main).
     */ globalMessenger = globalMessenger || createGlobalMessenger ( type ) ; const id = getId ( ) ; globalMessenger . addMessengerInstanceToStore ( id , name ) ; function send < E extends keyof MessagesToSend > ( message ) ; function send < E extends keyof MessagesToSend > ( message , ... args ) ; function send < E extends keyof MessagesToSend > ( message , ... args ) { globalMessenger . sendMessage ( name , message , args ) ; } return { on < E extends keyof MessagesToListen > ( message , listener ) { globalMessenger . addListener ( id , message , listener ) ; } , off < E extends keyof MessagesToListen > ( message , listener ? ) { globalMessenger . removeListener ( id , message , listener ) ; } , send } ; }