Signals allow an event to be dispatched to listening slots, without using a global object and without using strings for event names. For more information, see js-signals, which this is mostly based on. Typically, signals are named in the past tense, and can pass arbitrary arguments to the listening slots.
-
-
Save kirbysayshi/1286278 to your computer and use it in GitHub Desktop.
Signals in JS
This file contains hidden or 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
function( | |
a, // placeholder | |
b, // placeholder | |
c // placeholder | |
){ | |
return { // return an object | |
s: a = [] // s stands for 'slots', initialize a to empty array (slots) | |
,d: function(){ // d stands for 'dispatch', and accepts an arbitrary number of args | |
for( | |
c = a.slice(); // copy the array of slots | |
b = c.pop(); // loop until c.pop() returns undefined, unfortunately in reverse order | |
) | |
b.apply(this,arguments) // b is a slot, apply the args | |
} | |
,r: function(c){ // r stands for 'remove', accepts a single slot (function) to remove | |
a.splice( // splice the list of slots | |
a.indexOf(c), // matching the passed slot's index | |
1) // and remove that element | |
} | |
} | |
} |
This file contains hidden or 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
function(a,b,c){return{s:a=[],d:function(){for(c=a.slice();b=c.pop();)b.apply(this,arguments)},r:function(c){a.splice(a.indexOf(c),1)}}} |
This file contains hidden or 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Andrew Petersen <http://kirbysayshi.github.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains hidden or 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
{ | |
"name": "signal", | |
"description": "A signal, for named event properties", | |
"keywords": [ | |
"signal", | |
"event", | |
"emitter", | |
"slot" | |
] | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b><br />what it was: bigfoot sasquatch [object Window]<br/>what it was bigfoot [object Object]</b></div> | |
<div>Actual value: <br /><b id="ret"></b></div> | |
<script> | |
var Signal = function(a,b,c){return{s:a=[],d:function(){for(c=a.slice();b=c.pop();)b.apply(this,arguments)},r:function(c){a.splice(a.indexOf(b),1)}}} | |
var Something = function(){ | |
this.wasHere = Signal(); | |
this.move = function(){ | |
this.wasHere.d('bigfoot', 'sasquatch'); // d for dispatch! | |
} | |
} | |
// a slot is just a callback | |
,what = function(wasIt){ | |
// 'this' will be the signal obj itself | |
document.getElementById( "ret" ).innerHTML | |
+= 'what it was: ' | |
+ wasIt + ' ' + this + '<br />'; | |
} | |
,scope = (function(wasIt, sas){ | |
// 'this' will be the global obj | |
document.getElementById( "ret" ).innerHTML | |
+= 'what it was: ' | |
+ wasIt + ' ' + sas + ' ' + this + '<br />'; | |
}).bind(this) | |
var s = new Something(); | |
s.wasHere.s.push(what); // add 'what' as a slot in the wasHere signal | |
s.wasHere.s.push(scope); // add 'scope' as a slot in the wasHere signal | |
s.move(); // this signals the slots in reverse order | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment