Skip to content

Instantly share code, notes, and snippets.

@bsparks
Forked from jed/LICENSE.txt
Created December 5, 2011 23:58
Show Gist options
  • Save bsparks/1435991 to your computer and use it in GitHub Desktop.
Save bsparks/1435991 to your computer and use it in GitHub Desktop.
publish/subscribe, or pubsub
function(
a, // the subscription object
b, // the current event number
c // (placeholder)
){
return { // return an object, with
pub: function( // a "pub" function that takes
d, // a channel name and
e // a message value,
){ // and
for( // for each
c // subscriber
in a // in the subscription object,
) if ( // if
c // its name, with the event number discarded
.split // by splitting it
("-") // with a hyphen and
[0] // taking only the first part,
== d // is equal to the published channel
) a[c](e) // call the subscriber. and if
!== !1 || // the value returned is false
delete a[c] // unsubscribe by removing the subscriber, and
},
sub: function( // a "sub" function that takes
d, // a channel name and
e // a subscriber function
){ // and,
a[ // on the subscriptions object,
d + // sets the name of the channel appended with
--b // a hyphen and the decremented event number, to
] = e // the function passed.
}
}
}( // auto-run with
{}, // the subscription object
0 // and event id.
)
function(a,b,c){return{pub:function(d,e){for(c in a)if(c.split("-")[0]==d)a[c](e)!==!1||delete a[c]},sub:function(d,e){a[d+--b]=e}}}({},0)
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
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.
{
"name": "pubsub",
"keywords": [ "pubsub", "observer", "publish", "subscribe" ]
}
// Define the pubsub object, which provides the "pub" and "sub" methods.
var pubsub = function(a,b,c){return{pub:function(d,e){for(c in a)if(c.split("-")[0]==d)a[c](e)!==!1||delete a[c]},sub:function(d,e){a[d+--b]=e}}}({},0)
// Subscribe to the "random" channel, returning false to unsubscribe.
pubsub.sub("random", function( msg ) {
console.log("this is called once: " + msg);
return false;
})
// Subscribe to the "random" channel, and always renew.
pubsub.sub("random", function( msg ) {
console.log("this is called forever: " + msg);
})
// Subscribe to the "random" channel, but set an
// expiration date 5 seconds in the future, and
// unsubscribe when the date is reached.
expiration = +new Date + 5000
pubsub.sub("random", function( msg ) {
console.log( "this is called for 5 seconds: " + msg);
return +new Date < expiration;
})
// Publish a random number between 1 and 10 to
// the "random" channel every second.
setInterval( function(){ pubsub.pub( "random", 0|Math.random()*10 ) }, 1000 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment