Skip to content

Latest commit

 

History

History
77 lines (50 loc) · 1.45 KB

readme.md

File metadata and controls

77 lines (50 loc) · 1.45 KB

PubSub

Simple publisher-subscriber bus with handler invokation queuing and unlimited argument support.

Subscribing

bus.subscribe("message name", function(){
	// do stuff
});

Publishing

bus.publish("message name");

Passing Arguments

You can pass unlimited arguments:

bus.publish("message name", x, y, z);

bus.subscribe("message name", function(arg1, arg2, arg3){
	// do stuff
});

Queuing

When a message is published, all handlers for that message fire in the order they were subscribed.

If any of the handlers publish new messages, the handlers for this message will not be executed until the original set of handlers have been executed. This rule is obeyed recursively. Handler executions are continuously enqueued.

Unsubscribing

You can remove all handlers for all messages:

bus.unsubscribeAll();

You can remove all handlers for a given message:

bus.unsubscribeAll("message name");

You can remove a specific handler:

var handlerFunc = function() { };
bus.subscribe("message name", handlerFunc);

bus.unsubscribe(handlerFunc);

Complete Example

var bus = new Bus();

bus.subscribe("welcome", function(name){
	alert('Welcome ' + name);
});

bus.subscribe("welcome", function(name){
	if(window.console)
		console.log('Welcome ' + name);
});

bus.publish("welcome", "Zuul");