From 59afb753a7506ca90f2d22664a12c5615cd56799 Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Fri, 21 May 2021 11:44:04 +0300 Subject: [PATCH 1/5] feat: unify signatures of `with` and `bind` --- src/api/context.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/api/context.ts b/src/api/context.ts index 34a1b806..292f50ea 100644 --- a/src/api/context.ts +++ b/src/api/context.ts @@ -78,6 +78,7 @@ export class ContextAPI { /** * Bind a context to a target function or event emitter + * @deprecated in 0.x, will be removed in 1.x * * @param target function or event emitter to bind * @param context context to bind to the event emitter or function. Defaults to the currently active context @@ -86,6 +87,16 @@ export class ContextAPI { return this._getContextManager().bind(target, context); } + /** + * Bind a context to a target function or event emitter + * + * @param context context to bind to the event emitter or function. Defaults to the currently active context + * @param target function or event emitter to bind + */ + public bind(context: Context = this.active(), target: T): T { + return this._getContextManager().bind(target, context); + } + private _getContextManager(): ContextManager { return getGlobal(API_NAME) || NOOP_CONTEXT_MANAGER; } From 4e2d43459bb68254c87370020c15b4f543bf6bb3 Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Fri, 21 May 2021 22:52:34 +0300 Subject: [PATCH 2/5] feat: remove deprecated bind, short circuit if context === undefined --- src/api/context.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/api/context.ts b/src/api/context.ts index 292f50ea..e42c807a 100644 --- a/src/api/context.ts +++ b/src/api/context.ts @@ -76,24 +76,16 @@ export class ContextAPI { return this._getContextManager().with(context, fn, thisArg, ...args); } - /** - * Bind a context to a target function or event emitter - * @deprecated in 0.x, will be removed in 1.x - * - * @param target function or event emitter to bind - * @param context context to bind to the event emitter or function. Defaults to the currently active context - */ - public bind(target: T, context: Context = this.active()): T { - return this._getContextManager().bind(target, context); - } - /** * Bind a context to a target function or event emitter * * @param context context to bind to the event emitter or function. Defaults to the currently active context * @param target function or event emitter to bind */ - public bind(context: Context = this.active(), target: T): T { + public bind(context: Context, target: T): T { + if (context === undefined) { + return target; + } return this._getContextManager().bind(target, context); } From c31b6c26fd07c5d81c5fbec34c6e1b07f178000a Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Wed, 2 Jun 2021 12:23:21 +0300 Subject: [PATCH 3/5] feat: update tests and NoopContextManager Leave checking special cases for the actual ContextManager and remove the short-circuit from the API implementation. --- src/api/context.ts | 5 +---- src/context/NoopContextManager.ts | 2 +- src/context/types.ts | 2 +- test/context/NoopContextManager.test.ts | 4 ++-- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/api/context.ts b/src/api/context.ts index e42c807a..19220f25 100644 --- a/src/api/context.ts +++ b/src/api/context.ts @@ -83,10 +83,7 @@ export class ContextAPI { * @param target function or event emitter to bind */ public bind(context: Context, target: T): T { - if (context === undefined) { - return target; - } - return this._getContextManager().bind(target, context); + return this._getContextManager().bind(context, target); } private _getContextManager(): ContextManager { diff --git a/src/context/NoopContextManager.ts b/src/context/NoopContextManager.ts index 54589a06..a62a89d3 100644 --- a/src/context/NoopContextManager.ts +++ b/src/context/NoopContextManager.ts @@ -31,7 +31,7 @@ export class NoopContextManager implements types.ContextManager { return fn.call(thisArg, ...args); } - bind(target: T, _context?: types.Context): T { + bind(_context: types.Context, target: T): T { return target; } diff --git a/src/context/types.ts b/src/context/types.ts index af7a1ee1..622b1918 100644 --- a/src/context/types.ts +++ b/src/context/types.ts @@ -65,7 +65,7 @@ export interface ContextManager { * @param target Any object to which a context need to be set * @param [context] Optionally specify the context which you want to assign */ - bind(target: T, context?: Context): T; + bind(context: Context, target: T): T; /** * Enable context management diff --git a/test/context/NoopContextManager.test.ts b/test/context/NoopContextManager.test.ts index b5da8e6d..91428286 100644 --- a/test/context/NoopContextManager.test.ts +++ b/test/context/NoopContextManager.test.ts @@ -119,13 +119,13 @@ describe('NoopContextManager', () => { describe('.bind()', () => { it('should return the same target (when enabled)', () => { const test = { a: 1 }; - assert.deepStrictEqual(contextManager.bind(test), test); + assert.deepStrictEqual(contextManager.bind(contextManager.active(), test), test); }); it('should return the same target (when disabled)', () => { contextManager.disable(); const test = { a: 1 }; - assert.deepStrictEqual(contextManager.bind(test), test); + assert.deepStrictEqual(contextManager.bind(contextManager.active(), test), test); contextManager.enable(); }); }); From 7d3ae558ed7dc1bf138a32261f8609c08f74ea1a Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Wed, 2 Jun 2021 13:10:41 +0300 Subject: [PATCH 4/5] style: lint --- test/context/NoopContextManager.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/context/NoopContextManager.test.ts b/test/context/NoopContextManager.test.ts index 91428286..8a8a732b 100644 --- a/test/context/NoopContextManager.test.ts +++ b/test/context/NoopContextManager.test.ts @@ -119,13 +119,19 @@ describe('NoopContextManager', () => { describe('.bind()', () => { it('should return the same target (when enabled)', () => { const test = { a: 1 }; - assert.deepStrictEqual(contextManager.bind(contextManager.active(), test), test); + assert.deepStrictEqual( + contextManager.bind(contextManager.active(), test), + test + ); }); it('should return the same target (when disabled)', () => { contextManager.disable(); const test = { a: 1 }; - assert.deepStrictEqual(contextManager.bind(contextManager.active(), test), test); + assert.deepStrictEqual( + contextManager.bind(contextManager.active(), test), + test + ); contextManager.enable(); }); }); From 3a74ec3112fad225267a15c3cb0941934716f796 Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Wed, 2 Jun 2021 18:48:32 +0300 Subject: [PATCH 5/5] docs: update js docs according to the new signature --- src/context/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/context/types.ts b/src/context/types.ts index 622b1918..19ae6d9d 100644 --- a/src/context/types.ts +++ b/src/context/types.ts @@ -62,8 +62,8 @@ export interface ContextManager { /** * Bind an object as the current context (or a specific one) - * @param target Any object to which a context need to be set * @param [context] Optionally specify the context which you want to assign + * @param target Any object to which a context need to be set */ bind(context: Context, target: T): T;