A framework for emitting and receiving custom events.
Methods
# static emit(eventName, dataopt)
Emits the given event for all listeners to respond to with some optional additional data provided.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
eventName |
string
|
The name of the event to emit |
||
data |
Object
|
*
|
<optional> |
{} | The additional data to emit with the event; this will be passed as the only param to each listener's callback function |
Examples
// Emit a "saved" event with no additional data added
emit('saved');
// Emit a "saved" event with profile data attached
emit('saved', { name: 'John' });
# static onEmit(eventName, callback) → {EmitListener}
Creates a new listener for a given event.
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string
|
The name of the event to create the listener for |
callback |
function
|
The function to call whenever the given event is emitted; this function should take in the additional data object as its only param |
The object to interface with the created listener
Example
// Create a listener for when the "saved" event is fired
const savedListener = onEmit('saved', () => console.log('Saved!'));
// In order to cancel it, call the returned object's `cancel` method
savedListener.cancel();
Type Definitions
Object
# EmitListener
An object for interfacing with a create listener.
Properties:
Name | Type | Description |
---|---|---|
cancel |
function
|
The function to call to cancel the given listener |