@flowio/events
v0.1.18
Published
Event Bus (Pub/Sub)
Downloads
97
Keywords
Readme
@flowio/events
pub / sub event library for the browser. Provides a static reference to an event emitter so that the same channel can be used across files and other libraries.
API
ChannelEventEmitter(channel)
Creates a reference to an event emitter by a channel. A channel is a key that refers to the an internal wolfy87-eventemitter
instance. Channels are referenced statically so multiple instances of ChannelEventEmitter will use the same EventEmitter instance internally. Triggers on channelEventEmitterA can be received by channelEventEmitterB when the same channel
value is used.
Parameters
Parameter | Type | Default | Description --------- | ------ | ------- | ------------------ channel | string | default | The event emitter channel to use. Can be reused across multiple instances of ChannelEventEmitter.
Properties
Parameter | Type | Default | Description
--------- | ------ | ------- | ------------------
emitter | Object | | Internal wolfy87-eventemitter
EventEmitter instance
channel | string | default | The event emitter channel to use. Can be reused across multiple instances of ChannelEventEmitter.
Methods
on(event, callback)
Subscribe to an event and execute the callback when it is triggered.
const events = new ChannelEventEmitter();
events.on('myEvent', (data) => console.log('My event data!', data));
once(event, callback)
Subscribe to an event and execute the callback once and then remove it.
const events = new ChannelEventEmitter();
events.once('myEvent', (data) => console.log('My event data!', data));
trigger('myEvent', 'Hello!');
// -> My event data! Hello!
trigger('myEvent', 'Hello!');
// Nothing logged.
off(event, callback)
Remove a callback subscription to an event.
const events = new ChannelEventEmitter();
const myCallback = (data) => console.log('My event data!', data);
events.on('myEvent', myCallback);
trigger('myEvent', 'Hello!');
// -> My event data! Hello!
off('myEvent', myCallback);
trigger('myEvent', 'Hello!');
// Nothing logged.
trigger(event, data)
Trigger an event and pass the provided data to the callback of any subscribers.
const events = new ChannelEventEmitter();
events.on('myEvent', (data) => console.log('My event data!', data));
trigger('myEvent', 'Hello!');
// -> My event data! Hello!
removeAll()
Remove all subscriptions across all events
TriggerOnceEvents({ channel = 'default', triggerOnceEvents = [] } = {})
Creates an event emitter that will automatically execute callbacks if an event has been triggered already.
Parameters
Parameter | Type | Default | Description ----------------- | ------------- | ------- | ------------------ channel | string | default | The event emitter channel to use. Can be reused across multiple instances of ChannelEventEmitter. triggerOnceEvents | array(String) | [] | Events that can only be triggered once and will automatically execute callbacks of any future subscriptions
Properties
Property | Type | Default | Description
------------------- | ------------- | ------- | ------------------
emitter | string | default | Internal ChannelEventEmitter
instance.
triggerOnceEvents | array(String) | [] | Events that can only be triggered once and will automatically execute callbacks of any future subscriptions
previouslyTriggered | array(Object) | [] | Events that have already been triggered on this instance of TriggerOnceEvents. Each entry is an object with the shape { key, data }
Methods
on(event, callback)
Subscribe to an event.
const events = new ChannelEventEmitter();
events.on('myEvent', (data) => console.log('Data from my event!', data));
trigger(event, data)
Trigger an event.
const events = new ChannelEventEmitter();
events.trigger('myEvent', 'Hello World');
getPreviouslyTriggeredEvent(event)
Get previously triggered event
const events = new ChannelEventEmitter();
events.trigger('myEvent', 'Hello World');
console.log(events.getPreviouslyTriggeredEvent('myEvent');
// -> { key: 'myEvent', data: 'Hello World' }
resetPreviouslyTriggered()
Reset list of previously triggered events
const events = new ChannelEventEmitter();
events.trigger('myEvent', 'Hello World');
console.log(events.getPreviouslyTriggeredEvent('myEvent');
// -> { key: 'myEvent', data: 'Hello World' }
events.resetPreviouslyTriggered();
console.log(events.previouslyTriggered);
// -> []