-
Notifications
You must be signed in to change notification settings - Fork 226
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
[api] Add removeListenersByContext(context) method #178
base: master
Are you sure you want to change the base?
Conversation
Hmm, not really sure what this username and accessKey stuff for SAUCE is all about. Unit tests pass for sure. |
Encrypted variables are not available to untrusted builds such as pull requests coming from another repository so don't worry about that. Can we find a better name? Like, I don't know, |
@lpinca Thanks. Method renamed. |
@onlywei can't this be done with something like this? for (const event of ee.eventNames()) {
for (const listener of ee.listeners(event)) {
ee.removeListener(event, listener, context);
}
} I understand it is inefficient but I wonder if the suggested API will ever be used in hot paths. I'm a bit reluctant about adding this method because:
|
When using EventEmitter with any sort of browser-side framework such as Angular or React, a common pattern is such: export default class MyReactComponent {
componentDidMount() {
// someEE is passed in as a prop here
this.props.someEE.on('someEvent', () => doSomething(), this);
// ... attach more listeners ...
}
componentWillUnmount() {
this.props.someEE.removeListenersByContext(this);
}
} Angular components have a similar lifecycle using with $onInit and $onDestroy methods. If we are not destroying our components very often, then I guess this would not be a hot path. However, imagine that you have a list of components that is paginated and maybe can display hundreds of items per page. In this scenario, every time you paginate or sort or filter, these hundreds of items will destroy themselves and then get re-created. Is that "hot" enough for this library? I believe only you can decide :) |
It would be nice to see a benchmark. |
I'm indifferent about this addition. I think that most people are now using fat arrows to have the correct context by default so I don't consider this feature that heavily used anymore. |
@3rd-Eden Using fat arrow functions only solves the |
I find it very convenient when cleaning up my EventEmitters to remove all listeners that were added with a specific context. This allows me to avoid having to save bound references to my callback functions for use later in my destroy lifecycle methods.