An event bus system for registering, unregistering and triggering events.
npm install --save @uttori/event-dispatcherconst hooks = new EventDispatcher();
hooks.on('update', callback);
hooks.dispatch('update', { data }, this);
hooks.off('update', callback);An event bus system for registering, unregistering and triggering events.
Kind: global class
Properties
| Name | Type | Description |
|---|---|---|
| events | Record.<string, UttoriEvent> |
The collection of events to listen for. |
- EventDispatcher
- new EventDispatcher()
- instance
- .events :
Record.<string, UttoriEvent> - .validate(label, data, [context]) ⇒
Promise.<boolean> - .filter(label, data, [context]) ⇒
Promise.<T> - .dispatch(label, data, [context])
- .fetch(label, data, [context]) ⇒
Promise.<Array.<T>> - .on(label, callback)
- .once(label, callback)
- .off(label, callback)
- .events :
- static
Creates a new EventDispatcher instance.
Example (new EventDispatcher())
const bus = new EventDispatcher();
bus.on('update', callback);
bus.dispatch('update', { data }, { context });
bus.off('update', callback);The collection of events to listen for.
Kind: instance property of EventDispatcher
Fires off an event with passed in data and context for a given label.
Kind: instance method of EventDispatcher
Returns: Promise.<boolean> - The conclusion of the spam checks, true being it is spam, false meaning it is clean.
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| data | any |
Data to be used, updated, or modified by event callbacks. |
| [context] | object |
Context to help with updating or modification of the data. |
Example
is_spam = await bus.validate('check-for-spam', { data }, this);Fires off an event with passed in data and context for a given label.
Kind: instance method of EventDispatcher
Returns: Promise.<T> - The original input data, either modified or untouched.
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| data | any |
Data to be used, updated, or modified by event callbacks. |
| [context] | object |
Context to help with updating or modification of the data. |
Example
output = await bus.filter('loaded', { data }, this);Fires off an event with passed in data and context for a given label.
Kind: instance method of EventDispatcher
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| data | any |
Data to be used, updated, or modified by event callbacks. |
| [context] | object |
Context to help with updating or modification of the data. |
Example
bus.dispatch('loaded', { data }, this);Fires off an event with passed in data and context for a given label and returns an array of the results.
Kind: instance method of EventDispatcher
Returns: Promise.<Array.<T>> - An array of the results.
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| data | any |
Data to be used by event callbacks. |
| [context] | object |
Context to help with updating or modification of the data. |
Example
popular = await bus.fetch('popular-documents', { limit: 10 }, this);Add a function to an event that will be called when the label is dispatched. If no label is found, one is created.
Kind: instance method of EventDispatcher
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| callback | UttoriEventCallback.<any, any> |
Function to be called when the event is fired. |
Example
bus.on('loaded', callback);Add a function to an event that will be called only once when the label is dispatched.
Uses the EventDispatcher.on method with a function wrapped to call off on use.
Kind: instance method of EventDispatcher
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| callback | UttoriEventCallback.<any, any> |
Function to be called when the event is fired. |
Example
bus.once('one-time-process', callback);Remove a function from an event.
Kind: instance method of EventDispatcher
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
| callback | UttoriEventCallback.<any, any> |
Function to be removed. |
Example
bus.off('loaded', callback);Verifies an event label.
Kind: static method of EventDispatcher
| Param | Type | Description |
|---|---|---|
| label | string |
The human readable identifier of the event. |
Example
EventDispatcher.check('event'); // No Error
EventDispatcher.check(1); // Throws ErrorTo run the test suite, first install the dependencies, then run npm test:
npm install
npm test
DEBUG=Uttori* npm test