Git Product home page Git Product logo

pubsub.js's Introduction

pubsub.js

A tiny (~600 bytes when minified, ~300 bytes when gzip'd) and robust pubsub implementation in the spirit of microjs.com evolved from an original implementation by Daniel Lamb (of which, after refactoring and optimizing, only the method names have been kept, hence the change in the project's name).

The library is implemented as a universal module to support CommonJS-enabled, AMD-enabled and traditional Javascript environments and the code is optimized to be as compact, fast and memory-savvy as possible.

The code has also been tested toroughly and the functionality is secured with a complete set of unit tests built with Jasmine.

Supported platforms

  • Node.js
  • Rhino
  • Appcelerator Titanium Mobile 2.0+
  • EcmaScript 5 capable browsers
    • Google Chrome 5+
    • Safari 4+
    • Internet Explorer 5+
    • Firefox 3+
    • Opera 10+
    • Android
    • Mobile Safari
    • Firefox Mobile (Fennec)
    • Opera Mobile 10+

Documentation

The description of each method and its' parameters are inlined in the library's source using the Javadoc syntax.

Examples

Using the library as a traditional Javascript module:

	var p = PubSub;

	//subscribe to a channel
	var handle = p.subscribe("/some/channel", function(msg){
		console.log(msg);
	});

	//publish a message
	p.publish("/some/channel", "Hello!");

	//unsubscribe from the topic
	p.unsubscribe(handle);

Here's the same example using the library as a standard CommonJS module:

	var p = require('pubsub');

	//subscribe to a channel
	var handle = p.subscribe("/some/channel", function(msg){
		console.log(msg);
	});

	//publish a message
	p.publish("/some/channel", "Hello!");

	//unsubscribe from the topic
	p.unsubscribe(handle);

Alternatively using the library as an AMD module (where possible, see RequireJS for more info):

	require('pubsub', function(p){
		//subscribe to a channel
		var handle = p.subscribe("some/channel", function(msg){
			console.log(msg);
		});
	
		//publish a message
		p.publish("/some/channel", "Hello!");
	
		//unsubscribe from the topic
		p.unsubscribe(handle);
	});

The publish function support any number and type of data parameters:

	PubSub.subscribe('/some/channel', function( a, b, c, d ){ /* ... */ });
	PubSub.publish('/some/channel', 1 /* a */, "two" /* b */, [3, 4, 5] /* c */, {total: 15} /* d */);

Credits

pubsub.js's People

Contributors

daniellmb avatar fserb avatar

Stargazers

James Dunlop avatar

Watchers

James Dunlop avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.