-
Notifications
You must be signed in to change notification settings - Fork 388
once doesn't always work #54
Comments
Code like the following describe('play button', function() {
it('should work', function(done) {
var state = player.playing();
console.log('-');
player.once('track.toggle', function() {
console.log('.');
assert(player.playing()).not().equals(state);
var newState = player.playing();
player.once('track.toggle', function() {
assert(player.playing()).not().equals(newState);
assert(player.playing()).equals(state);
done();
});
$el.find('[data-button=play]').click();
});
$el.find('[data-button=play]').click();
});
}); results in this in the log. |
Managed to reduce the test case to this. var ee = new EventEmitter();
var counter = document.querySelector('#count');
var count = 0;
ee.once('foo', function() {
counter.innerHTML = ++count;
this.trigger('foo');
});
ee.trigger('foo'); |
Am I correct in saying that if you recursively execute a once listener it will be called twice and only twice? If that is the case then it should be fixed by removing once listeners before they are executed. It seems a little bit weird to me, but that will probably be the only way. |
No, that test case I showed you will run until there is a StackOverflow. The reason mine only ran twice was because of the way I ended up triggering the event inside of the listener. However, it will be fixed by removing the once listener before it is triggered. |
Okay got it, should still be able to fix it in the same way though. Thanks for bringing this up, looking into it now. |
Fixed and releasing now. I don't like repeating the removal line but it is a necessary evil at the moment. I was thinking of adding a partial application function and creating a removal variable which can be executed when required. That would save duplicating any arguments and function calls but it seems like overkill for such a trivial change. I'll just live with it for now. I can restructure in the future when the API is even more mature and I know what users actually want. |
Awesome, thanks mate. |
In some situations, a once event gets fired multiple times.
I haven't yet made a reproducible test case for this, but I've seen it happen and cause bugs in projects of mine.
The text was updated successfully, but these errors were encountered: