Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindMethods() method and a mixin() decorator (TS) #37

Merged
merged 9 commits into from Jun 15, 2019

Conversation

stroncium
Copy link
Contributor

@stroncium stroncium commented Mar 6, 2019

Fixes #30

@stroncium
Copy link
Contributor Author

/**
 * Bind a `methodNames` or all `Emittery` methods if `methodNames` not defined into `target` object.
 */
bindMethods(target: Object, methodNames?: string[]): void;

/**
 * Returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames` or all `Emittery` methods if `methodNames` not defined into target class.
 */
static mixin(emitteryPropertyName: string, methodNames?: string[]): Function;

Solves #30

@sindresorhus sindresorhus changed the title added bindMethods and mixin, solves #30 Add bindMethods() method and a mixin() decorator Jun 12, 2019
Emittery.d.ts Outdated
@@ -76,6 +76,16 @@ declare class Emittery {
* specified.
*/
listenerCount(eventName?: string): number;

/**
* Bind a `methodNames` or all `Emittery` methods if `methodNames` not defined into `target` object.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Bind a `methodNames` or all `Emittery` methods if `methodNames` not defined into `target` object.
* Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emittery.d.ts Outdated Show resolved Hide resolved
Emittery.d.ts Outdated
/**
* Returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames` or all `Emittery` methods if `methodNames` not defined into target class.
*/
static mixin(emitteryPropertyName: string, methodNames?: string[]): Function;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to the top? I prefer having static properties first.

Suggested change
static mixin(emitteryPropertyName: string, methodNames?: string[]): Function;
static mixin(emitteryPropertyName: string, methodNames?: readonly string[]): Function;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to make the type more specific than Function?

Copy link
Contributor Author

@stroncium stroncium Jun 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not supposed to be more specific than function, I guess. There shouldn't be any type checks on decorators value. It is pretty hard to misuse and it's not supposed to be used directly in TS anyway.

Also, as far as I understand it will require to type function as all possible numbers of arguments of constructor. And do we really need this complication for absurd level check?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Probably not worth it.

Emittery.d.ts Outdated
bindMethods(target: Object, methodNames?: string[]): void;

/**
* Returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames` or all `Emittery` methods if `methodNames` not defined into target class.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames` or all `Emittery` methods if `methodNames` not defined into target class.
* Returns a [decorator](https://github.com/tc39/proposal-decorators) which mixes in `Emittery` as a property named by `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into target class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not conform to this proposal. (Hint: it is a proposal, it is not implemented anywhere yet.)

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated
}
}

static mixin(emitteryPropertyName, methodNames) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this following the latest decorator spec? https://github.com/tc39/proposal-decorators

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a spec, it is a proposal. There is no implementation which can handle decorators implemented in such a way yet. I followed the typescript implementation of decorators.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. But it needs to be clearly documented in the type and readme that it follows the TypeScript implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I described it only for typescript part of readme so far(as using it from JS is weird anyway atm).

test/_run.js Outdated Show resolved Hide resolved
test/_run.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

sindresorhus commented Jun 12, 2019

You also need to document them in the readme. Should use the same text and examples as the TS definition, to make it easy to keep them in sync in the future.

@stroncium
Copy link
Contributor Author

stroncium commented Jun 14, 2019

merged master in

@sindresorhus sindresorhus changed the title Add bindMethods() method and a mixin() decorator Add bindMethods() method and a mixin() decorator (TS) Jun 15, 2019
@sindresorhus sindresorhus merged commit 96a2cb8 into sindresorhus:master Jun 15, 2019
@Andykmcc
Copy link

@sindresorhus can you cut a new release and push it to npm? github master source and docs include this feature but they haven't been pushed to NPM.

@sindresorhus
Copy link
Owner

Done: https://github.com/sindresorhus/emittery/releases/tag/v0.5.0

@glasser
Copy link

glasser commented Jan 24, 2020

It seems to me that this feature is not really TypeScript-specific, right? Should its docs in the readme be in its own ## instead of nested under the TypeScript section?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add convenience method to bind Emittery methods to own instance
4 participants