Do not remove listeners while dispatching event
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
goog.provide('ol.events.EventTarget');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.Disposable');
|
||||
goog.require('ol.events');
|
||||
goog.require('ol.events.Event');
|
||||
@@ -26,6 +27,12 @@ ol.events.EventTarget = function() {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {!Object.<string, number>}
|
||||
*/
|
||||
this.pendingRemovals_ = {};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {!Object.<string, Array.<ol.events.ListenerFunctionType>>}
|
||||
@@ -46,7 +53,7 @@ ol.events.EventTarget.prototype.addEventListener = function(type, listener) {
|
||||
listeners = this.listeners_[type] = [];
|
||||
}
|
||||
if (listeners.indexOf(listener) === -1) {
|
||||
listeners.unshift(listener);
|
||||
listeners.push(listener);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -64,12 +71,17 @@ ol.events.EventTarget.prototype.dispatchEvent = function(event) {
|
||||
evt.target = this;
|
||||
var listeners = this.listeners_[type];
|
||||
if (listeners) {
|
||||
for (var i = listeners.length - 1; i >= 0; --i) {
|
||||
if (listeners[i].call(this, evt) === false ||
|
||||
evt.propagationStopped) {
|
||||
this.pendingRemovals_[type] = 0;
|
||||
for (var i = 0, ii = listeners.length; i < ii; ++i) {
|
||||
if (listeners[i].call(this, evt) === false || evt.propagationStopped) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var pendingRemovals = this.pendingRemovals_[type];
|
||||
delete this.pendingRemovals_[type];
|
||||
while (pendingRemovals--) {
|
||||
this.removeEventListener(type, ol.nullFunction);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -84,7 +96,7 @@ ol.events.EventTarget.prototype.disposeInternal = function() {
|
||||
|
||||
/**
|
||||
* Get the listeners for a specified event type. Listeners are returned in the
|
||||
* opposite order that they will be called in.
|
||||
* order that they will be called in.
|
||||
*
|
||||
* @param {string} type Type.
|
||||
* @return {Array.<ol.events.ListenerFunctionType>} Listeners.
|
||||
@@ -114,9 +126,16 @@ ol.events.EventTarget.prototype.removeEventListener = function(type, listener) {
|
||||
var listeners = this.listeners_[type];
|
||||
if (listeners) {
|
||||
var index = listeners.indexOf(listener);
|
||||
listeners.splice(index, 1);
|
||||
if (listeners.length === 0) {
|
||||
delete this.listeners_[type];
|
||||
goog.asserts.assert(index != -1, 'listener not found');
|
||||
if (type in this.pendingRemovals_) {
|
||||
// make listener a no-op, and remove later in #dispatchEvent()
|
||||
listeners[index] = ol.nullFunction;
|
||||
++this.pendingRemovals_[type];
|
||||
} else {
|
||||
listeners.splice(index, 1);
|
||||
if (listeners.length === 0) {
|
||||
delete this.listeners_[type];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user