Permalink
Browse files

Modified removeListener to set listener to null instead of using spli…

…ce. splice changes the array dynamically and affect the execution order.
  • Loading branch information...
rgr-myrg committed May 4, 2012
1 parent 7076df3 commit 21c3b1ecfbf5581971b7a2b09a492d322d9083aa
Showing with 25 additions and 21 deletions.
  1. +25 −21 src/eventsignal.js
View
@@ -3,28 +3,32 @@
* Released under the MIT license: * Released under the MIT license:
* https://github.com/rgr-myrg/DevShop-JS/raw/master/MIT-LICENSE * https://github.com/rgr-myrg/DevShop-JS/raw/master/MIT-LICENSE
*/ */
(function(DevShop){ (function($){
DevShop.EventSignal=function(obj){ $.EventSignal = function(obj){
var listeners=[]; var listeners = [];
this.addListener=function(listener){ return {
if(typeof listener==='function'){ addListener : function(listener){
listeners.push(listener); if(typeof listener === 'function'){
} listeners.push(listener);
};
this.removeListener=function(listener){
var size=listeners.length;
for(var x=0;x<size;x++){
if(listeners[x]===listener){
listeners.splice(x,1);
} }
} },
}; removeListener : function(listener){
this.dispatch=function(){ var size = listeners.length;
var size=listeners.length; for(var x = 0; x < size; x++){
for(var x=0;x<size;x++){ if(listeners[x] === listener){
try{ //listeners.splice(x, 1);
listeners[x].apply(this,arguments); //Setting to null instead as splice changes the array dynamically
}catch(e){ listeners[x] = null;
}
}
},
dispatch : function(){
var size = listeners.length;
for(var x = 0; x < size; x++){
var listener = listeners[x];
if(typeof listener === 'function'){
listeners[x].apply(this, arguments);
}
} }
} }
}; };

0 comments on commit 21c3b1e

Please sign in to comment.