chore: rework log api calls functionality to not replace objects (#518)

This commit is contained in:
Dmitry Gozman 2020-01-16 17:48:38 -08:00 committed by Andrey Lushnikov
parent 82057ac610
commit 734c752fea
6 changed files with 15 additions and 30 deletions

View file

@ -20,7 +20,7 @@ const packageJson = require('./package.json');
for (const className in api) {
if (typeof api[className] === 'function')
helper.installAsyncStackHooks(api[className]);
helper.installApiHooks(className, api[className]);
}
module.exports = {

View file

@ -123,7 +123,6 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
super(context, remoteObject);
this._context = context;
this._page = context.frame._page;
return helper.logPublicApiCalls('handle', this);
}
asElement(): ElementHandle<T> | null {

View file

@ -303,8 +303,6 @@ export class Frame {
if (this._parentFrame)
this._parentFrame._childFrames.add(this);
return helper.logPublicApiCalls('frame', this);
}
async goto(url: string, options?: GotoOptions): Promise<network.Response | null> {

View file

@ -41,12 +41,24 @@ class Helper {
}
}
static installAsyncStackHooks(classType: any) {
static installApiHooks(className: string, classType: any) {
const log = platform.debug('pw:api');
for (const methodName of Reflect.ownKeys(classType.prototype)) {
const method = Reflect.get(classType.prototype, methodName);
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function' || method.constructor.name !== 'AsyncFunction')
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
continue;
const isAsync = method.constructor.name === 'AsyncFunction';
if (!isAsync && !log.enabled)
continue;
Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) {
if (log.enabled) {
if (args.length)
log(`${className}.${methodName} %o`, args);
else
log(`${className}.${methodName}`);
}
if (!isAsync)
return method.call(this, ...args);
const syncStack: any = {};
Error.captureStackTrace(syncStack);
return method.call(this, ...args).catch((e: any) => {
@ -60,26 +72,6 @@ class Helper {
}
}
static logPublicApiCalls(className: string, object: any): any {
const log = platform.debug('pw:api');
if (!log.enabled)
return object;
return new Proxy(object, {
get(target, key: string) {
const value = target[key];
if (typeof key === 'string' && key.startsWith('_') || typeof value !== 'function')
return value;
return function(this: any, ...args: any) {
if (args.length)
log(`${className}.${key} %o`, args);
else
log(`${className}.${key}`);
return value.apply(this, args);
};
}
});
}
static addEventListener(
emitter: platform.EventEmitterType,
eventName: (string | symbol),

View file

@ -16,7 +16,6 @@
import * as types from './types';
import * as dom from './dom';
import { helper } from './helper';
export interface ExecutionContextDelegate {
evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: string | Function, ...args: any[]): Promise<any>;
@ -58,7 +57,6 @@ export class JSHandle<T = any> {
constructor(context: ExecutionContext, remoteObject: any) {
this._context = context;
this._remoteObject = remoteObject;
return helper.logPublicApiCalls('handle', this);
}
evaluate: types.EvaluateOn<T> = (pageFunction, ...args) => {

View file

@ -139,7 +139,6 @@ export class Page extends platform.EventEmitter {
if (delegate.pdf)
this.pdf = delegate.pdf.bind(delegate);
this.coverage = delegate.coverage();
return helper.logPublicApiCalls('page', this);
}
_didClose() {
@ -539,7 +538,6 @@ export class Worker {
this._url = url;
this._executionContextCallback = () => {};
this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
return helper.logPublicApiCalls('worker', this);
}
_createExecutionContext(delegate: js.ExecutionContextDelegate) {