Skip to content

trullock/PubSub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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");

About

JavaScript Pub Sub

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published