Skip to content

Commit

Permalink
chore: make parent scope explicit (#16819)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Aug 25, 2022
1 parent de2eb30 commit a07a4a2
Show file tree
Hide file tree
Showing 33 changed files with 212 additions and 178 deletions.
4 changes: 2 additions & 2 deletions packages/playwright-core/src/cli/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import fs from 'fs';
import * as playwright from '../..';
import type { BrowserType } from '../client/browserType';
import type { LaunchServerOptions } from '../client/types';
import { createPlaywright, DispatcherConnection, Root, PlaywrightDispatcher } from '../server';
import { createPlaywright, DispatcherConnection, RootDispatcher, PlaywrightDispatcher } from '../server';
import type { Playwright } from '../server';
import { IpcTransport, PipeTransport } from '../protocol/transport';
import { PlaywrightServer } from '../remote/playwrightServer';
Expand All @@ -38,7 +38,7 @@ export function printApiJson() {

export function runDriver() {
const dispatcherConnection = new DispatcherConnection();
new Root(dispatcherConnection, async (rootScope, { sdkLanguage }) => {
new RootDispatcher(dispatcherConnection, async (rootScope, { sdkLanguage }) => {
const playwright = createPlaywright(sdkLanguage);
return new PlaywrightDispatcher(rootScope, playwright);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
this._channel.on('bindingCall', ({ binding }) => this._onBinding(BindingCall.from(binding)));
this._channel.on('close', () => this._onClose());
this._channel.on('page', ({ page }) => this._onPage(Page.from(page)));
this._channel.on('route', ({ route, request }) => this._onRoute(network.Route.from(route), network.Request.from(request)));
this._channel.on('route', ({ route }) => this._onRoute(network.Route.from(route)));
this._channel.on('backgroundPage', ({ page }) => {
const backgroundPage = Page.from(page);
this._backgroundPages.add(backgroundPage);
Expand Down Expand Up @@ -147,14 +147,14 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
response._finishedPromise.resolve();
}

async _onRoute(route: network.Route, request: network.Request) {
async _onRoute(route: network.Route) {
const routeHandlers = this._routes.slice();
for (const routeHandler of routeHandlers) {
if (!routeHandler.matches(request.url()))
if (!routeHandler.matches(route.request().url()))
continue;
if (routeHandler.willExpire())
this._routes.splice(this._routes.indexOf(routeHandler), 1);
const handled = await routeHandler.handle(route, request);
const handled = await routeHandler.handle(route);
if (!this._routes.length)
this._wrapApiCall(() => this._disableInterception(), true).catch(() => {});
if (handled)
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,14 @@ export class RouteHandler {
return urlMatches(this._baseURL, requestURL, this.url);
}

public async handle(route: Route, request: Request): Promise<boolean> {
public async handle(route: Route): Promise<boolean> {
++this.handledCount;
const handledPromise = route._startHandling();
// Extract handler into a variable to avoid [RouteHandler.handler] in the stack.
const handler = this.handler;
const [handled] = await Promise.all([
handledPromise,
handler(route, request),
handler(route, route.request()),
]);
return handled;
}
Expand Down
13 changes: 7 additions & 6 deletions packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ import { Keyboard, Mouse, Touchscreen } from './input';
import { assertMaxArguments, JSHandle, parseResult, serializeArgument } from './jsHandle';
import type { FrameLocator, Locator, LocatorOptions } from './locator';
import type { RouteHandlerCallback } from './network';
import { Request, Response, Route, RouteHandler, validateHeaders, WebSocket } from './network';
import { Response, Route, RouteHandler, validateHeaders, WebSocket } from './network';
import type { Request } from './network';
import type { FilePayload, Headers, LifecycleEvent, SelectOption, SelectOptionOptions, Size, URLMatch, WaitForEventOptions, WaitForFunctionOptions } from './types';
import { Video } from './video';
import { Waiter } from './waiter';
Expand Down Expand Up @@ -145,7 +146,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
this._channel.on('frameAttached', ({ frame }) => this._onFrameAttached(Frame.from(frame)));
this._channel.on('frameDetached', ({ frame }) => this._onFrameDetached(Frame.from(frame)));
this._channel.on('pageError', ({ error }) => this.emit(Events.Page.PageError, parseError(error)));
this._channel.on('route', ({ route, request }) => this._onRoute(Route.from(route), Request.from(request)));
this._channel.on('route', ({ route }) => this._onRoute(Route.from(route)));
this._channel.on('video', ({ artifact }) => {
const artifactObject = Artifact.from(artifact);
this._forceVideo()._artifactReady(artifactObject);
Expand Down Expand Up @@ -177,20 +178,20 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
this.emit(Events.Page.FrameDetached, frame);
}

private async _onRoute(route: Route, request: Request) {
private async _onRoute(route: Route) {
const routeHandlers = this._routes.slice();
for (const routeHandler of routeHandlers) {
if (!routeHandler.matches(request.url()))
if (!routeHandler.matches(route.request().url()))
continue;
if (routeHandler.willExpire())
this._routes.splice(this._routes.indexOf(routeHandler), 1);
const handled = await routeHandler.handle(route, request);
const handled = await routeHandler.handle(route);
if (!this._routes.length)
this._wrapApiCall(() => this._disableInterception(), true).catch(() => {});
if (handled)
return;
}
await this._browserContext._onRoute(route, request);
await this._browserContext._onRoute(route);
}

async _onBinding(bindingCall: BindingCall) {
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/inProcessFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { Playwright as PlaywrightAPI } from './client/playwright';
import { createPlaywright, DispatcherConnection, Root, PlaywrightDispatcher } from './server';
import { createPlaywright, DispatcherConnection, RootDispatcher, PlaywrightDispatcher } from './server';
import { Connection } from './client/connection';
import { BrowserServerLauncherImpl } from './browserServerImpl';

Expand All @@ -29,7 +29,7 @@ export function createInProcessPlaywright(): PlaywrightAPI {
dispatcherConnection.onmessage = message => clientConnection.dispatch(message);
clientConnection.onmessage = message => dispatcherConnection.dispatch(message);

const rootScope = new Root(dispatcherConnection);
const rootScope = new RootDispatcher(dispatcherConnection);

// Initialize Playwright channel.
new PlaywrightDispatcher(rootScope, playwright);
Expand Down
2 changes: 0 additions & 2 deletions packages/playwright-core/src/protocol/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,6 @@ export type BrowserContextPageEvent = {
};
export type BrowserContextRouteEvent = {
route: RouteChannel,
request: RequestChannel,
};
export type BrowserContextVideoEvent = {
artifact: ArtifactChannel,
Expand Down Expand Up @@ -1586,7 +1585,6 @@ export type PagePageErrorEvent = {
};
export type PageRouteEvent = {
route: RouteChannel,
request: RequestChannel,
};
export type PageVideoEvent = {
artifact: ArtifactChannel,
Expand Down
2 changes: 0 additions & 2 deletions packages/playwright-core/src/protocol/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,6 @@ BrowserContext:
route:
parameters:
route: Route
request: Request

video:
parameters:
Expand Down Expand Up @@ -1422,7 +1421,6 @@ Page:
route:
parameters:
route: Route
request: Request

video:
parameters:
Expand Down
2 changes: 0 additions & 2 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ scheme.BrowserContextPageEvent = tObject({
});
scheme.BrowserContextRouteEvent = tObject({
route: tChannel(['Route']),
request: tChannel(['Request']),
});
scheme.BrowserContextVideoEvent = tObject({
artifact: tChannel(['Artifact']),
Expand Down Expand Up @@ -842,7 +841,6 @@ scheme.PagePageErrorEvent = tObject({
});
scheme.PageRouteEvent = tObject({
route: tChannel(['Route']),
request: tChannel(['Request']),
});
scheme.PageVideoEvent = tObject({
artifact: tChannel(['Artifact']),
Expand Down
16 changes: 8 additions & 8 deletions packages/playwright-core/src/remote/playwrightConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import type { WebSocket } from '../utilsBundle';
import type { Playwright, DispatcherScope } from '../server';
import { createPlaywright, DispatcherConnection, Root, PlaywrightDispatcher } from '../server';
import type { Playwright } from '../server';
import { createPlaywright, DispatcherConnection, RootDispatcher, PlaywrightDispatcher } from '../server';
import { Browser } from '../server/browser';
import { serverSideCallMetadata } from '../server/instrumentation';
import { gracefullyCloseAll } from '../utils/processLauncher';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class PlaywrightConnection {
private _disconnected = false;
private _preLaunched: PreLaunched;
private _options: Options;
private _root: Root;
private _root: RootDispatcher;

constructor(lock: Promise<void>, mode: Mode, ws: WebSocket, options: Options, preLaunched: PreLaunched, log: (m: string) => void, onClose: () => void) {
this._ws = ws;
Expand All @@ -72,7 +72,7 @@ export class PlaywrightConnection {
ws.on('close', () => this._onDisconnect());
ws.on('error', error => this._onDisconnect(error));

this._root = new Root(this._dispatcherConnection, async scope => {
this._root = new RootDispatcher(this._dispatcherConnection, async scope => {
if (mode === 'reuse-browser')
return await this._initReuseBrowsersMode(scope);
if (mode === 'use-pre-launched-browser')
Expand All @@ -83,7 +83,7 @@ export class PlaywrightConnection {
});
}

private async _initPlaywrightConnectMode(scope: DispatcherScope) {
private async _initPlaywrightConnectMode(scope: RootDispatcher) {
this._debugLog(`engaged playwright.connect mode`);
const playwright = createPlaywright('javascript');
// Close all launched browsers on disconnect.
Expand All @@ -93,7 +93,7 @@ export class PlaywrightConnection {
return new PlaywrightDispatcher(scope, playwright, socksProxy);
}

private async _initLaunchBrowserMode(scope: DispatcherScope) {
private async _initLaunchBrowserMode(scope: RootDispatcher) {
this._debugLog(`engaged launch mode for "${this._options.browserName}"`);

const playwright = createPlaywright('javascript');
Expand All @@ -112,7 +112,7 @@ export class PlaywrightConnection {
return new PlaywrightDispatcher(scope, playwright, socksProxy, browser);
}

private async _initPreLaunchedBrowserMode(scope: DispatcherScope) {
private async _initPreLaunchedBrowserMode(scope: RootDispatcher) {
this._debugLog(`engaged pre-launched mode`);
const playwright = this._preLaunched.playwright!;
const browser = this._preLaunched.browser!;
Expand All @@ -130,7 +130,7 @@ export class PlaywrightConnection {
return playwrightDispatcher;
}

private async _initReuseBrowsersMode(scope: DispatcherScope) {
private async _initReuseBrowsersMode(scope: RootDispatcher) {
this._debugLog(`engaged reuse browsers mode for ${this._options.browserName}`);
const playwright = this._preLaunched.playwright!;
const requestedOptions = launchOptionsHash(this._options.launchOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

import type { DispatcherScope } from './dispatcher';
import type { RootDispatcher } from './dispatcher';
import { Dispatcher, existingDispatcher } from './dispatcher';
import type { Android, SocketBackend } from '../android/android';
import { AndroidDevice } from '../android/android';
import type * as channels from '../../protocol/channels';
import { BrowserContextDispatcher } from './browserContextDispatcher';
import type { CallMetadata } from '../instrumentation';

export class AndroidDispatcher extends Dispatcher<Android, channels.AndroidChannel> implements channels.AndroidChannel {
export class AndroidDispatcher extends Dispatcher<Android, channels.AndroidChannel, RootDispatcher, AndroidDispatcher> implements channels.AndroidChannel {
_type_Android = true;
constructor(scope: DispatcherScope, android: Android) {
constructor(scope: RootDispatcher, android: Android) {
super(scope, android, 'Android', {}, true);
}

Expand All @@ -40,16 +40,16 @@ export class AndroidDispatcher extends Dispatcher<Android, channels.AndroidChann
}
}

export class AndroidDeviceDispatcher extends Dispatcher<AndroidDevice, channels.AndroidDeviceChannel> implements channels.AndroidDeviceChannel {
export class AndroidDeviceDispatcher extends Dispatcher<AndroidDevice, channels.AndroidDeviceChannel, AndroidDispatcher, AndroidDeviceDispatcher> implements channels.AndroidDeviceChannel {
_type_EventTarget = true;
_type_AndroidDevice = true;

static from(scope: DispatcherScope, device: AndroidDevice): AndroidDeviceDispatcher {
static from(scope: AndroidDispatcher, device: AndroidDevice): AndroidDeviceDispatcher {
const result = existingDispatcher<AndroidDeviceDispatcher>(device);
return result || new AndroidDeviceDispatcher(scope, device);
}

constructor(scope: DispatcherScope, device: AndroidDevice) {
constructor(scope: AndroidDispatcher, device: AndroidDevice) {
super(scope, device, 'AndroidDevice', {
model: device.model,
serial: device.serial,
Expand Down Expand Up @@ -174,10 +174,10 @@ export class AndroidDeviceDispatcher extends Dispatcher<AndroidDevice, channels.
}
}

export class AndroidSocketDispatcher extends Dispatcher<SocketBackend, channels.AndroidSocketChannel> implements channels.AndroidSocketChannel {
export class AndroidSocketDispatcher extends Dispatcher<SocketBackend, channels.AndroidSocketChannel, AndroidDeviceDispatcher, AndroidSocketDispatcher> implements channels.AndroidSocketChannel {
_type_AndroidSocket = true;

constructor(scope: DispatcherScope, socket: SocketBackend) {
constructor(scope: AndroidDeviceDispatcher, socket: SocketBackend) {
super(scope, socket, 'AndroidSocket', {}, true);
this.addObjectListener('data', (data: Buffer) => this._dispatchEvent('data', { data }));
this.addObjectListener('close', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/

import type * as channels from '../../protocol/channels';
import type { DispatcherScope } from './dispatcher';
import { Dispatcher } from './dispatcher';
import type { DispatcherScope } from './dispatcher';
import { StreamDispatcher } from './streamDispatcher';
import fs from 'fs';
import { mkdirIfNeeded } from '../../utils/fileUtils';
import type { Artifact } from '../artifact';

export class ArtifactDispatcher extends Dispatcher<Artifact, channels.ArtifactChannel> implements channels.ArtifactChannel {
export class ArtifactDispatcher extends Dispatcher<Artifact, channels.ArtifactChannel, DispatcherScope> implements channels.ArtifactChannel {
_type_Artifact = true;
constructor(scope: DispatcherScope, artifact: Artifact) {
super(scope, artifact, 'Artifact', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import { BrowserContext } from '../browserContext';
import type { DispatcherScope } from './dispatcher';
import { Dispatcher, lookupDispatcher } from './dispatcher';
import type { DispatcherScope } from './dispatcher';
import { PageDispatcher, BindingCallDispatcher, WorkerDispatcher } from './pageDispatcher';
import type { FrameDispatcher } from './frameDispatcher';
import type * as channels from '../../protocol/channels';
Expand All @@ -34,15 +34,15 @@ import * as path from 'path';
import { createGuid } from '../../utils';
import { WritableStreamDispatcher } from './writableStreamDispatcher';

export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextChannel> implements channels.BrowserContextChannel {
export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextChannel, DispatcherScope, BrowserContextDispatcher> implements channels.BrowserContextChannel {
_type_EventTarget = true;
_type_BrowserContext = true;
private _context: BrowserContext;

constructor(parentScope: DispatcherScope, context: BrowserContext) {
// We will reparent these to the context below.
const requestContext = APIRequestContextDispatcher.from(parentScope, context.fetchRequest);
const tracing = TracingDispatcher.from(parentScope, context.tracing);
const requestContext = APIRequestContextDispatcher.from(parentScope as BrowserContextDispatcher, context.fetchRequest);
const tracing = TracingDispatcher.from(parentScope as BrowserContextDispatcher, context.tracing);

super(parentScope, context, 'BrowserContext', {
isChromium: context._browser.options.isChromium,
Expand Down Expand Up @@ -70,17 +70,19 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
}

for (const page of context.pages())
this._dispatchEvent('page', { page: new PageDispatcher(this._scope, page) });
this.addObjectListener(BrowserContext.Events.Page, page => this._dispatchEvent('page', { page: new PageDispatcher(this._scope, page) }));
this._dispatchEvent('page', { page: PageDispatcher.from(this._scope, page) });
this.addObjectListener(BrowserContext.Events.Page, page => {
this._dispatchEvent('page', { page: PageDispatcher.from(this._scope, page) });
});
this.addObjectListener(BrowserContext.Events.Close, () => {
this._dispatchEvent('close');
this._dispose();
});

if (context._browser.options.name === 'chromium') {
for (const page of (context as CRBrowserContext).backgroundPages())
this._dispatchEvent('backgroundPage', { page: new PageDispatcher(this._scope, page) });
this.addObjectListener(CRBrowserContext.CREvents.BackgroundPage, page => this._dispatchEvent('backgroundPage', { page: new PageDispatcher(this._scope, page) }));
this._dispatchEvent('backgroundPage', { page: PageDispatcher.from(this._scope, page) });
this.addObjectListener(CRBrowserContext.CREvents.BackgroundPage, page => this._dispatchEvent('backgroundPage', { page: PageDispatcher.from(this._scope, page) }));
for (const serviceWorker of (context as CRBrowserContext).serviceWorkers())
this._dispatchEvent('serviceWorker', { worker: new WorkerDispatcher(this._scope, serviceWorker) });
this.addObjectListener(CRBrowserContext.CREvents.ServiceWorker, serviceWorker => this._dispatchEvent('serviceWorker', { worker: new WorkerDispatcher(this._scope, serviceWorker) }));
Expand Down Expand Up @@ -128,7 +130,8 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel

async exposeBinding(params: channels.BrowserContextExposeBindingParams): Promise<void> {
await this._context.exposeBinding(params.name, !!params.needsHandle, (source, ...args) => {
const binding = new BindingCallDispatcher(this._scope, params.name, !!params.needsHandle, source, args);
const pageDispatcher = PageDispatcher.from(this._scope, source.page);
const binding = new BindingCallDispatcher(pageDispatcher, params.name, !!params.needsHandle, source, args);
this._dispatchEvent('bindingCall', { binding });
return binding.promise();
});
Expand Down Expand Up @@ -184,7 +187,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
return;
}
await this._context.setRequestInterceptor((route, request) => {
this._dispatchEvent('route', { route: RouteDispatcher.from(this._scope, route), request: RequestDispatcher.from(this._scope, request) });
this._dispatchEvent('route', { route: RouteDispatcher.from(RequestDispatcher.from(this._scope, request), route) });
});
}

Expand Down
Loading

0 comments on commit a07a4a2

Please sign in to comment.