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
@@ -3,28 +3,32 @@
* Released under the MIT license:
* https://github.com/rgr-myrg/DevShop-JS/raw/master/MIT-LICENSE
*/
(function(DevShop){
DevShop.EventSignal=function(obj){
var listeners=[];
this.addListener=function(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);
(function($){
$.EventSignal = function(obj){
var listeners = [];
return {
addListener : function(listener){
if(typeof listener === 'function'){
listeners.push(listener);
}
}
};
this.dispatch=function(){
var size=listeners.length;
for(var x=0;x<size;x++){
try{
listeners[x].apply(this,arguments);
}catch(e){
},
removeListener : function(listener){
var size = listeners.length;
for(var x = 0; x < size; x++){
if(listeners[x] === listener){
//listeners.splice(x, 1);
//Setting to null instead as splice changes the array dynamically
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.