1.0.1 • Published 4 years ago

@definitylabs/dispatcher.js v1.0.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
4 years ago

dispatcher.js

This package is responsible for managing events.

Install

npm install @definitylabs/dispatcher.js

Register

Start listening events.

Register for single event

const {Dispatcher} = require('@definitylabs/dispatcher.js');

const MY_EVENT = 'my-event-name';

function subscriber(eventType, object) {
  console.log(eventType, object);
}

Dispatcher.register(subscriber, MY_EVENT);

Register for multiple events

The subscriber will be triggered in case of any of these events will be dispatch.

const Events = {
  EVENT_ONE: 'eventOne',
  EVENT_TWO: 'eventTwo'
};

Dispatcher.register(subscriber, [Events.EVENT_ONE, Events.EVENT_TWO]);

Register for all events

The subscriber will be triggered in case of any events will be dispatch.

Dispatcher.register(subscriber);

Register for combined events

The subscriber will be triggered only after all events will be dispatch.

const {Dispatch, Collector} = require('@definitylabs/dispatcher.js');

const collector = new Collector(subscriber, [Events.EVENT_ONE, Events.EVENT_TWO]);

Dispatch.reigster(collector, collector.events());

A timeout can be specified in case one of the events is missing.

const {Dispatch, Collector} = require('@definitylabs/dispatcher.js');

const collector = new Collector(subscriber, [Events.EVENT_ONE, Events.EVENT_TWO]).timeout(1000); // 1s timeout

Dispatch.reigster(collector, collector.events());

Dispatch

Dispatch event with (or without) a payload and it will trigger all registered subscribers for that particular event.

// dispatch with payload
const payload = {...}; // it can be any object or value
Dispatcher.dispatch(Events.EVENT_ONE, payload);

// dispatch without payload
Dispatcher.dispatch(Events.EVENT_ONE);

Unsubscribe

Stop listening events.

// unregister for single event
Dispatcher.unregister(subscriber, MY_EVENT);

// unregister for multiple events
Dispatcher.unregister(subscriber, [Events.EVENT_ONE, Events.EVENT_TWO]);

// unregister for all events
Dispatcher.unregister(subscriber);
1.0.1

4 years ago

1.0.0

4 years ago