import { Injectable } from '@angular/core' ; import { Message , MessageResponse } from '../../communication' ; import { deserialize } from '../../utils' ; @ Injectable ( ) export class DirectConnection { handleImmediate < T > ( message ) { return this . remoteExecute ( `inspectedApplication.handleImmediate(${ JSON . stringify ( message ) })` ) . then ( response => deserialize ( response ) ) . catch ( e => console . error ( e ) ) ; } readQueue ( processor ) { return this . remoteExecute ( 'inspectedApplication.readMessageQueue()' ) . then ( result => { const encode = value => JSON . stringify ( value ) ; for ( const message of result ) { const respond = ( response ) => { this . remoteExecute ( `inspectedApplication.response(${ encode ( response ) })` ) ; } ; processor ( message , respond ) ; } } ) . catch ( error => { throw new Error ( `Failed to read message queue: ${ error . stack || error . message }` ) ; } ) ; } private remoteExecute ( code ) { return new Promise ( ( resolve , reject ) => { type ExceptionInfo = chrome . devtools . inspectedWindow . EvaluationExceptionInfo ; const handler = ( result , exceptionInfo ) => { if ( exceptionInfo && ( exceptionInfo . isError || exceptionInfo . isException ) ) { const e = new Error ( 'Code evaluation failed' ) ; if ( exceptionInfo . isException ) { e . stack = exceptionInfo . value ; } reject ( e ) ; } else { resolve ( result ) ; } } ; chrome . devtools . inspectedWindow . eval ( code , handler ) ; } ) ; } }