1.0.1 • Published 7 years ago

majak v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Majak Build Status

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 instance
  • dispatcher: 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 subscriptions

  • Majak.prototype.publish(channel, message) Publishes message to all subscribers in channel
  • Majak.prototype.subscribe(channel, listener) Registers listener callback to a channel
  • Majak.prototype.unsubscribe(channel, listener) Removes callback subscription from channel

TO-DOS

  • This Readme
  • Topic style subscriptions
  • More builtin dispatchers
  • Some more tests