majak
v1.0.1
Published
Simple promise based pub/sub
Downloads
2
Readme
Majak
Small promise based pub-sub/eventEmitter
Usage
Basic usage example
const Majak = require('majak');
const majacek = new Majak();
majacek.subscribe('channel', (message) => {
return `Received message: ${message}`;
});
majacek.subscribe('channel', (message) => {
return new Promise((resolve, reject) => {
global.setTimeout(() => resolve('I made you wait for it'), 2000);
});
});
majacek.publish('channel', 'Lovely message on channel')
.then((responses) => responses.forEach(console.log));
// Outputs [
// 'Received message: Lovely message on channel',
// 'I made you wait for it'
// ]
API
new Majak(dispatcher)
Creates new Majak instancedispatcher: Function with following signature
(subscribers, message) => Promise
- Responsible for dispatching the event to subscribers
- Default is based on
Promise.all
collecting results from subscribers
Majak.prototype.purge()
Removes all subscriptionsMajak.prototype.publish(channel, message)
Publishes message to all subscribers in channelMajak.prototype.subscribe(channel, listener)
Registers listener callback to a channelMajak.prototype.unsubscribe(channel, listener)
Removes callback subscription from channel
TO-DOS
- This Readme
- Topic style subscriptions
- More builtin dispatchers
- Some more tests