@aptsketch/event-broker

1.2.2 • Public • Published

Purpose

Provide a consistent manner in using the CustomEvent in DOM for message passing.

Usage

Example

import * as eventBroker from '@aptsketch/event-broker';

// Register an event listener
// Event are group by the 'event-group' namespace and identified by 'event-name'
// Behind the scene the event dom string is event-group.event-name
eventBroker.register('event-group', 'event-name', (payload) => {
    console.log('Event fired');
    return 'Event successfully fired';
});

// Emit an event
// Allows you to pass in data to the listening handler
// Using the example above, payload will be MyPayloadData
//
// Callback handler will be executed after the event is processed
//      reply: contain the return value of the listener handler. null if nothing.
//      err: contain the error if any error occur. null if nothing.
eventBroker.emit('event-group', 'event-name', MyPayloadData, (reply, err) => {
    console.log('Callback after event was handled');
});

const data = await eventBroker.emit('event-group', 'event-name', MyPayloadData);

// Unregister all handler associated with the event-group
eventBroker.unregisterByGroup('event-group');

// Unregister all event handler associated with the event-group and event-name
eventBroker.unregisterByName('event-group', 'event-name');

// Unregister a specific handler associated with the event-group and event-name
eventBroker.unregisterByName('event-group', 'event-name', handler);

Readme

Keywords

Package Sidebar

Install

npm i @aptsketch/event-broker

Weekly Downloads

108

Version

1.2.2

License

MIT

Unpacked Size

23.1 kB

Total Files

3

Last publish

Collaborators

  • tycoona
  • forex