Skip to content

Instantly share code, notes, and snippets.

@krawaller
Created May 5, 2010 17:03
Show Gist options
  • Save krawaller/391092 to your computer and use it in GitHub Desktop.
Save krawaller/391092 to your computer and use it in GitHub Desktop.
// Demo usage of http://github.com/krawaller/PubSubHotTub
var func = function(){ console.log(Array.prototype.slice.call(arguments).join(" ")); };
pb.sub('/root/leaf/meatloaf', func, 'i', 'like', 'curry');
pb.pub('/root/leaf/meatloaf', 'and', 'cheese'); // => i like curry and cheese
pb.sub('/*/leaf/*', func);
pb.pub('/root/leaf/*', 'cheese'); // => cheese, i like curry cheese ^^ (curry cheese sounds rad)
pb.sub('/cheese/**', func);
pb.pub('/cheese/doodles/rocks', 'yeah'); // => yeah
pb.oneSub('/sausage', func);
pb.pub('/sausage', 'bratwurst'); // => bratwurst
pb.pub('/sausage', 'chorizo'); // => *nothing*
var handle = pb.sub('/goat/cheese', func);
pb.pub('/goat/*', 'cheese?'); // => cheese
pb.unSub(handle);
pb.pub('/goat/*', 'butter?'); // => *nothing*
var obj = {
val: 42,
method: function(){ console.log(this.val); }
};
pb.sub('/gnu', obj, 'method');
pb.pub('/gnu'); // => 42
pb.sub('/**', func, 'logger');
pb.pub('/some/random/func' 'can see dead people'); // => logger can see dead people
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment