Skip to content

Commit

Permalink
Modified removeListener to set listener to null instead of using spli…
Browse files Browse the repository at this point in the history
…ce. splice changes the array dynamically and affect the execution order.
  • Loading branch information
rgr-myrg committed May 4, 2012
1 parent 7076df3 commit 21c3b1e
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/eventsignal.js
Expand Up @@ -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);
}
}
}
};
Expand Down

0 comments on commit 21c3b1e

Please sign in to comment.