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

Calling .unbind() from within a callback skips next callback #10

Open
aron opened this issue May 17, 2011 · 1 comment
Open

Calling .unbind() from within a callback skips next callback #10

aron opened this issue May 17, 2011 · 1 comment

Comments

@aron
Copy link

aron commented May 17, 2011

Because .trigger() is iterating over the array of callbacks when a callback unbinds itself .splice() modifies the array and the it's length decreases by one so the next callback in line is skipped.

In the following example we unbind function B in it's own callback. We would expect "ABC" to be output but instead we only get "AB".

var e = new MicroEvent();

e.bind('change', function A() { console.log('A'); });
e.bind('change', function B() { console.log('B'); e.unbind('change', B); });
e.bind('change', function C() { console.log('C'); });

e.trigger('change'); //=> Expect A B C, but C is not called.

There are at least two solutions, clone the array in either the .trigger() or .unbind() methods. [].slice() will do this.

@aron
Copy link
Author

aron commented May 17, 2011

I've created a patch here to fix the issue should you want it. https://gist.github.com/8e281e63aa2fa0fcdbf8

aron added a commit to BrandwatchLtd/microevent.js that referenced this issue Apr 28, 2014
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

No branches or pull requests

1 participant