npm.io
1.1.8 • Published 7 years ago

subnpub

Licence
ISC
Version
1.1.8
Deps
0
Size
7 kB
Vulns
0
Weekly
0

subnpub

CircleCI

Minimal implementation of publish/subscribe pattern

USAGE

const SubNPub = require("subnpub");
const snp = SubNPub();

const fn1 = str => `1-Called Me with: ${str}`;
const fn2 = str => `2-Called Me with: ${str}`;
const fn3 = str => `3-Called Me with: ${str}`;

// subscribe enforces uniqueness
snp.subscribe(fn1);
// or:
snp.subscribe(fn1, fn2, fn3);
// or:
snp.subscribe([fn1, fn2, fn3]);


snp.publish("test"); // dispatches argument to all subscribers

snp.has(fn1); // true

snp.length(); // 3


snp.unsubscribe(fn1); // removes subscriber
// or:
snp.unsubscribe(fn1, fn2, fn3);
// or:
snp.unsubscribe([fn1, fn2, fn3]);

snp.reset(); // removes all subscribers

Keywords