Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

extends node.js event emitter class so that the listener methods return destroyables.

License

Notifications You must be signed in to change notification settings

hkan/node-destroyable-listener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

node-destroyable-listener

The package provides a simplist API to extend Node.js's event listener creators. What eventually changes is that creator functions will return an Object with a destroy method so that you don't have to have a reference to event listener's function in order to remove the listener.

That text ☝️ sounds confusing? Check the comparison below.

Removing a listener with the conventional way:

const Events = require('events')

// Create your event bus as usual.
let eb = Events()

// Assign your listener to a variable.
let foo = (bar) => {
    // stuff...
}

// OR
// Create a named function.
function foo(bar) {
    // stuff...
}

// Start listening to an event.
eb.addListener('change', foo)   // eb.on() is an alias for eb.addListener()

// You need to have the reference to `foo` function to do this:
eb.removeListener('change', foo)

Removing a listener with this package:

const Events = require('events')

// Create your event bus as usual.
let eb = Events()

// Start listening to an event.
let changeListener = eb.addListener('change', bar => {

})

// You *do not* need the reference to `foo` function. Simply call the destroy method on the returned listener object.
changeListener.destroy()

Installing

$ npm install destroyable-listener

Contribution

Any pull requests, ideas or issue creations are welcome.

Also, for god's sake, someone rewrite that confusing intro text up there. I don't know how else to put it. 😅

About

extends node.js event emitter class so that the listener methods return destroyables.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published