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 {NetworkManagerEmittedEvents} from '../NetworkManager.js';
import {Connection} from './Connection.js'; import {Connection} from './Connection.js';
import {Frame} from './Frame.js';
import {HTTPRequest} from './HTTPRequest.js'; import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js'; import {HTTPResponse} from './HTTPResponse.js';
import {Page} from './Page.js'; import {Page} from './Page.js';
@ -74,7 +75,9 @@ export class NetworkManager extends EventEmitter {
#onResponseCompleted(event: Bidi.Network.ResponseCompletedParams): void { #onResponseCompleted(event: Bidi.Network.ResponseCompletedParams): void {
const request = this.#requestMap.get(event.request.request); const request = this.#requestMap.get(event.request.request);
if (request) { if (!request) {
return;
}
const response = new HTTPResponse(request, event); const response = new HTTPResponse(request, event);
request._response = response; request._response = response;
if (event.navigation) { if (event.navigation) {
@ -85,7 +88,7 @@ export class NetworkManager extends EventEmitter {
} }
this.emit(NetworkManagerEmittedEvents.Response, response); this.emit(NetworkManagerEmittedEvents.Response, response);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request); this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
} this.#requestMap.delete(event.request.request);
} }
#onFetchError(event: Bidi.Network.FetchErrorParams) { #onFetchError(event: Bidi.Network.FetchErrorParams) {
@ -95,10 +98,16 @@ export class NetworkManager extends EventEmitter {
} }
request._failureText = event.errorText; request._failureText = event.errorText;
this.emit(NetworkManagerEmittedEvents.RequestFailed, request); this.emit(NetworkManagerEmittedEvents.RequestFailed, request);
this.#requestMap.delete(event.request.request);
} }
getNavigationResponse(navigationId: string | null): HTTPResponse | null { 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 { inFlightRequestsCount(): number {
@ -112,6 +121,20 @@ export class NetworkManager extends EventEmitter {
return inFlightRequestCounter; 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 { dispose(): void {
this.removeAllListeners(); this.removeAllListeners();
this.#requestMap.clear(); this.#requestMap.clear();

View File

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