majak

1.0.1 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i majak

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • lynxysscz