import RIL from './ril' ; RIL . install ( ) ; import { AbstractMessageReader , DataCallback , AbstractMessageWriter , Message , Disposable , Emitter , NullLogger , ConnectionStrategy , ConnectionOptions , createMessageConnection as _createMessageConnection , MessageReader , MessageWriter , Logger , MessageConnection } from '../common/api' ; export * from '../common/api' ; export class BrowserMessageReader extends AbstractMessageReader implements MessageReader { private _onData : Emitter < Message > ; private _messageListener : ( event : MessageEvent ) => void ; public constructor ( context ) { super ( ) ; this . _onData = new Emitter < Message > ( ) ; this . _messageListener = ( event ) => { this . _onData . fire ( event . data ) ; } ; context . addEventListener ( 'error' , ( event ) => this . fireError ( event ) ) ; context . onmessage = this . _messageListener ; } public listen ( callback ) { return this . _onData . event ( callback ) ; } } export class BrowserMessageWriter extends AbstractMessageWriter implements MessageWriter { private errorCount : number ; public constructor ( private context ) { super ( ) ; this . errorCount = 0 ; context . addEventListener ( 'error' , ( event ) => this . fireError ( event ) ) ; } public write ( msg ) { try { this . context . postMessage ( msg ) ; return Promise . resolve ( ) ; } catch ( error ) { this . handleError ( error , msg ) ; return Promise . reject ( error ) ; } } private handleError ( error , msg ) { this . errorCount ++ ; this . fireError ( error , msg , this . errorCount ) ; } public end ( ) { } } export function createMessageConnection ( reader , writer , logger ? , options ? ) { if ( logger === undefined ) { logger = NullLogger ; } if ( ConnectionStrategy . is ( options ) ) { options = { connectionStrategy : options } as ConnectionOptions ; } return _createMessageConnection ( reader , writer , logger , options ) ; }