Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 17, 2011 06:33
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jed/976046 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 )
@floriancargoet
Copy link

setInterval( function(){ pubsub.pub( "foo", 0|Math.random()*10 ) }, 1000 )

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

@jed
Copy link
Author

jed commented May 22, 2011

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

@floriancargoet
Copy link

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.

@jed
Copy link
Author

jed commented May 22, 2011

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment