import { isOpenYoloMessageFormat , Message , MESSAGE_DATA_VALIDATORS , MessageData , MessageType } from './messages' ; import { PostMessageData , PostMessageType } from './post_messages' ; import { RpcMessageData , RpcMessageType } from './rpc_messages' ; /**
 * Interface exposing only the required properties and methods of the Window
 * object that this library uses. Useful to test the logic without having to
 * handle real Window objects.
 */ export interface WindowLike extends EventTarget { parent : WindowLike ; opener : WindowLike | null ; close ( ) : void ; postMessage ( data , targetOrigin , transfer ? ) : void ; } /**
 * Sends a message to the specified target frame, optionally restricting
 * delivery to the specified target origin.
 */ export function sendMessage < T extends PostMessageType > ( target , message , targetOrigin ? ) { target . postMessage ( message , targetOrigin || '*' ) ; } /**
 * Determines whether the specified origin is part of the set of permitted
 * origins.
 */ export function isPermittedOrigin ( origin , permittedOrigins ) { return permittedOrigins . indexOf ( origin ) !== - 1 ; } export type PostMessageListener < T extends PostMessageType > = ( data : PostMessageData < T > , type : T , event : MessageEvent ) => void ; export type RpcMessageListener < T extends RpcMessageType > = ( data : RpcMessageData < T > , type : T , event : MessageEvent ) => void ; export type MessageListener < T extends MessageType > = ( data : MessageData < T > , type : T , event : MessageEvent ) => void ; export type FilteringEventListener = ( ev : MessageEvent ) => boolean ; /**
 * Returns an EventListener that captures and validates OpenYOLO messages
 * of a specific type, before
 * before passing them on to a provided handler.
 */ export function createMessageListener < T extends MessageType > ( type , listener ) { let messageListener = ( ev ) => { if ( ! isOpenYoloMessageFormat ( ev . data ) ) { return false ; } if ( ev . data [ 'type' ] !== type ) return false ; let validator = MESSAGE_DATA_VALIDATORS [ ( type as MessageType ) ] ; if ( ! validator ( ev . data [ 'data' ] ) ) { return false ; } listener ( ev . data [ 'data' ] , ev . data [ 'type' ] , ev ) ; return true ; } ; messageListener . toString = ( ) => `${ type } message listener` ; return messageListener ; } export interface TypedMessageEvent < T extends MessageType > extends MessageEvent { data : MessageData < T > ; } export type MessageEventListener < T extends MessageType > = ( ev : TypedMessageEvent < T > ) => void ;