From 1998542d35a7e0d7f162a4c999ca4c223fa26420 Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Wed, 30 Jun 2021 21:50:04 +0300 Subject: [PATCH] feat: unify the signatures of bind and with (#2247) Co-authored-by: Daniel Dyla --- .../src/AbstractAsyncHooksContextManager.ts | 12 ++++++------ .../test/AsyncHooksContextManager.test.ts | 3 ++- .../src/ZoneContextManager.ts | 12 ++++++------ .../opentelemetry-web/src/StackContextManager.ts | 8 ++++---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/opentelemetry-context-async-hooks/src/AbstractAsyncHooksContextManager.ts b/packages/opentelemetry-context-async-hooks/src/AbstractAsyncHooksContextManager.ts index 0e9600b2c9..4eb200c0aa 100644 --- a/packages/opentelemetry-context-async-hooks/src/AbstractAsyncHooksContextManager.ts +++ b/packages/opentelemetry-context-async-hooks/src/AbstractAsyncHooksContextManager.ts @@ -59,16 +59,16 @@ export abstract class AbstractAsyncHooksContextManager */ bind(context: Context, target: T): T { if (target instanceof EventEmitter) { - return this._bindEventEmitter(target, context); + return this._bindEventEmitter(context, target); } if (typeof target === 'function') { - return this._bindFunction(target, context); + return this._bindFunction(context, target); } return target; } - private _bindFunction(target: T, context: Context): T { + private _bindFunction(context: Context, target: T): T { const manager = this; const contextWrapper = function (this: never, ...args: unknown[]) { return manager.with(context, () => target.apply(this, args)); @@ -91,12 +91,12 @@ export abstract class AbstractAsyncHooksContextManager * By default, EventEmitter call their callback with their context, which we do * not want, instead we will bind a specific context to all callbacks that * go through it. - * @param ee EventEmitter an instance of EventEmitter to patch * @param context the context we want to bind + * @param ee EventEmitter an instance of EventEmitter to patch */ private _bindEventEmitter( - ee: T, - context: Context + context: Context, + ee: T ): T { const map = this._getPatchMap(ee); if (map !== undefined) return ee; diff --git a/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts b/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts index 762096c800..0b8e486891 100644 --- a/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts +++ b/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts @@ -367,7 +367,8 @@ for (const contextManagerClass of [ contextManager.bind(context, () => { assert.strictEqual(contextManager.active(), context); assert.strictEqual(otherContextManager.active(), otherContext); - })); + }) + ); fn(); }); }); diff --git a/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts b/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts index 4ff0a9b30b..9f56f46657 100644 --- a/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts +++ b/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts @@ -50,10 +50,10 @@ export class ZoneContextManager implements ContextManager { } /** - * @param target Function to be executed within the context * @param context A context (span) to be executed within target function + * @param target Function to be executed within the context */ - private _bindFunction(target: T, context: Context): T { + private _bindFunction(context: Context, target: T): T { const manager = this; const contextWrapper = function (this: any, ...args: unknown[]) { return manager.with(context, () => target.apply(this, args)); @@ -68,10 +68,10 @@ export class ZoneContextManager implements ContextManager { } /** - * @param obj target object on which the listeners will be patched * @param context A context (span) to be bind to target + * @param obj target object on which the listeners will be patched */ - private _bindListener(obj: T, context: Context): T { + private _bindListener(context: Context, obj: T): T { const target = (obj as unknown) as TargetWithEvents; if (target.__ot_listeners !== undefined) { return obj; @@ -212,9 +212,9 @@ export class ZoneContextManager implements ContextManager { context = this.active(); } if (typeof target === 'function') { - return this._bindFunction(target, context); + return this._bindFunction(context, target); } else if (isListenerObject(target)) { - this._bindListener(target, context); + this._bindListener(context, target); } return (target as unknown) as T; } diff --git a/packages/opentelemetry-web/src/StackContextManager.ts b/packages/opentelemetry-web/src/StackContextManager.ts index c293be8833..fb72c947ea 100644 --- a/packages/opentelemetry-web/src/StackContextManager.ts +++ b/packages/opentelemetry-web/src/StackContextManager.ts @@ -33,12 +33,12 @@ export class StackContextManager implements ContextManager { /** * - * @param target Function to be executed within the context * @param context + * @param target Function to be executed within the context */ private _bindFunction( - target: T, - context = ROOT_CONTEXT + context = ROOT_CONTEXT, + target: T ): T { const manager = this; const contextWrapper = function (this: unknown, ...args: unknown[]) { @@ -72,7 +72,7 @@ export class StackContextManager implements ContextManager { context = this.active(); } if (typeof target === 'function') { - return this._bindFunction(target, context); + return this._bindFunction(context, target); } return target; }