Make event firing reentrant

Close #4833
This commit is contained in:
Per Liedman 2016-08-24 09:22:29 +02:00
parent e9957cfa02
commit 0c6dc2ce1e
2 changed files with 23 additions and 3 deletions

View File

@ -341,6 +341,26 @@ describe('Events', function () {
expect(spy.called).to.be(false);
});
it('handles reentrant event firing', function () {
var obj = new L.Evented(),
spy = sinon.spy();
obj
.addEventListener('test1', function () {
obj.fire('test2');
})
.addEventListener('test2', function () {
// NOP, just to make sure event actually fires
})
.addEventListener('test1', function () {
obj.removeEventListener('test1', spy);
})
.addEventListener('test1', spy);
obj.fireEvent('test1');
expect(spy.called).to.be(false);
});
});
describe('#on, #off & #fire', function () {

View File

@ -163,7 +163,7 @@ L.Evented = L.Class.extend({
l.fn = L.Util.falseFn;
typeListeners.count--;
if (this._isFiring) {
if (this._firingCount) {
/* copy array in case events are being fired */
listeners = listeners.slice();
}
@ -188,14 +188,14 @@ L.Evented = L.Class.extend({
var typeListeners = this._events[type];
if (typeListeners) {
this._isFiring = true;
this._firingCount = (this._firingCount + 1) || 1;
var listeners = typeListeners.listeners;
for (var i = 0, len = listeners.length; i < len; i++) {
var l = listeners[i];
l.fn.call(l.ctx || this, event);
}
this._isFiring = false;
this._firingCount--;
}
}