podium

Event emitter with async features.

Latest Version: 5.0.1
hapi-family
Installation:

npm: npm install @hapi/podium

yarn: yarn add @hapi/podium

Module Status:
Version License Node Dependencies CI
5.0.1
hapi helmet github logo
BSD 16, 18, 20 Dependency Status Build Status
4.1.3
hapi helmet github logo
BSD 16, 18, 20 Dependency Status Build Status

Introduction

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.

Example

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.