public
Last active — forked from 140bytes/LICENSE.txt

publish/subscribe, or pubsub

  • Download Gist
LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
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.
annotated.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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.
)
package.json
JSON
1 2 3 4
{
"name": "pubsub",
"keywords": [ "pubsub", "observer", "publish", "subscribe" ]
}
test.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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 )
setInterval( function(){ pubsub.pub( "foo", 0|Math.random()*10 ) }, 1000 )

Shouldn't the channel be "random" instead of "foo"?

foo is the channel, and the message is a random number.

Yes but in this example, suscribers are listening to the "random" channel while the publisher uses the "foo" channel. Nothing happens.
In addition to that, the comment is about the "random" channel.

ha, good catch. my apologies, i misunderstood you. will fix now.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.