-
-
Save cocodrino/82b0cb231a5a8cdc5192 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Script: Signal.js | |
Makes chaining functions easy! | |
License & Copyright: | |
Copyright 2009, Mark Obcena <keetology.com> | |
MIT-Style License | |
*/ | |
(function(global){ | |
this.Signal = new Class({ | |
obj: global, | |
fn: $empty, | |
fnName: '$empty', | |
preStack: [], | |
postStack: [], | |
initialize: function(fn, obj){ | |
obj = this.obj = obj || global; | |
if (typeof fn !== 'string' | |
&& !(obj[fn] instanceof Function)) throw new Error("No such function"); | |
this.fnName = fn; | |
this.fn = obj[fn]; | |
this.attach(); | |
}, | |
attach: function(){ | |
var self = this; | |
this.obj[this.fnName] = function(){ | |
for (var x = 0, y = self.preStack.length; x < y; x++){ | |
var item = self.preStack[x]; | |
item.obj[item.fn].apply(item.obj, arguments); | |
} | |
var result = self.fn.apply(self.obj, arguments); | |
for (var x = 0, y = self.postStack.length; x < y; x++){ | |
var item = self.postStack[x]; | |
item.obj[item.fn].apply(item.obj, arguments); | |
} | |
return result; | |
}; | |
}, | |
detach: function(){ | |
this.obj[this.fnName] = this.fn; | |
}, | |
connect: function(fn, obj, before){ | |
obj = obj || global; | |
if (typeof fn !== 'string' && !(obj[fn] instanceof Function)) return; | |
this[before ? 'preStack' : 'postStack'].push({ | |
obj: obj, | |
fn: fn | |
}); | |
return this; | |
}, | |
disconnect: function(fn, obj, before){ | |
obj = obj || global; | |
var stack = before ? 'preStack' : 'postStack'; | |
this[stack] = this[stack].filter(function(item){ | |
return !(item.obj === obj && item.fn === fn); | |
}); | |
return this; | |
} | |
}); | |
})(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Globals | |
var woot = function(msg){ | |
console.log('Woot! ' + msg); | |
}; | |
var yay = function(msg){ | |
console.log('Yay! ' + msg); | |
}; | |
var mySignal = new Signal('woot'); | |
mySignal.connect('yay'); | |
woot('test'); | |
// Woot! test | |
// Yay! test | |
mySignal.disconnect('yay'); | |
woot('test'); | |
// Woot! test | |
// Methods | |
var A = { | |
woot: function(msg){ | |
console.log('Woot! ' + msg); | |
} | |
}; | |
var B = { | |
yay: function(msg){ | |
console.log('Yay! ' + msg); | |
} | |
}; | |
var mySignal = new Signal('woot', A); | |
mySignal.connect('yay', B); | |
A.woot('test'); | |
// Woot! test | |
// Yay! test | |
mySignal.disconnect('yay', B); | |
A.woot('test'); | |
// Woot! test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment