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

Fix bug where EventBus handlers are removed before execution #1774

Conversation

justinanastos
Copy link
Contributor

Stop mutating handlers[type] in EventBus#off. Doing so prevents all handlers from being run if off() is called while executing a callback.

Fixes #1773

@dsparacio
Copy link
Contributor

Seems like a great fix. just missed the release ;) Thanks for the commit.

@justinanastos justinanastos changed the title Stop mutating handler[type] in EventBus Fix bug where EventBus handlers are removed before execution Feb 2, 2017
@justinanastos
Copy link
Contributor Author

justinanastos commented Feb 2, 2017

@AkamaiDASH

Seems like a great fix. just missed the release ;) Thanks for the commit.

Darn! How often does your team release?

@justinanastos justinanastos force-pushed the fix-event-bus-handler-mutation branch 2 times, most recently from 9876e0d to cc621d4 Compare February 3, 2017 20:13
If you remove the handler currently being executed, the next
handler in the list will not be run.
@dsparacio
Copy link
Contributor

6 weeks or so. It will get merged this week into dev and nightly.

@dsparacio dsparacio merged commit 87c9dd7 into Dash-Industry-Forum:development Feb 15, 2017
@justinanastos justinanastos deleted the fix-event-bus-handler-mutation branch February 15, 2017 23:49
@dsparacio
Copy link
Contributor

@justinanastos Looks like we have a regression here from last release that will hold up this release. I can either revert your change or you can try to address it.

@justinanastos
Copy link
Contributor Author

Thanks for the heads up @AkamaiDASH , I'll look into the regression today.

@justinanastos
Copy link
Contributor Author

justinanastos commented Apr 24, 2017

I reviewed #1889 and it doesn't seem like this PR (#1744) caused a regression, but revealed a bug in the code elsewhere.

Previously, when the handler list was mutated, the handler would be removed and not called.

As far as I can understand, this is a mistake. calling off(type, listener, scope) should not be removing anything besides listener. #1889 is caused by expecting off to work incorrectly.

@davemevans
Copy link
Contributor

#1858 is caused by expecting off to work incorrectly.

I think anyone calling off would be surprised to see that listener is still called in certain circumstances. I don't believe it is expecting off to work incorrectly to expect listener not to be called at all after a call to off.

Before this PR, calling off removed listener and it was no longer called, including during the current trigger iteration. After the merge, calling off removes the listener, but it will still be called, if it was registered after the listener ultimately calling off, if removed during the current trigger iteration. This seems like a regression to me?

Another approach might be to set handler[type][idx] = undefined in off, and check handler is defined in
the forEach in trigger, getHandlerIdx etc though that is not ideal as the handler list will grow and need to be cleaned up - see https://gist.github.com/bbcrddave/e314004600eadc6452fc124ab41c6905.

@justinanastos
Copy link
Contributor Author

Looking again with fresh eyes, you are totally right and I was mistaken. I apologize for my hasty response, I didn't read #1889 correctly.

The bug I was trying to fix can be seen here: https://jsbin.com/haculos/2/edit?js,console

While traversing the array of handlers, if a handler removes the event listener for the current handler, the next handler will not be called.

A apparently didn't understand how Array.prototype.forEach worked well enough.

@bbcrddave I like your gist. I submitted PR #1891 with one variation: I used setTimeout(..., 0) to clean up the null handlers in off so we don't have to make trigger clean it up.

justinanastos added a commit to justinanastos/dash.js that referenced this pull request Apr 25, 2017
justinanastos added a commit to justinanastos/dash.js that referenced this pull request Apr 25, 2017
justinanastos added a commit to justinanastos/dash.js that referenced this pull request Apr 25, 2017
@dsparacio
Copy link
Contributor

All agree we should hold the release until we sort this one out?

dsparacio pushed a commit that referenced this pull request Apr 25, 2017
fix(EventBus): Fix faulty logic for `off`, caused by #1774
justinanastos added a commit to justinanastos/dash.js that referenced this pull request May 3, 2017
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.

3 participants