Event Bus¶
The EventBus class can either be used statically (to access global events) or locally using standard instance syntax (new EventBus).
Installation¶
Terminal
pnpm add @mvps-genesis/event-bus
Example Usage¶
.js¶
import EventBus from '@mvps-genesis/event-bus';
/**
* EXAMPLE FOR GLOBAL (STATIC) EVENT BUS
*/
// create example listener callback function
const listener = ( e ) => {
const data = e.detail;
};
// listen for a global event
EventBus.addEventListener( type, listener [, options] );
// remove listener for a global event
EventBus.removeEventListener( type, listener [, options] );
// dispatch global event. `data` will be accessable via `e.detail` within the listener callback.
EventBus.dispatchEvent( type, { detail: data } );
/**
* EXAMPLE FOR INSTANCED EVENT BUS
*/
const localBus = new EventBus();
localBus.addEventListener( ... );
localBus.removeEventListener( ... );
localBus.dispatchEvent( ... );
Options¶
See standard documentation for EventTarget.