import Protocol from 'devtools-protocol' ; public export class FrameNavigation { private resolve : ( res : Protocol . Network . ResponseReceivedEvent [ ] ) => void ; private promise : Promise < Protocol . Network . ResponseReceivedEvent [ ] > ; private outstandingRequests : Set < string > ; private responses : Protocol . Network . ResponseReceivedEvent [ ] ; private waitForLoad : boolean ; private onLoadEventFired : boolean ; constructor ( waitForLoad ) { this . outstandingRequests = new Set ( ) ; this . responses = [ ] ; this . waitForLoad = waitForLoad ; this . onLoadEventFired = false ; this . promise = new Promise ( ( resolve , reject ) => { const timeout = setTimeout ( ( ) => { reject ( new Error ( 'Response timeout' ) ) ; } , 10000 ) ; this . resolve = ( res ) => { clearTimeout ( timeout ) ; resolve ( res ) ; } ; } ) ; } onRequestWillBeSent ( req ) { this . outstandingRequests . add ( req . requestId ) ; } resolveIfComplete ( ) { if ( this . onLoadEventFired && this . outstandingRequests . size === 0 ) { this . resolve ( this . responses ) ; } } onNetworkResponse ( res ) { this . responses . push ( res ) ; this . outstandingRequests . delete ( res . requestId ) ; this . resolveIfComplete ( ) ; } onNavigationComplete ( ) { if ( ! this . waitForLoad ) { this . resolve ( this . responses ) ; } } getPromise ( ) { return this . promise ; } onLoadEvent ( ) { this . onLoadEventFired = true ; this . resolveIfComplete ( ) ; } } public export class FrameStore { private frames : { [ frameId ] : FrameNavigation ; } ; constructor ( ) { this . frames = { } ; } start ( frameId , waitForLoad ) { const nav = new FrameNavigation ( waitForLoad ) ; this . frames [ frameId ] = nav ; const navPromise = nav . getPromise ( ) ; return navPromise ; } onNetworkResponse ( res ) { if ( res . frameId ) { const nav = this . frames [ res . frameId ] ; if ( nav ) { nav . onNetworkResponse ( res ) ; } } else { throw new Error ( 'Received network response without frameId' ) ; } } onRequestWillBeSent ( req ) { if ( req . frameId ) { const nav = this . frames [ req . frameId ] ; if ( nav ) { nav . onRequestWillBeSent ( req ) ; } } else { throw new Error ( 'Received "request will be sent" event without frameId' ) ; } } onNavigationComplete ( result ) { const nav = this . frames [ result . frame . id ] ; if ( nav ) { nav . onNavigationComplete ( ) ; } } onLoadEvent ( ) { Object . keys ( this . frames ) . forEach ( ( frameId ) => { this . frames [ frameId ] . onLoadEvent ( ) ; } ) ; } }