chore: clean NetworkManager Maps for BiDi (#10468)

This commit is contained in:
Nikolay Vitkov 2023-06-29 10:16:35 +02:00 committed by GitHub
parent 1c80ebe846
commit 0a7bad6d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 12 deletions

View File

@ -20,6 +20,7 @@ import {EventEmitter, Handler} from '../EventEmitter.js';
import {NetworkManagerEmittedEvents} from '../NetworkManager.js';
import {Connection} from './Connection.js';
import {Frame} from './Frame.js';
import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js';
import {Page} from './Page.js';
@ -74,18 +75,20 @@ export class NetworkManager extends EventEmitter {
#onResponseCompleted(event: Bidi.Network.ResponseCompletedParams): void {
const request = this.#requestMap.get(event.request.request);
if (request) {
const response = new HTTPResponse(request, event);
request._response = response;
if (event.navigation) {
this.#navigationMap.set(event.navigation, response);
}
if (response.fromCache()) {
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
}
this.emit(NetworkManagerEmittedEvents.Response, response);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
if (!request) {
return;
}
const response = new HTTPResponse(request, event);
request._response = response;
if (event.navigation) {
this.#navigationMap.set(event.navigation, response);
}
if (response.fromCache()) {
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
}
this.emit(NetworkManagerEmittedEvents.Response, response);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
this.#requestMap.delete(event.request.request);
}
#onFetchError(event: Bidi.Network.FetchErrorParams) {
@ -95,10 +98,16 @@ export class NetworkManager extends EventEmitter {
}
request._failureText = event.errorText;
this.emit(NetworkManagerEmittedEvents.RequestFailed, request);
this.#requestMap.delete(event.request.request);
}
getNavigationResponse(navigationId: string | null): HTTPResponse | null {
return this.#navigationMap.get(navigationId ?? '') ?? null;
if (!navigationId) {
return null;
}
const response = this.#navigationMap.get(navigationId);
return response ?? null;
}
inFlightRequestsCount(): number {
@ -112,6 +121,20 @@ export class NetworkManager extends EventEmitter {
return inFlightRequestCounter;
}
clearMapAfterFrameDispose(frame: Frame): void {
for (const [id, request] of this.#requestMap.entries()) {
if (request.frame() === frame) {
this.#requestMap.delete(id);
}
}
for (const [id, response] of this.#navigationMap.entries()) {
if (response.frame() === frame) {
this.#requestMap.delete(id);
}
}
}
dispose(): void {
this.removeAllListeners();
this.#requestMap.clear();

View File

@ -269,6 +269,7 @@ export class Page extends PageBase {
this.#removeFramesRecursively(child);
}
frame.dispose();
this.#networkManager.clearMapAfterFrameDispose(frame);
this.#frameTree.removeFrame(frame);
this.emit(PageEmittedEvents.FrameDetached, frame);
}