Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit d28c68e

Browse files
authored
feat: unify signatures of with and bind (#78)
1 parent ee57789 commit d28c68e

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/api/context.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ export class ContextAPI {
7979
/**
8080
* Bind a context to a target function or event emitter
8181
*
82-
* @param target function or event emitter to bind
8382
* @param context context to bind to the event emitter or function. Defaults to the currently active context
83+
* @param target function or event emitter to bind
8484
*/
85-
public bind<T>(target: T, context: Context = this.active()): T {
86-
return this._getContextManager().bind(target, context);
85+
public bind<T>(context: Context, target: T): T {
86+
return this._getContextManager().bind(context, target);
8787
}
8888

8989
private _getContextManager(): ContextManager {

src/context/NoopContextManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class NoopContextManager implements types.ContextManager {
3131
return fn.call(thisArg, ...args);
3232
}
3333

34-
bind<T>(target: T, _context?: types.Context): T {
34+
bind<T>(_context: types.Context, target: T): T {
3535
return target;
3636
}
3737

src/context/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export interface ContextManager {
6262

6363
/**
6464
* Bind an object as the current context (or a specific one)
65-
* @param target Any object to which a context need to be set
6665
* @param [context] Optionally specify the context which you want to assign
66+
* @param target Any object to which a context need to be set
6767
*/
68-
bind<T>(target: T, context?: Context): T;
68+
bind<T>(context: Context, target: T): T;
6969

7070
/**
7171
* Enable context management

test/context/NoopContextManager.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,19 @@ describe('NoopContextManager', () => {
119119
describe('.bind()', () => {
120120
it('should return the same target (when enabled)', () => {
121121
const test = { a: 1 };
122-
assert.deepStrictEqual(contextManager.bind(test), test);
122+
assert.deepStrictEqual(
123+
contextManager.bind(contextManager.active(), test),
124+
test
125+
);
123126
});
124127

125128
it('should return the same target (when disabled)', () => {
126129
contextManager.disable();
127130
const test = { a: 1 };
128-
assert.deepStrictEqual(contextManager.bind(test), test);
131+
assert.deepStrictEqual(
132+
contextManager.bind(contextManager.active(), test),
133+
test
134+
);
129135
contextManager.enable();
130136
});
131137
});

0 commit comments

Comments
 (0)