mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: make instrumentation multiplexing proxy-based (#5410)
This commit is contained in:
parent
a06cf70d28
commit
a164f2a810
@ -72,37 +72,17 @@ export interface InstrumentationListener {
|
|||||||
onAfterAction?(result: ActionResult, sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
|
onAfterAction?(result: ActionResult, sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class InstrumentationMultiplexer implements Instrumentation {
|
export function multiplexInstrumentation(listeners: InstrumentationListener[]): Instrumentation {
|
||||||
private _listeners: InstrumentationListener[];
|
return new Proxy({}, {
|
||||||
|
get: (obj: any, prop: string) => {
|
||||||
constructor(listeners: InstrumentationListener[]) {
|
if (!prop.startsWith('on'))
|
||||||
this._listeners = listeners;
|
return obj[prop];
|
||||||
}
|
return async (...params: any[]) => {
|
||||||
|
for (const listener of listeners)
|
||||||
async onContextCreated(context: BrowserContext): Promise<void> {
|
await (listener as any)[prop]?.(...params);
|
||||||
for (const listener of this._listeners)
|
};
|
||||||
await listener.onContextCreated?.(context);
|
},
|
||||||
}
|
});
|
||||||
|
|
||||||
async onContextWillDestroy(context: BrowserContext): Promise<void> {
|
|
||||||
for (const listener of this._listeners)
|
|
||||||
await listener.onContextWillDestroy?.(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
async onContextDidDestroy(context: BrowserContext): Promise<void> {
|
|
||||||
for (const listener of this._listeners)
|
|
||||||
await listener.onContextDidDestroy?.(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
async onActionCheckpoint(name: string, sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
|
|
||||||
for (const listener of this._listeners)
|
|
||||||
await listener.onActionCheckpoint?.(name, sdkObject, metadata);
|
|
||||||
}
|
|
||||||
|
|
||||||
async onAfterAction(result: ActionResult, sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
|
|
||||||
for (const listener of this._listeners)
|
|
||||||
await listener.onAfterAction?.(result, sdkObject, metadata);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function internalCallMetadata(): CallMetadata {
|
export function internalCallMetadata(): CallMetadata {
|
||||||
|
|||||||
@ -27,7 +27,7 @@ import { HarTracer } from './supplements/har/harTracer';
|
|||||||
import { InspectorController } from './supplements/inspectorController';
|
import { InspectorController } from './supplements/inspectorController';
|
||||||
import { WebKit } from './webkit/webkit';
|
import { WebKit } from './webkit/webkit';
|
||||||
import { Registry } from '../utils/registry';
|
import { Registry } from '../utils/registry';
|
||||||
import { InstrumentationMultiplexer, SdkObject } from './instrumentation';
|
import { InstrumentationListener, multiplexInstrumentation, SdkObject } from './instrumentation';
|
||||||
|
|
||||||
export class Playwright extends SdkObject {
|
export class Playwright extends SdkObject {
|
||||||
readonly selectors: Selectors;
|
readonly selectors: Selectors;
|
||||||
@ -39,11 +39,13 @@ export class Playwright extends SdkObject {
|
|||||||
readonly options: PlaywrightOptions;
|
readonly options: PlaywrightOptions;
|
||||||
|
|
||||||
constructor(isInternal: boolean) {
|
constructor(isInternal: boolean) {
|
||||||
const instrumentation = new InstrumentationMultiplexer(isInternal ? [] : [
|
const listeners: InstrumentationListener[] = [];
|
||||||
new InspectorController(),
|
if (!isInternal) {
|
||||||
new Tracer(),
|
listeners.push(new Tracer());
|
||||||
new HarTracer()
|
listeners.push(new HarTracer());
|
||||||
]);
|
listeners.push(new InspectorController());
|
||||||
|
}
|
||||||
|
const instrumentation = multiplexInstrumentation(listeners);
|
||||||
super({ attribution: {}, instrumentation } as any);
|
super({ attribution: {}, instrumentation } as any);
|
||||||
this.options = {
|
this.options = {
|
||||||
isInternal,
|
isInternal,
|
||||||
|
|||||||
@ -42,8 +42,6 @@ export class HarTracer implements InstrumentationListener {
|
|||||||
await contextTracer.flush();
|
await contextTracer.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onContextDidDestroy(context: BrowserContext): Promise<void> { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type HarOptions = {
|
type HarOptions = {
|
||||||
|
|||||||
@ -28,6 +28,4 @@ export class InspectorController implements InstrumentationListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async onContextWillDestroy(context: BrowserContext): Promise<void> {}
|
|
||||||
async onContextDidDestroy(context: BrowserContext): Promise<void> {}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,8 +47,6 @@ export class Tracer implements InstrumentationListener {
|
|||||||
this._contextTracers.set(context, contextTracer);
|
this._contextTracers.set(context, contextTracer);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onContextWillDestroy(context: BrowserContext): Promise<void> {}
|
|
||||||
|
|
||||||
async onContextDidDestroy(context: BrowserContext): Promise<void> {
|
async onContextDidDestroy(context: BrowserContext): Promise<void> {
|
||||||
const contextTracer = this._contextTracers.get(context);
|
const contextTracer = this._contextTracers.get(context);
|
||||||
if (contextTracer) {
|
if (contextTracer) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user