add-listener-first

1.0.1 • Public • Published

add-listener-first

Build Status Code Climate Test Coverage Issue Count

A module which allows you to add a listener to the beginning of an EventEmitter's list of handled events. Barring future displacement, this ensures that your listener will be called before all others.

Why?

If your application does any top-level logging, you may be hooking into process.on([SIGNAL] or process.on("UncaughtException". Often times, listeners for these signals include process.exit to gracefully shutdown the application with a meaningful error code. If these listeners are tirggered prior to your logging logic, your application will shutdown before your logging happens. This module ensures that your listener will be executed first, barring future displacement.

Example

const addListenerFirst = require('../index.js')
const EventEmitter = require('events')
 
function listenerOne () {
  console.log('Listener 1: But I was added first!')
}
 
function listenerTwo () {
  console.log('Listener 2: I was run first!')
}
 
const emitter = new EventEmitter()
emitter.on('WOO!', listenerOne)
addListenerFirst(emitter, 'WOO!', listenerTwo)
emitter.emit('WOO!')
Listener 2: I was run first!
Listener 1: But I was added first!

Dependencies (0)

    Dev Dependencies (6)

    Package Sidebar

    Install

    npm i add-listener-first

    Weekly Downloads

    2

    Version

    1.0.1

    License

    MIT

    Last publish

    Collaborators

    • retrohack3r