Fix listener removal even if an event is firing

Close #4840.
This commit is contained in:
Per Liedman 2016-08-25 15:20:51 +02:00
parent 59c6bb201c
commit cee8edeaf2
2 changed files with 23 additions and 1 deletions

View File

@ -361,6 +361,28 @@ describe('Events', function () {
expect(spy1.called).to.be(true);
expect(spy2.called).to.be(false);
});
it('can remove an event listener while firing', function () {
var obj = new L.Evented(),
spy = sinon.spy();
var removeSpy = function () {
obj.removeEventListener('test', spy);
};
obj.addEventListener('test', spy);
obj.addEventListener('test', removeSpy);
obj.fire('test');
obj.removeEventListener('test', removeSpy);
// expect(obj.listens('test', L.Util.falseFn)).to.be(false);
// Remove the expect below and comment out the one above, once we've
// gotten rid of _events.count (which makes the expect above pass).
var listeners = obj._events['test'];
expect(listeners.count === listeners.listeners.length).to.be(true);
});
});
describe('#on, #off & #fire', function () {

View File

@ -165,7 +165,7 @@ L.Evented = L.Class.extend({
if (this._firingCount) {
/* copy array in case events are being fired */
listeners = listeners.slice();
typeListeners.listeners = listeners = listeners.slice();
}
listeners.splice(i, 1);