sublish

Minimalist publish/subscribe.

npm install sublish
1 downloads in the last week
23 downloads in the last month

Synopsis

sublish is a minimalist lightweight (504 Bytes minified and 297 Bytes gzipped) implementation of publish/subscribe.

stability 3 - stable license - Unlicense Flattr this

browser support

Build Status Coverage Status Dependencies

NPM status

Install

Node.js

With NPM

npm install sublish

From source

git clone https://github.com/pluma/sublish.git
cd sublish
npm install
make && make dist

Browser

With component

component install pluma/sublish

Learn more about component.

With bower

bower install sublish

Learn more about bower.

With a CommonJS module loader

Download the latest minified CommonJS release and add it to your project.

Learn more about CommonJS modules.

With an AMD module loader

Download the latest minified AMD release and add it to your project.

Learn more about AMD modules.

As a standalone library

Download the latest minified standalone release and add it to your project.

<script src="/your/js/path/sublish.globals.min.js"></script>

This makes the sublish module available in the global namespace.

Basic usage example

var myPubSub = new sublish.PubSub();

myPubSub.subscribe(function(message) {
  console.log('myPubSub says: "' + message + '"');
});

// elsewhere
myPubSub.publish('something amazing');
// -> 'myPubSub says: "something amazing"'

Mixin usage example with mixed

function Person(name) {
    this.name = name;
}
Person.prototype = {
    say: function(message) {
        this.publish(this.name + ' says: "' + message + '"');
    }
};

var joe = new Person('Joe');
mixed.mixin(sublish.PubSub, joe);

joe.subscribe(function(message) {
    console.log(message);
});

// elsewhere
joe.say('Hello there!');
// -> 'Joe says: "Hello there!"

API

new PubSub()

Creates a new PubSub instance.

NOTE: This is a constructor. Use of the new keyword is therefore not optional.

PubSub#subscribe(callback:Function)

Adds the given callback function to this object's list of subscribers.

NOTE: The callback will be called with the PubSub instance as its context. If you want to preserve the callback's original context, use Function#bind or (in legacy browsers) wrap the callback in a closure.

PubSub#unsubscribe(callback:Function)

Removes the given callback function from this object's list of subscribers.

PubSub#publish(argsā€¦)

Publishes the given arguments as a message. Every callback function in this object's list of subscribers will be called sequentially with the given messages as its arguments.

Unlicense

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.

npm loves you