@adessa-node/pubsub

a library to communicate your code using publisher/subscriber pattern

Usage no npm install needed!

<script type="module">
  import adessaNodePubsub from 'https://cdn.skypack.dev/@adessa-node/pubsub';
</script>

README

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