import { Protocol } from 'devtools-protocol' ; import { HTTPRequest } from './HTTPRequest.js' ; export type QueuedEventGroup = { responseReceivedEvent : Protocol . Network . ResponseReceivedEvent ; loadingFinishedEvent ? : Protocol . Network . LoadingFinishedEvent ; loadingFailedEvent ? : Protocol . Network . LoadingFailedEvent ; } ; export type FetchRequestId = string ; export type NetworkRequestId = string ; type RedirectInfo = { event : Protocol . Network . RequestWillBeSentEvent ; fetchRequestId ? : FetchRequestId ; } ; export type RedirectInfoList = RedirectInfo [ ] ; internal export class NetworkEventManager { private _requestWillBeSentMap = new Map < NetworkRequestId , Protocol . Network . RequestWillBeSentEvent > ( ) ; private _requestPausedMap = new Map < NetworkRequestId , Protocol . Fetch . RequestPausedEvent > ( ) ; private _httpRequestsMap = new Map < NetworkRequestId , HTTPRequest > ( ) ; private _responseReceivedExtraInfoMap = new Map < NetworkRequestId , Protocol . Network . ResponseReceivedExtraInfoEvent [ ] > ( ) ; private _queuedRedirectInfoMap = new Map < NetworkRequestId , RedirectInfoList > ( ) ; private _queuedEventGroupMap = new Map < NetworkRequestId , QueuedEventGroup > ( ) ; forget ( networkRequestId ) { this . _requestWillBeSentMap . delete ( networkRequestId ) ; this . _requestPausedMap . delete ( networkRequestId ) ; this . _queuedEventGroupMap . delete ( networkRequestId ) ; this . _queuedRedirectInfoMap . delete ( networkRequestId ) ; this . _responseReceivedExtraInfoMap . delete ( networkRequestId ) ; } responseExtraInfo ( networkRequestId ) { if ( ! this . _responseReceivedExtraInfoMap . has ( networkRequestId ) ) { this . _responseReceivedExtraInfoMap . set ( networkRequestId , [ ] ) ; } return this . _responseReceivedExtraInfoMap . get ( networkRequestId ) ; } private queuedRedirectInfo ( fetchRequestId ) { if ( ! this . _queuedRedirectInfoMap . has ( fetchRequestId ) ) { this . _queuedRedirectInfoMap . set ( fetchRequestId , [ ] ) ; } return this . _queuedRedirectInfoMap . get ( fetchRequestId ) ; } queueRedirectInfo ( fetchRequestId , redirectInfo ) { this . queuedRedirectInfo ( fetchRequestId ) . push ( redirectInfo ) ; } takeQueuedRedirectInfo ( fetchRequestId ) { return this . queuedRedirectInfo ( fetchRequestId ) . shift ( ) ; } numRequestsInProgress ( ) { return [ ... this . _httpRequestsMap ] . filter ( ( [ , request ] ) => { return ! request . response ( ) ; } ) . length ; } storeRequestWillBeSent ( networkRequestId , event ) { this . _requestWillBeSentMap . set ( networkRequestId , event ) ; } getRequestWillBeSent ( networkRequestId ) { return this . _requestWillBeSentMap . get ( networkRequestId ) ; } forgetRequestWillBeSent ( networkRequestId ) { this . _requestPausedMap . delete ( networkRequestId ) ; } getRequestPaused ( networkRequestId ) { return this . _requestPausedMap . get ( networkRequestId ) ; } forgetRequestPaused ( networkRequestId ) { this . _requestPausedMap . delete ( networkRequestId ) ; } storeRequestPaused ( networkRequestId , event ) { this . _requestPausedMap . set ( networkRequestId , event ) ; } getRequest ( networkRequestId ) { return this . _httpRequestsMap . get ( networkRequestId ) ; } storeRequest ( networkRequestId , request ) { this . _httpRequestsMap . set ( networkRequestId , request ) ; } forgetRequest ( networkRequestId ) { this . _httpRequestsMap . delete ( networkRequestId ) ; } getQueuedEventGroup ( networkRequestId ) { return this . _queuedEventGroupMap . get ( networkRequestId ) ; } queueEventGroup ( networkRequestId , event ) { this . _queuedEventGroupMap . set ( networkRequestId , event ) ; } }