Skip to content

Commit 7eefb89

Browse files
committed
chore: refactor protocol metainfo for debugger pause and snapshot phases
Replace pausesBeforeAction/pausesBeforeInput with pause/isAutoWaiting/input fields. Make snapshot capture phase-aware (before/after/input). Move getMetainfo to protocolMetainfo.ts.
1 parent 837d8eb commit 7eefb89

File tree

10 files changed

+287
-238
lines changed

10 files changed

+287
-238
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { EventEmitter } from './eventEmitter';
1818
import { ValidationError, maybeFindValidator } from '../protocol/validator';
19-
import { getMetainfo } from '../utils/isomorphic/protocolFormatter';
19+
import { getMetainfo } from '../utils/isomorphic/protocolMetainfo';
2020
import { captureLibraryStackTrace } from './clientStackTrace';
2121
import { stringifyStackFrames } from '../utils/isomorphic/stackTrace';
2222

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { SdkObject } from './instrumentation';
1818
import { monotonicTime } from '../utils';
1919
import { BrowserContext } from './browserContext';
20-
import { getMetainfo } from '../utils/isomorphic/protocolFormatter';
20+
import { getMetainfo } from '../utils/isomorphic/protocolMetainfo';
2121

2222
import type { CallMetadata, InstrumentationListener } from './instrumentation';
2323

@@ -29,7 +29,7 @@ export class Debugger extends SdkObject implements InstrumentationListener {
2929
private _pauseAt: PauseAt = {};
3030
private _pausedCallsMetadata = new Map<CallMetadata, { resolve: () => void, sdkObject: SdkObject }>();
3131
private _enabled = false;
32-
private _pauseBeforeInputActions = false; // instead of inside input actions
32+
private _pauseBeforeWaitingActions = false; // instead of inside input actions
3333
private _context: BrowserContext;
3434

3535
static Events = {
@@ -52,24 +52,27 @@ export class Debugger extends SdkObject implements InstrumentationListener {
5252
}
5353

5454
async onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
55-
if (this._muted)
55+
if (this._muted || metadata.internal)
5656
return;
57+
const metainfo = getMetainfo(metadata);
5758
const pauseOnPauseCall = this._enabled && metadata.type === 'BrowserContext' && metadata.method === 'pause';
58-
const pauseOnNextStep = !!this._pauseAt.next && shouldPauseBeforeStep(metadata, this._pauseBeforeInputActions);
59+
const pauseBeforeAction = !!this._pauseAt.next && !!metainfo?.pause && (this._pauseBeforeWaitingActions || !metainfo?.isAutoWaiting);
5960
const pauseOnLocation = !!this._pauseAt.location && matchesLocation(metadata, this._pauseAt.location);
60-
if (pauseOnPauseCall || pauseOnNextStep || pauseOnLocation)
61+
if (pauseOnPauseCall || pauseBeforeAction || pauseOnLocation)
6162
await this._pause(sdkObject, metadata);
6263
}
6364

6465
async onBeforeInputAction(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
65-
if (this._muted)
66+
if (this._muted || metadata.internal)
6667
return;
67-
if (!!this._pauseAt.next && !this._pauseBeforeInputActions)
68+
const metainfo = getMetainfo(metadata);
69+
const pauseBeforeInput = !!this._pauseAt.next && !!metainfo?.pause && !!metainfo?.isAutoWaiting && this._pauseBeforeWaitingActions;
70+
if (pauseBeforeInput)
6871
await this._pause(sdkObject, metadata);
6972
}
7073

7174
private async _pause(sdkObject: SdkObject, metadata: CallMetadata) {
72-
if (this._muted)
75+
if (this._muted || metadata.internal)
7376
return;
7477
this._pauseAt = {};
7578
metadata.pauseStartTime = monotonicTime();
@@ -93,8 +96,8 @@ export class Debugger extends SdkObject implements InstrumentationListener {
9396
this.emit(Debugger.Events.PausedStateChanged);
9497
}
9598

96-
setPauseBeforeInputActions() {
97-
this._pauseBeforeInputActions = true;
99+
setPauseBeforeWaitingActions() {
100+
this._pauseBeforeWaitingActions = true;
98101
}
99102

100103
setPauseAt(at: { next?: boolean, location?: { file: string, line?: number, column?: number } } = {}) {
@@ -121,10 +124,3 @@ function matchesLocation(metadata: CallMetadata, location: { file: string, line?
121124
(location.line === undefined || metadata.location.line === location.line) &&
122125
(location.column === undefined || metadata.location.column === location.column);
123126
}
124-
125-
function shouldPauseBeforeStep(metadata: CallMetadata, includeInputActions: boolean): boolean {
126-
if (metadata.internal)
127-
return false;
128-
const metainfo = getMetainfo(metadata);
129-
return !!metainfo?.pausesBeforeAction || (includeInputActions && !!metainfo?.pausesBeforeInput);
130-
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class DebuggerDispatcher extends Dispatcher<Debugger, channels.DebuggerCh
5353
async pause(params: channels.DebuggerPauseParams, progress: Progress): Promise<void> {
5454
if (this._object.isPaused())
5555
throw new Error('Debugger is already paused');
56-
this._object.setPauseBeforeInputActions();
56+
this._object.setPauseBeforeWaitingActions();
5757
this._object.setPauseAt({ next: true });
5858
}
5959

@@ -66,15 +66,15 @@ export class DebuggerDispatcher extends Dispatcher<Debugger, channels.DebuggerCh
6666
async next(params: channels.DebuggerNextParams, progress: Progress): Promise<void> {
6767
if (!this._object.isPaused())
6868
throw new Error('Debugger is not paused');
69-
this._object.setPauseBeforeInputActions();
69+
this._object.setPauseBeforeWaitingActions();
7070
this._object.setPauseAt({ next: true });
7171
this._object.resume();
7272
}
7373

7474
async runTo(params: channels.DebuggerRunToParams, progress: Progress): Promise<void> {
7575
if (!this._object.isPaused())
7676
throw new Error('Debugger is not paused');
77-
this._object.setPauseBeforeInputActions();
77+
this._object.setPauseBeforeWaitingActions();
7878
this._object.setPauseAt({ location: params.location });
7979
this._object.resume();
8080
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { TargetClosedError, isTargetClosedError, serializeError } from '../error
2424
import { createRootSdkObject, SdkObject } from '../instrumentation';
2525
import { isProtocolError } from '../protocolError';
2626
import { compressCallLog } from '../callLog';
27-
import { getMetainfo } from '../../utils/isomorphic/protocolFormatter';
27+
import { getMetainfo } from '../../utils/isomorphic/protocolMetainfo';
2828
import { Progress, ProgressController } from '../progress';
2929

3030
import type { CallMetadata } from '../instrumentation';

packages/playwright-core/src/server/trace/recorder/tracing.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import os from 'os';
1919
import path from 'path';
2020

2121
import { Snapshotter } from './snapshotter';
22-
import { getMetainfo } from '../../../utils/isomorphic/protocolFormatter';
22+
import { getMetainfo } from '../../../utils/isomorphic/protocolMetainfo';
2323
import { assert } from '../../../utils/isomorphic/assert';
2424
import { monotonicTime } from '../../../utils/isomorphic/time';
2525
import { eventsHelper } from '../../utils/eventsHelper';
@@ -435,8 +435,19 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
435435
await this._snapshotter?.captureSnapshot(sdkObject.attribution.page, metadata.id, snapshotName).catch(() => {});
436436
}
437437

438-
private _shouldCaptureSnapshot(sdkObject: SdkObject, metadata: CallMetadata) {
439-
return !!this._snapshotter?.started() && shouldCaptureSnapshot(metadata) && !!sdkObject.attribution.page;
438+
private _shouldCaptureSnapshot(sdkObject: SdkObject, metadata: CallMetadata, phase: 'before' | 'after' | 'input') {
439+
if (!sdkObject.attribution.page || !this._snapshotter?.started())
440+
return;
441+
442+
const metainfo = getMetainfo(metadata);
443+
if (!metainfo?.snapshot)
444+
return false;
445+
446+
switch (phase) {
447+
case 'before': return !metainfo.input || !!metainfo.isAutoWaiting;
448+
case 'input': return !!metainfo.input;
449+
case 'after': return true;
450+
}
440451
}
441452

442453
onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata, parentId?: string) {
@@ -445,7 +456,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
445456
if (!event)
446457
return Promise.resolve();
447458
sdkObject.attribution.page?.screencast.temporarilyDisableThrottling();
448-
if (this._shouldCaptureSnapshot(sdkObject, metadata))
459+
if (this._shouldCaptureSnapshot(sdkObject, metadata, 'before'))
449460
event.beforeSnapshot = `before@${metadata.id}`;
450461
this._state?.callIds.add(metadata.id);
451462
this._appendTraceEvent(event);
@@ -460,7 +471,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
460471
if (!event)
461472
return Promise.resolve();
462473
sdkObject.attribution.page?.screencast.temporarilyDisableThrottling();
463-
if (this._shouldCaptureSnapshot(sdkObject, metadata))
474+
if (this._shouldCaptureSnapshot(sdkObject, metadata, 'input'))
464475
event.inputSnapshot = `input@${metadata.id}`;
465476
this._appendTraceEvent(event);
466477
return this._captureSnapshot(event.inputSnapshot, sdkObject, metadata);
@@ -487,7 +498,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
487498
if (!event)
488499
return Promise.resolve();
489500
sdkObject.attribution.page?.screencast.temporarilyDisableThrottling();
490-
if (this._shouldCaptureSnapshot(sdkObject, metadata))
501+
if (this._shouldCaptureSnapshot(sdkObject, metadata, 'after'))
491502
event.afterSnapshot = `after@${metadata.id}`;
492503
this._appendTraceEvent(event);
493504
return this._captureSnapshot(event.afterSnapshot, sdkObject, metadata);
@@ -659,11 +670,6 @@ function visitTraceEvent(object: any, sha1s: Set<string>): any {
659670
return object;
660671
}
661672

662-
function shouldCaptureSnapshot(metadata: CallMetadata): boolean {
663-
const metainfo = getMetainfo(metadata);
664-
return !!metainfo?.snapshot;
665-
}
666-
667673
function createBeforeActionTraceEvent(metadata: CallMetadata, parentId?: string): trace.BeforeActionTraceEvent | null {
668674
if (metadata.internal || metadata.method.startsWith('tracing'))
669675
return null;

packages/playwright-core/src/utils/isomorphic/protocolFormatter.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { methodMetainfo } from './protocolMetainfo';
18-
import type { MethodMetainfo } from './protocolMetainfo';
17+
import { getMetainfo } from './protocolMetainfo';
1918

2019
export function formatProtocolParam(params: Record<string, string> | undefined, alternatives: string): string | undefined {
2120
return _formatProtocolParam(params, alternatives)?.replaceAll('\n', '\\n');
@@ -70,10 +69,6 @@ export function renderTitleForCall(metadata: { title?: string, type: string, met
7069
});
7170
}
7271

73-
export function getMetainfo(metadata: { type: string, method: string }): MethodMetainfo | undefined {
74-
return methodMetainfo.get(metadata.type + '.' + metadata.method);
75-
}
76-
7772
export type ActionGroup = 'configuration' | 'route' | 'getter';
7873

7974
export function getActionGroup(metadata: { type: string, method: string }) {

0 commit comments

Comments
 (0)