-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Events #2883
Events #2883
Conversation
merging events test in new events
This will not save us from everything. Some bad implementation could fire Event "A" , and then in an handler of "A" , delete some other "A" handlers and fire "A" again before the first one finished firing. The sub event "A" would finish, clear the array from the false one, and then the outerloop would get an error because the array is shortened. Tracking wich event is being fired adds comeplexity, we should have an array of firing events, at the end of the loop we should check if the event is still firing and just in that moment clear the events marked for deletion. |
} | ||
this.__eventListeners[eventName] = listenersForEvent.filter(function(value) { | ||
return (value === false ? false : true); |
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.
return value !== false
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.
of course ;p
just playing around trying to make a test fail.
@asturur after reading the code, I am still unsure about this. This is not a new problem, and every library I saw solves this problem the same way, by cloning the array before it loops over to fire the events. Maybe we should just try it and see how big of a performance impact it is. |
I'm unsure as well, that is why I'm discussing with you. Just to add more doubts, "off" is an alias of "stopObserving". But "off" in my opinion would lead to immediate stop, while effectively "stopObserving" could suggest something different, like stop a the next time the event fires. Only thing shure is that we should have same behaving for adding and stopping handlers. |
All the event emitter implementations I have seen have that problem (where the second listener will still fire if removed by the first). It doesn't really seem like a realistic scenario, and can't really see a valid use case for it. |
Modify how event listeners get deactivted.
Our current implementation slice the array for listeners in the moment it gets set to "off". The array shift and either it throws an error or it jumps a currently registered event.
While the use case are rare and often are just bad implementation, having a more robust code should not be problematic at performance level.
Solution proposed are:
clone the eventListener array before iterating over it
or
mark the events for deletion and delete them at the end of the trigger
closes #2706