From b018f69ed022ccfb08e3cb482e9055b0b091f0cb Mon Sep 17 00:00:00 2001 From: Gheric Speiginer Date: Fri, 15 Sep 2017 14:28:56 -0400 Subject: [PATCH] fix #5827 --- Source/Core/Event.js | 17 ++++++++++++----- Specs/Core/EventSpec.js | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Source/Core/Event.js b/Source/Core/Event.js index 4244fca65768..2f5b6b810add 100644 --- a/Source/Core/Event.js +++ b/Source/Core/Event.js @@ -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. * @@ -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; }; diff --git a/Specs/Core/EventSpec.js b/Specs/Core/EventSpec.js index 944a04f61a2e..7c88919b8661 100644 --- a/Specs/Core/EventSpec.js +++ b/Specs/Core/EventSpec.js @@ -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) { }; @@ -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;