newsletter

Simple pub/sub implementation.

Install

$ npm install newsletter

API

To create publisher instance use Newsletter constructor (see Usage). Instance implements next interface:

Usage

// get newsletter
import Newsletter from 'newsletter';

// create instance
var signal = new Newsletter();

// subscribe notifications
signal.subscribe((data) => console.log(data));

// publish some data to subscribers
signal.publish(13);

There are two ways to remove listeners

// add listener and get "unsubscribe" function
var unsubscribe = signal.subscribe(() => ...);

// remove listener
unsubscribe();
function listener() { ... }

// add listener
signal.subscribe(listener);

// remove listener
signal.unsubscribe(listener);

There is a way to subscribe to a single update

var signal = new Newsletter();

// subscribe to a single update
signal.subscribe((data) => console.log(data), { once: true });

// will call a listener and remove it
signal.publish(13);

// no listeners called
signal.publish(14);

The project is licensed under the MIT license.