Skip to content

Instantly share code, notes, and snippets.

@totty90
Created January 25, 2011 01:23
Show Gist options
  • Save totty90/794351 to your computer and use it in GitHub Desktop.
Save totty90/794351 to your computer and use it in GitHub Desktop.
Javascript: inspired from actionscript 3 signals
var Signal = function(){}
Signal.prototype = {
init: function(){
this.listeners = [];
this.looping = false;
this.length = 0;
},
add: function(method, applyOn){
if(!method) return;
this.listeners[this.length] = {method: method, applyOn: applyOn};
this.length += 1;
},
remove: function(method){
if(this.looping){
this.listeners[this.listeners.indexOf(method)] = false;
this.length -= 1;
}else{
this.listeners.splice(this.listeners.indexOf(method), 1);
}
},
removeAll: function(){
this.listeners = [];
this.length = 0;
},
destroy: function(){
this.removeAll();
delete this.listeners;
},
dispatch: function(){
var listener;
this.looping = true;
for(var i = 0; i < this.length; i++){
if(typeof this.listeners[i] === "object"){
listener = this.listeners[i];
listener.method.apply(listener.applyOn, arguments);
}
}
this.looping = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment