podium is an event emitter with support for tags, filters, channels, event update cloning,
arguments spreading, and other features useful when building large scale applications.
While node's native EventEmitter
is strictly focused on maximum performance,
it lacks many features that do not belong in the core implementation. podium is not restricted by
node's performance requirement as it is designed for application layer needs where its overhead
is largely insignificant as implementing these features will have similar cost on top of the native emitter.
const Podium = require('@hapi/podium');
const emitter = new Podium.Podium()
const context = { count: 0 }
emitter.registerEvent({
name: 'event',
channels: ['ch1', 'ch2']
})
const handler1 = function () {
++this.count
console.log(this.count)
};
const handler2 = function () {
this.count = this.count + 2
console.log(this.count)
}
emitter.on({
name: 'event',
channels: ['ch1']
}, handler1, context);
emitter.on({
name: 'event',
channels: ['ch2']
}, handler2, context)
emitter.emit({
name: 'event',
channel: 'ch1'
})
emitter.emit({
name: 'event',
channel: 'ch2'
})
emitter.hasListeners('event') // true
emitter.removeAllListeners('event') // Removes all listeners subscribed to 'event'
The above example uses podium's channel event parameter to restrict the event update to only the specified channel.
First you register the event by calling the registerEvent()
method. Here, you name the event 'event'
and give it channels ['ch1', 'ch2']
.
Next you specify your listener handlers. These will be called when an event is updated. Here you make use of podium's listener context, data that you can bind to your listener handlers.
In this case, handler1
will add 1 to count, which is specified as { count: 0 }
, while handler2
will add 2.
Next you call the on()
method to subscribe a handler to an event. You use the same event name, but two different channels. 'ch1'
will use handler1 and 'ch2'
will use handler2.
Lastly, you use emit()
to emit and event update to the subscribers.
Along with channels, podium allows you to specify other event parameters. Below are more examples:
const Podium = require('@hapi/podium');
const podiumObject = new Podium.Podium();
podiumObject.registerEvent([
{
name: 'event1',
channels: ['ch1', 'ch2', 'ch3', 'ch4'],
},
{
name: 'event2',
channels: ['ch1', 'ch2']
}
]);
const listener1 = (data) => {
console.log('listener1 called', data);
};
const listener2 = (data) => {
console.log('listener2 called', data);
};
podiumObject.on({
name: 'event1',
channels: ['ch1']
}, listener1);
podiumObject.on({
name: 'event1',
channels: ['ch3', 'ch4']
}, listener2);
podiumObject.on({ name: 'event1', channels: 'ch2' }, (data) => { // autonomous function
console.log('auto', data);
});
var arr = [0, 1, 2, 3, 4, 4, 5];
podiumObject.emit({
name: 'event1',
channel: 'ch3'
});
const Podium = require('@hapi/podium');
const podiumObject = new Podium.Podium();
podiumObject.registerEvent([
{
name: 'event1',
channels: ['ch1', 'ch2'],
clone: true
},
{
name: 'event2',
channels: ['ch1', 'ch2']
}
]);
const listener1 = (data) => {
data[0] = 55;
console.log('listener1 called', data);
};
const listener2 = (data) => {
data[0] = 100;
console.log('listener2 called', data);
};
podiumObject.on({
name: 'event1',
channels: ['ch1']
}, listener1);
podiumObject.on({
name: 'event2',
channels: ['ch1']
}, listener2);
var arr = [0, 1, 2, 3, 4, 4, 5];
console.log('initially: ', arr);
podiumObject.emit({
name: 'event1',
channel: 'ch1'
});
console.log('after event1, ch1: ', arr);
podiumObject.emit({
name: 'event2',
channel: 'ch1'
});
console.log('after event2, ch1: ', arr);
const Podium = require('@hapi/podium');
const podiumObject = new Podium.Podium();
podiumObject.registerEvent([
{
name: 'event1',
channels: ['ch1', 'ch2'],
spread: true
},
{
name: 'event2',
channels: ['ch1', 'ch2']
}
]);
const listener1 = (data1, data2, data3, data4) => {
console.log('listener1 called', data1, data2, data3, data4);
};
const listener2 = (data) => {
data[0] = 100;
console.log('listener2 called', data);
};
podiumObject.on({
name: 'event1',
channels: ['ch1']
}, listener1);
podiumObject.on({
name: 'event2',
channels: ['ch1']
}, listener2);
var arr = [0, 1, 2, 3, 4, 4, 5];
console.log('initially: ', arr);
podiumObject.emit({
name: 'event1',
channel: 'ch1'
});
console.log('after event1, ch1: ', arr);
podiumObject.emit({
name: 'event2',
channel: 'ch1'
});
console.log('after event2, ch1: ', arr);
const Podium = require('@hapi/podium');
const podiumObject = new Podium.Podium();
podiumObject.registerEvent([
{
name: 'event1',
channels: ['ch1', 'ch2'],
}
]);
podiumObject.registerEvent([
{
name: 'event1',
channels: ['ch1', 'ch2'],
shared: true
}
]);
const listener2 = (data) => {
console.log('listener2 called', data);
};
podiumObject.on({
name: 'event1',
channels: ['ch1']
}, listener2);
var arr = [0, 1, 2, 3, 4, 4, 5];
podiumObject.emit({
name: 'event1',
channel: 'ch1'
});
const Podium = require('@hapi/podium');
const emitter = new Podium.Podium('test');
const updates = [];
emitter.on('test', (data) => updates.push({ id: 1, data }));
emitter.on({ name: 'test', filter: ['a', 'b'] }, (data) => updates.push({ id: 2, data }));
emitter.on({ name: 'test', filter: 'b' }, (data) => updates.push({ id: 3, data }));
emitter.on({ name: 'test', filter: ['c'] }, (data) => updates.push({ id: 4, data }));
emitter.on({ name: 'test', filter: { tags: ['a', 'b'], all: true } }, (data) => updates.push({ id: 5, data }));
emitter.emit({ name: 'test', tags: 'a' }, 1);
emitter.emit({ name: 'test', tags: ['b'] }, 2);
emitter.emit({ name: 'test', tags: ['d'] }, 3);
emitter.emit({ name: 'test', tags: ['a'] }, 4);
emitter.emit({ name: 'test', tags: ['a', 'b'] }, 5);
emitter.emit('test', 6, () => {
console.log(updates);
});
const Podium = require('@hapi/podium');
const podiumObject = new Podium();
podiumObject.registerEvent('event1');
const listener1 = function(data) {
console.log('listener1 called', data);
};
podiumObject.on({
name: 'event1',
count: 2
}, listener1);
podiumObject.emit('event1', 'emit 1');
podiumObject.emit('event1', 'emit 2');
podiumObject.emit('event1', 'emit 3'); // this wont call listener1
Creates a new podium emitter where:
events
- if present, the value is passed topodium.registerEvent()
.options
- optional configuration options passed topodium.registerEvent()
.
Returns a Podium
object.
Register the specified events and their optional configuration. Events must be registered before
they can be emitted or subscribed to. This is done to detect event name misspelling and invalid
event activities. The events
argument can be:
- an event string.
- an event options object with the following optional keys (unless noted otherwise):
name
- the event name string (required).channels
- a string or array of strings specifying the event channels available. Defaults to no channel restrictions (event updates can specify a channel or not).clone
- iftrue
, thedata
object passed topodium.emit()
is cloned before it is passed to the listeners (unless an override specified by each listener). Defaults tofalse
(data
is passed as-is).spread
- iftrue
, thedata
object passed topodium.emit()
must be an array and thelistener
method is called with each array element passed as a separate argument (unless an override specified by each listener). This should only be used when the emitted data structure is known and predictable. Defaults tofalse
(data
is emitted as a single argument regardless of its type).tags
- iftrue
and thecriteria
object passed topodium.emit()
includestags
, the tags are mapped to an object (where each tag string is the key and the value istrue
) which is appended to the arguments list at the end. A configuration override can be set by each listener. Defaults tofalse
.shared
- iftrue
, the same eventname
can be registered multiple times where the second registration is ignored. Note that if the registration config is changed between registrations, only the first configuration is used. Defaults tofalse
(a duplicate registration will throw an error). For detailed examples of event parameters see here
- an array containing any of the above.
The options
argument is an object with the following optional properties:
validate
- iffalse
, events are not validated. This is only allowed when theevents
value is returned fromPodium.validate()
. Defaults totrue
Emits an event update to all the subscribed listeners where:
criteria
- the event update criteria which must be one of:- the event name string.
- an object with the following optional keys (unless noted otherwise):
name
- the event name string (required).channel
- the channel name string.tags
- a tag string or array of tag strings.
data
- the value emitted to the subscribers.
Behaves identically to podium.emit()
, but also returns an array of the results of all the event listeners that run. The return value is that of Promise.allSettled()
, where each item in the resulting array is { status: 'fulfilled', value }
in the case of a successful handler, or { status: 'rejected', reason }
in the case of a handler that throws.
Please note that system errors such as a TypeError
are not handled specially, and it's recommended to scrutinize any rejections using something like bounce.
Subscribe a handler to an event where:
criteria
- the subscription criteria which must be one of the following:- event name string.
- a criteria object with the following optional keys (unless noted otherwise):
name
- the event name string (required).channels
- a string or array of strings specifying the event channels to subscribe to. If the event registration specified a list of allowed channels, thechannels
array must match the allowed channels. Ifchannels
are specified, event updates without any channel designation will not be included in the subscription. Defaults to no channels filter.clone
- iftrue
, thedata
object passed topodium.emit()
is cloned before it is passed to thelistener
method. Defaults to the event registration option (which defaults tofalse
).count
- a positive integer indicating the number of times thelistener
can be called after which the subscription is automatically removed. A count of1
is the same as callingpodium.once()
. Defaults to no limit.filter
- the event tags (if present) to subscribe to which can be one of the following:- a tag string.
- an array of tag strings.
- an object with the following:
tags
- a tag string or array of tag strings.all
- iftrue
, alltags
must be present for the event update to match the subscription. Defaults tofalse
(at least one matching tag).
spread
- iftrue
, and thedata
object passed topodium.emit()
is an array, thelistener
method is called with each array element passed as a separate argument. This should only be used when the emitted data structure is known and predictable. Defaults to the event registration option (which defaults tofalse
).tags
- iftrue
and thecriteria
object passed topodium.emit()
includestags
, the tags are mapped to an object (where each tag string is the key and the value istrue
) which is appended to the arguments list at the end. Defaults to the event registration option (which defaults tofalse
).
listener
- the handler method set to receive event updates. The function signature depends on thespread
, andtags
options.context
- an object that binds to the listener handler.
Same as podium.on()
.
Same as calling podium.on()
with the count
option set to 1
.
Subscribes to an event by returning a promise that resolves when the event is emitted. criteria
can be specified
in any format supported by podium.on()
, except for the count
option that is set to 1
.
Return a promise that resolves when the event is emitted. The resolution value is an array of emitted arguments.
Subscribes to an event by returning a promise that resolves when the event is emitted count
times. criteria
can only be specified
in the object format supported by podium.on()
and the count
option is required.
Returns a promise that resolves when the event is emitted count
times. The resolution value is an array where each item is an array of emitted arguments.
Removes all listeners subscribed to a given event name matching the provided listener method where:
name
- the event name string.listener
- the function reference provided when subscribed.
Same as podium.off()
.
Returns a reference to the current emitter.
Removes all listeners subscribed to a given event name where:
name
- the event name string.
Returns a reference to the current emitter.
Returns whether an event has any listeners subscribed where:
name
- the event name string.
Returns true
if the event name has any listeners, otherwise false
.
Validates that events are declared in the correct format. Events can be declared
in any of the formats supported by the podium.registerEvent()
method.
When the declaration is valid, the array of events returned can be passed to the podium.registerEvent()
method with validations disabled, otherwise a validation error is thrown.