Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
speigg committed Sep 15, 2017
1 parent 2fa9955 commit b018f69
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
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
23 changes: 22 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,27 @@ defineSuite([
expect(event.numberOfListeners).toEqual(0);
});

it('can remove multiple listeners within a callback', function() {
var listeners = [];

function addListener(index) {
var remove;
var listener = function() {
if (index % 2 === 0) remove(); // remove even listeners
};
remove = event.addEventListener(listener);
listeners.push(listener);
}

for (var index = 0; index < 10; index++) {
addListener(index);
}

expect(event.numberOfListeners).toEqual(10);
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 b018f69

Please sign in to comment.