Skip to content

Commit

Permalink
Merge pull request #5833 from aelatgt/fix-event
Browse files Browse the repository at this point in the history
Fixed removing multiple listeners within event callbacks #5827
  • Loading branch information
pjcozzi authored Sep 28, 2017
2 parents c5a627c + 020998c commit d5040f1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Change Log
* Fixed CZML processing of `velocityReference` within an interval. [#5738](https://github.com/AnalyticalGraphicsInc/cesium/issues/5738)
* Zoom about mouse now maintains camera heading, pitch, and roll [#4639](https://github.com/AnalyticalGraphicsInc/cesium/pull/5603)
* Fixed a bug in `PolylineCollection` preventing the display of more than 16K points in a single collection [#5538](https://github.com/AnalyticalGraphicsInc/cesium/pull/5782)
* Fixed removing multiple event listeners within event callbacks. [#5827](https://github.com/AnalyticalGraphicsInc/cesium/issues/5827)

### 1.37 - 2017-09-01

Expand Down
17 changes: 12 additions & 5 deletions Source/Core/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ define([
return false;
};

function compareNumber(a,b) {
return b - a;
}

/**
* Raises the event by calling each registered listener with all supplied arguments.
*
Expand All @@ -146,12 +150,15 @@ define([
//Actually remove items removed in removeEventListener.
var toRemove = this._toRemove;
length = toRemove.length;
for (i = 0; i < length; i++) {
var index = toRemove[i];
listeners.splice(index, 1);
scopes.splice(index, 1);
if (length > 0) {
toRemove.sort(compareNumber);
for (i = 0; i < length; i++) {
var index = toRemove[i];
listeners.splice(index, 1);
scopes.splice(index, 1);
}
toRemove.length = 0;
}
toRemove.length = 0;

this._insideRaiseEvent = false;
};
Expand Down
21 changes: 20 additions & 1 deletion Specs/Core/EventSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defineSuite([
expect(spyListener).not.toHaveBeenCalled();
});

it('can remove from withing a callback', function() {
it('can remove from within a callback', function() {
var doNothing = function(evt) {
};

Expand All @@ -67,6 +67,25 @@ defineSuite([
expect(event.numberOfListeners).toEqual(0);
});

it('can remove multiple listeners within a callback', function() {
var removeEvent0 = event.addEventListener(function() { removeEvent0(); });
event.addEventListener(function() {});
var removeEvent2 = event.addEventListener(function() { removeEvent2(); });
event.addEventListener(function() {});
var removeEvent4 = event.addEventListener(function() { removeEvent4(); });
event.addEventListener(function() {});
var removeEvent6 = event.addEventListener(function() { removeEvent6(); });
event.addEventListener(function() {});
var removeEvent8 = event.addEventListener(function() { removeEvent8(); });
event.addEventListener(function() {});

expect(event.numberOfListeners).toEqual(10);
event.raiseEvent();
expect(event.numberOfListeners).toEqual(5);
event.raiseEvent();
expect(event.numberOfListeners).toEqual(5);
});

it('addEventListener and removeEventListener works with same function of different scopes', function() {
var Scope = function() {
this.timesCalled = 0;
Expand Down

0 comments on commit d5040f1

Please sign in to comment.