chore: allow aborting in-flight routes on context reuse (#15460)

This commit is contained in:
Pavel Feldman 2022-07-07 12:07:09 -08:00 committed by GitHub
parent be4f27d685
commit 5fd6ce4de0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -75,6 +75,7 @@ export abstract class BrowserContext extends SdkObject {
readonly _tempDirs: string[] = []; readonly _tempDirs: string[] = [];
private _settingStorageState = false; private _settingStorageState = false;
readonly initScripts: string[] = []; readonly initScripts: string[] = [];
private _routesInFlight = new Set<network.Route>();
constructor(browser: Browser, options: channels.BrowserNewContextParams, browserContextId: string | undefined) { constructor(browser: Browser, options: channels.BrowserNewContextParams, browserContextId: string | undefined) {
super(browser, 'browser-context'); super(browser, 'browser-context');
@ -458,6 +459,19 @@ export abstract class BrowserContext extends SdkObject {
const recorder = this._harRecorders.get(harId || '')!; const recorder = this._harRecorders.get(harId || '')!;
return recorder.export(); return recorder.export();
} }
addRouteInFlight(route: network.Route) {
this._routesInFlight.add(route);
}
removeRouteInFlight(route: network.Route) {
this._routesInFlight.delete(route);
}
async _cancelAllRoutesInFlight() {
await Promise.all([...this._routesInFlight].map(r => r.abort())).catch(() => {});
this._routesInFlight.clear();
}
} }
export function assertBrowserContextIsNotOwned(context: BrowserContext) { export function assertBrowserContextIsNotOwned(context: BrowserContext) {

View File

@ -232,6 +232,7 @@ export class Route extends SdkObject {
super(request._frame || request._context , 'route'); super(request._frame || request._context , 'route');
this._request = request; this._request = request;
this._delegate = delegate; this._delegate = delegate;
this._request._context.addRouteInFlight(this);
} }
request(): Request { request(): Request {
@ -241,6 +242,7 @@ export class Route extends SdkObject {
async abort(errorCode: string = 'failed') { async abort(errorCode: string = 'failed') {
this._startHandling(); this._startHandling();
await this._delegate.abort(errorCode); await this._delegate.abort(errorCode);
this._endHandling();
} }
async redirectNavigationRequest(url: string) { async redirectNavigationRequest(url: string) {
@ -272,6 +274,7 @@ export class Route extends SdkObject {
body, body,
isBase64, isBase64,
}); });
this._endHandling();
} }
// See https://github.com/microsoft/playwright/issues/12929 // See https://github.com/microsoft/playwright/issues/12929
@ -301,12 +304,17 @@ export class Route extends SdkObject {
throw new Error('New URL must have same protocol as overridden URL'); throw new Error('New URL must have same protocol as overridden URL');
} }
await this._delegate.continue(this._request, overrides); await this._delegate.continue(this._request, overrides);
this._endHandling();
} }
private _startHandling() { private _startHandling() {
assert(!this._handled, 'Route is already handled!'); assert(!this._handled, 'Route is already handled!');
this._handled = true; this._handled = true;
} }
private _endHandling() {
this._request._context.removeRouteInFlight(this);
}
} }
export type RouteHandler = (route: Route, request: Request) => void; export type RouteHandler = (route: Route, request: Request) => void;