@adessa-node/pubsub

1.1.0 • Public • Published

PUBSUB

It's a library that provides you a way to communicate your code using publisher/subscriber pattern.

API Methods

  • on
  • subscribe
  • publish
  • notify
  • getChannels
  • removeChannels

on(eventName, listener)

Attaches a listener to a specific event, so when that channel gets a notification, the listener will be triggered.

Arguments

  • eventName (string): the channel which is going to be used to register the listener.
  • listener (Function): the callback to be triggered when channel receives an event.

Returns

  • dispose (Function): a dispose function to be called when this listener needs to be removed.

Example

const dispose = pubsub.on('myEvent', (arg) => {
  console.log('this is the arg i am receiving', arg);
});

dispose(); // this listener won't be here any longer

subscribe

Alias of on

publish(eventName[, ...args])

publishes on a channel an event and sends the arguments.

  pubsub.publish('a channel', 'one');

notify

Alias of publish

getChannels => string[]

  const noop = () => {};
  pubsub.on('channel1', noop);
  pubsub.on('channel2', noop);

  // more lines of code

  const registeredChannels = pubsub.getChannels();
  // => [channel1, channel2]

removeChannels

it will remove every channel listener put into pubsub

Readme

Keywords

none

Package Sidebar

Install

npm i @adessa-node/pubsub

Weekly Downloads

5

Version

1.1.0

License

MIT

Unpacked Size

5.38 kB

Total Files

6

Last publish

Collaborators

  • seigravi
  • nsinha
  • demonofthefall
  • jamalvarez
  • gipsy_martinez
  • worker-8