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

feat: unify signatures of with and bind #78

Merged
merged 5 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api/context.ts
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
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
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
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