Skip to content

Commit df38422

Browse files
committed
chore: move slowmo and logging to dispatcher from server core
For better code isolation.
1 parent d99f6e9 commit df38422

File tree

22 files changed

+53
-83
lines changed

22 files changed

+53
-83
lines changed

packages/playwright-core/src/androidServerImpl.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { createPlaywright } from './server/playwright';
1919
import { createGuid } from './server/utils/crypto';
2020
import { ws } from './utilsBundle';
2121
import { ProgressController } from './server/progress';
22-
import { serverSideCallMetadata } from './server';
2322

2423
import type { BrowserServer } from './client/browserType';
2524
import type { LaunchAndroidServerOptions } from './client/types';
@@ -29,7 +28,7 @@ export class AndroidServerLauncherImpl {
2928
async launchServer(options: LaunchAndroidServerOptions = {}): Promise<BrowserServer> {
3029
const playwright = createPlaywright({ sdkLanguage: 'javascript', isServer: true });
3130
// 1. Pre-connect to the device
32-
const controller = new ProgressController(serverSideCallMetadata(), playwright);
31+
const controller = new ProgressController();
3332
let devices = await controller.run(progress => playwright.android.devices(progress, {
3433
host: options.adbHost,
3534
port: options.adbPort,

packages/playwright-core/src/browserServerImpl.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import { PlaywrightServer } from './remote/playwrightServer';
1818
import { helper } from './server/helper';
19-
import { serverSideCallMetadata } from './server/instrumentation';
2019
import { createPlaywright } from './server/playwright';
2120
import { createGuid } from './server/utils/crypto';
2221
import { isUnderTest } from './server/utils/debug';
@@ -42,7 +41,7 @@ export class BrowserServerLauncherImpl implements BrowserServerLauncher {
4241
async launchServer(options: LaunchServerOptions & { _sharedBrowser?: boolean, _userDataDir?: string } = {}): Promise<BrowserServer> {
4342
const playwright = createPlaywright({ sdkLanguage: 'javascript', isServer: true });
4443
// 1. Pre-launch the browser
45-
const metadata = serverSideCallMetadata();
44+
const metadata = { id: '', startTime: 0, endTime: 0, type: 'Internal', method: '', params: {}, log: [], internal: true };
4645
const validatorContext = {
4746
tChannelImpl: (names: '*' | string[], arg: any, path: string) => {
4847
throw new validatorPrimitives.ValidationError(`${path}: channels are not expected in launchServer`);
@@ -60,7 +59,7 @@ export class BrowserServerLauncherImpl implements BrowserServerLauncher {
6059

6160
let browser: Browser;
6261
try {
63-
const controller = new ProgressController(metadata, playwright[this._browserName]);
62+
const controller = new ProgressController(metadata);
6463
browser = await controller.run(async progress => {
6564
if (options._userDataDir !== undefined) {
6665
const validator = validatorPrimitives.scheme['BrowserTypeLaunchPersistentContextParams'];

packages/playwright-core/src/client/channelOwner.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
207207
}
208208
}
209209

210-
_toImpl(): any {
211-
return this._connection.toImpl?.(this);
212-
}
213-
214210
private toJSON() {
215211
// Jest's expect library tries to print objects sometimes.
216212
// RPC objects can contain links to lots of other objects,

packages/playwright-core/src/client/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class Connection extends EventEmitter {
7474
private _localUtils?: LocalUtils;
7575
private _rawBuffers = false;
7676
// Some connections allow resolving in-process dispatchers.
77-
toImpl: ((client: ChannelOwner) => any) | undefined;
77+
toImpl: ((client: ChannelOwner | Connection) => any) | undefined;
7878
private _tracingCount = 0;
7979
readonly _instrumentation: ClientInstrumentation;
8080
// Used from @playwright/test fixtures -> TODO remove?

packages/playwright-core/src/client/electron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
9999
}
100100

101101
process(): childProcess.ChildProcess {
102-
return this._toImpl().process();
102+
return this._connection.toImpl?.(this)?.process();
103103
}
104104

105105
_onPage(page: Page) {

packages/playwright-core/src/inProcessFactory.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ export function createInProcessPlaywright(): PlaywrightAPI {
4949
dispatcherConnection.onmessage = message => setImmediate(() => clientConnection.dispatch(message));
5050
clientConnection.onmessage = message => setImmediate(() => dispatcherConnection.dispatch(message));
5151

52-
clientConnection.toImpl = (x: any) => x ? dispatcherConnection._dispatcherByGuid.get(x._guid)!._object : dispatcherConnection._dispatcherByGuid.get('');
53-
(playwrightAPI as any)._toImpl = clientConnection.toImpl;
52+
clientConnection.toImpl = x => {
53+
if (x instanceof Connection)
54+
return x === clientConnection ? dispatcherConnection : undefined;
55+
if (!x)
56+
return dispatcherConnection._dispatcherByGuid.get('');
57+
return dispatcherConnection._dispatcherByGuid.get(x._guid)!._object;
58+
};
5459
return playwrightAPI;
5560
}

packages/playwright-core/src/remote/playwrightServer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { WSServer } from '../server/utils/wsServer';
2222
import { wrapInASCIIBox } from '../server/utils/ascii';
2323
import { getPlaywrightVersion } from '../server/utils/userAgent';
2424
import { debugLogger, isUnderTest } from '../utils';
25-
import { serverSideCallMetadata } from '../server';
2625
import { SocksProxy } from '../server/utils/socksProxy';
2726
import { Browser } from '../server/browser';
2827
import { ProgressController } from '../server/progress';
@@ -204,7 +203,7 @@ export class PlaywrightServer {
204203

205204
if (!browser) {
206205
const browserType = this._playwright[(browserName || 'chromium') as 'chromium'];
207-
const controller = new ProgressController(serverSideCallMetadata(), browserType);
206+
const controller = new ProgressController();
208207
browser = await controller.run(progress => browserType.launch(progress, {
209208
...launchOptions,
210209
headless: !!process.env.PW_DEBUG_CONTROLLER_HEADLESS,
@@ -234,7 +233,7 @@ export class PlaywrightServer {
234233
let browser = this._playwright.allBrowsers().find(b => b.options.name === browserName);
235234
if (!browser) {
236235
const browserType = this._playwright[browserName as 'chromium'];
237-
const controller = new ProgressController(serverSideCallMetadata(), browserType);
236+
const controller = new ProgressController();
238237
browser = await controller.run(progress => browserType.launch(progress, launchOptions), launchOptions.timeout);
239238
this._dontReuse(browser);
240239
}
@@ -286,7 +285,7 @@ export class PlaywrightServer {
286285
launchOptions.socksProxyPort = undefined;
287286
}
288287
const browserType = this._playwright[browserName as 'chromium'];
289-
const controller = new ProgressController(serverSideCallMetadata(), browserType);
288+
const controller = new ProgressController();
290289
const browser = await controller.run(progress => browserType.launch(progress, launchOptions), launchOptions.timeout);
291290
this._dontReuseBrowsers.add(browser);
292291
return {

packages/playwright-core/src/server/android/android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { chromiumSwitches } from '../chromium/chromiumSwitches';
3232
import { CRBrowser } from '../chromium/crBrowser';
3333
import { removeFolders } from '../utils/fileUtils';
3434
import { helper } from '../helper';
35-
import { SdkObject, serverSideCallMetadata } from '../instrumentation';
35+
import { SdkObject } from '../instrumentation';
3636
import { gracefullyCloseSet } from '../utils/processLauncher';
3737
import { isAbortError, Progress, ProgressController, raceUncancellableOperationWithCleanup } from '../progress';
3838
import { registry } from '../registry';
@@ -162,7 +162,7 @@ export class AndroidDevice extends SdkObject {
162162
if (this._isClosed)
163163
return;
164164
if (!this._driverPromise) {
165-
const controller = new ProgressController(serverSideCallMetadata(), this);
165+
const controller = new ProgressController();
166166
this._driverPromise = controller.run(progress => this._installDriver(progress));
167167
}
168168
return this._driverPromise;

packages/playwright-core/src/server/debugger.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export class Debugger extends EventEmitter implements InstrumentationListener {
3434
PausedStateChanged: 'pausedstatechanged'
3535
};
3636
private _muted = false;
37-
private _slowMo: number | undefined;
3837

3938
constructor(context: BrowserContext) {
4039
super();
@@ -47,7 +46,6 @@ export class Debugger extends EventEmitter implements InstrumentationListener {
4746
this._context.once(BrowserContext.Events.Close, () => {
4847
this._context.instrumentation.removeListener(this);
4948
});
50-
this._slowMo = this._context._browser.options.slowMo;
5149
}
5250

5351
async setMuted(muted: boolean) {
@@ -61,15 +59,6 @@ export class Debugger extends EventEmitter implements InstrumentationListener {
6159
await this.pause(sdkObject, metadata);
6260
}
6361

64-
async _doSlowMo() {
65-
await new Promise(f => setTimeout(f, this._slowMo));
66-
}
67-
68-
async onAfterCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
69-
if (this._slowMo && shouldSlowMo(metadata))
70-
await this._doSlowMo();
71-
}
72-
7362
async onBeforeInputAction(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
7463
if (this._muted)
7564
return;
@@ -135,8 +124,3 @@ function shouldPauseBeforeStep(metadata: CallMetadata): boolean {
135124
const metainfo = methodMetainfo.get(metadata.type + '.' + metadata.method);
136125
return !!metainfo?.pausesBeforeAction;
137126
}
138-
139-
function shouldSlowMo(metadata: CallMetadata): boolean {
140-
const metainfo = methodMetainfo.get(metadata.type + '.' + metadata.method);
141-
return !!metainfo?.slowMo;
142-
}

packages/playwright-core/src/server/dispatchers/dispatcher.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { EventEmitter } from 'events';
1818

1919
import { eventsHelper } from '../utils/eventsHelper';
2020
import { ValidationError, createMetadataValidator, findValidator } from '../../protocol/validator';
21-
import { assert, monotonicTime, rewriteErrorMessage } from '../../utils';
21+
import { assert, monotonicTime, rewriteErrorMessage, debugLogger } from '../../utils';
2222
import { isUnderTest } from '../utils/debug';
2323
import { TargetClosedError, isTargetClosedError, serializeError } from '../errors';
2424
import { createRootSdkObject, SdkObject } from '../instrumentation';
@@ -101,7 +101,11 @@ export class Dispatcher<Type extends SdkObject, ChannelType, ParentScopeType ext
101101
}
102102

103103
async _runCommand(callMetadata: CallMetadata, method: string, validParams: any) {
104-
const controller = new ProgressController(callMetadata, this._object);
104+
const controller = new ProgressController(callMetadata, message => {
105+
const logName = this._object.logName || 'api';
106+
debugLogger.log(logName, message);
107+
this._object.instrumentation.onCallLog(this._object, callMetadata, logName, message);
108+
});
105109
this._activeProgressControllers.add(controller);
106110
try {
107111
return await controller.run(progress => (this as any)[method](validParams, progress), validParams?.timeout);
@@ -318,7 +322,8 @@ export class DispatcherConnection {
318322
return;
319323
}
320324

321-
if (methodMetainfo.get(dispatcher._type + '.' + method)?.internal) {
325+
const metainfo = methodMetainfo.get(dispatcher._type + '.' + method);
326+
if (metainfo?.internal) {
322327
// For non-js ports, it is easier to detect internal calls here rather
323328
// than generate protocol metainfo for each language.
324329
validMetadata.internal = true;
@@ -396,12 +401,20 @@ export class DispatcherConnection {
396401
} finally {
397402
callMetadata.endTime = monotonicTime();
398403
await sdkObject.instrumentation.onAfterCall(sdkObject, callMetadata);
404+
if (metainfo?.slowMo)
405+
await this._doSlowMo(sdkObject);
399406
}
400407

401408
if (response.error)
402409
response.log = compressCallLog(callMetadata.log);
403410
this.onmessage(response);
404411
}
412+
413+
private async _doSlowMo(sdkObject: SdkObject): Promise<void> {
414+
const slowMo = sdkObject.attribution.browser?.options.slowMo;
415+
if (slowMo)
416+
await new Promise(f => setTimeout(f, slowMo));
417+
}
405418
}
406419

407420
function closeReason(sdkObject: SdkObject): string | undefined {

0 commit comments

Comments
 (0)