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

Commit

Permalink
feat: unify signatures of with and bind (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
rauno56 authored Jun 4, 2021
1 parent ee57789 commit d28c68e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export class ContextAPI {
/**
* Bind a context to a target function or event emitter
*
* @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
* @param target function or event emitter to bind
*/
public bind<T>(target: T, context: Context = this.active()): T {
return this._getContextManager().bind(target, context);
public bind<T>(context: Context, target: T): T {
return this._getContextManager().bind(context, target);
}

private _getContextManager(): ContextManager {
Expand Down
2 changes: 1 addition & 1 deletion src/context/NoopContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class NoopContextManager implements types.ContextManager {
return fn.call(thisArg, ...args);
}

bind<T>(target: T, _context?: types.Context): T {
bind<T>(_context: types.Context, target: T): T {
return target;
}

Expand Down
4 changes: 2 additions & 2 deletions src/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ 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<T>(target: T, context?: Context): T;
bind<T>(context: Context, target: T): T;

/**
* Enable context management
Expand Down
10 changes: 8 additions & 2 deletions test/context/NoopContextManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,19 @@ 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();
});
});
Expand Down

0 comments on commit d28c68e

Please sign in to comment.