import { SecureChannel } from '../protocol/secure_channel' ; import { ListenerManager } from './listeners' ; import { createMessageEvent } from './messages' ; /**
 * A MessageChannel-like object that works within a single window.
 */ export class FakeMessageChannel implements MessageChannel { port1 : FakeMessagePort ; port2 : FakeMessagePort ; constructor ( ) { this . port1 = new FakeMessagePort ( ) ; this . port2 = new FakeMessagePort ( ) ; this . port1 . twin = this . port2 ; this . port2 . twin = this . port1 ; } } /**
 * A MessagePort-like object that works within a single window.
 */ export class FakeMessagePort implements MessagePort { twin : FakeMessagePort ; closed : boolean ; listeners : ListenerManager = new ListenerManager ( ) ; onmessage ( this , ev ) { } close ( ) { this . closed = true ; } postMessage ( message ? , ports ? ) { this . twin . twinDispatch ( createMessageEvent ( message ) ) ; } start ( ) { } addEventListener ( type , listener , useCapture ? ) { this . listeners . add ( type , listener ) ; } dispatchEvent ( evt ) { this . listeners . dispatch ( evt . type , evt ) ; return true ; } private twinDispatch ( evt ) { this . listeners . dispatch ( 'message' , evt ) ; } removeEventListener ( type , listener ? , useCapture ? ) { if ( listener ) { this . listeners . remove ( type , listener ) ; } } } export class FakeProviderConnection { public readonly clientChannel : SecureChannel ; public readonly providerChannel : SecureChannel ; public readonly messageChannel : FakeMessageChannel ; public readonly clientPort : FakeMessagePort ; public readonly providerPort : FakeMessagePort ; constructor ( ) { this . messageChannel = new FakeMessageChannel ( ) ; this . clientPort = this . messageChannel . port1 ; this . providerPort = this . messageChannel . port2 ; this . clientChannel = new SecureChannel ( this . clientPort ) ; this . providerChannel = new SecureChannel ( this . providerPort ) ; } }