type PostMessageHandler = ( value : any , name : string ) => void ; /**
 * Wrapper around the HTML postMessage API.
 */ export class PostMessageService { private window : Window ; private scope : string ; private defaultTarget : Window ; private defaultTargetOrigin : string ; constructor ( window , scope , defaultTarget , defaultTargetOrigin ) { this . window = window ; this . scope = scope ; this . defaultTarget = defaultTarget ; this . defaultTargetOrigin = defaultTargetOrigin ; } param name param value param target param targetOrigin public send ( name , value , target ? , targetOrigin = "*" ) { if ( ! target ) { if ( ! this . defaultTargetOrigin ) { return ; } target = this . defaultTarget ; targetOrigin = this . defaultTargetOrigin ; } const msg = { name , value , scope : this . scope } ; target . postMessage ( msg , targetOrigin ) ; } param name param handler public on ( name , handler ) { const listener = ( event ) => { if ( ! event . data || typeof event . data . scope !== "string" || event . data . scope !== this . scope ) { return ; } if ( event . data . name !== name ) { return ; } handler ( event . data . value , event . data . name ) ; } ; this . window . addEventListener ( "message" , listener ) ; return ( ) => { this . window . removeEventListener ( "message" , listener ) ; } ; } }