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.
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)
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()
$ npm install destroyable-listener
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. 😅