export namespace MessageListeners { export function createListener < T > ( type , handler ) { return ( event ) => { if ( event . data . type === type ) { handler ( event . data . value ) ; } } ; } export type MessageDispatcher < M > = ( message : M ) => void ; export function createDispatcher < M > ( type ) { function computeTarget ( ) { let result = window ; while ( result . parent && result . parent !== result ) { result = result . parent ; } return result ; } const target = computeTarget ( ) ; return ( message ) => { target . postMessage ( { type , value : message } , '*' ) ; } ; } }