-
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
Adds ability to removeListener when in the same emit loop #65
Conversation
Adds ability to removeListener when in the same emit loop
foo = null; | ||
e.removeListener('update', second); | ||
} | ||
function second() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldnt second never be called? we should test that this is the case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copying this test to what's in master (no change from PR), this test fails with TypeError: Cannot read property 'bar' of null
. Current master attempts to call second
in the same emit call. It's not removed until the next stack.
The Node.js EventEmitter has the same behavior. It calls |
The use-case for this was creating an update loop that things could subscribe to. If anything is done synchronously in the callbacks to destroy other listeners to the same event loop, it complained. @lpinca If not a bug fix, would you consider this a feature update? There are already some divergent things from Node's EventEmitter. |
Here are a couple of relevant discussions: nodejs/node-v0.x-archive#7872 This change might have sense as it is probably the expected behavior. It is a breaking change but the next version will be a major anyway as there are other breaking changes in master. |
That change makes sense for NodeJS. One of the Node Foundation comments said "Your best bet might be to use an alternative EE implementation". So that's why I'm here. Would love to see this happen. |
On vacation atm, will look at it once im back
|
@bigtimebuddy can you add docs for this change? Another element in the existing bulleted list at the top of the README should suffice. |
Sure, you bet. |
I'm a -1 on this and the reason for that is plain simple. Adding this will break compatibility with Node.js as we are no longer handling events in the same way they are using. I can see the use-case here though but I don't know if this edge case is enough to break compatibility for. |
@3rd-Eden Thanks for chiming in. The README lists a bunch of subtle differences between Node and EE3 where changed implementation or API differs slightly. This doesn't seem like the first breaking-change. Like you said, this is an edge-case so it will probably not affect many people. It doesn't seem unreasonable to assume that if one removes a listener from within another callback, that should happen immediately not asynchronously. Any compromises here? Maybe until the next major release, throwing a console.warn or info to let developers know when this happens in their code? Would make it easier for developers to upgrade their code, if this breaking change does, in fact, present issues. |
I don't know if it makes sense to put this behavior under an option disabled by default. |
I have created a fork for this. Thanks for the consideration. |
Overview
This PR
fixes a bugintroduces a new feature when trying to remove an event listener immediately when theremoveListener
call present in one of the listeners of the stack currently being called.Bug Example