on-object
v1.0.0
Published
Register many EventEmitter listeners at once using objects
Downloads
44
Readme
on-object
Register many EventEmitter listeners at once using objects
Usage
npm install on-object
var EventEmitter = require('on-object')
var emitter = new EventEmitter()
emitter.on({
foo: function () {
console.log('foo event emitted')
},
bar: function () {
console.log('bar event emitted')
}
})
emitter.once({ ... })
emitter.removeListener({ ... })
Instantiation
You can just use this module as a replacement for the EventEmitter
class in the events
module:
var EventEmitter = require('on-object')
var emitter = new EventEmitter()
You can also use it to wrap the methods of a pre-existing EventEmitter
:
var EventEmitter = require('events')
var onObject = require('on-object')
var emitter = onObject(new EventEmitter())
Rationale
This module is useful when you find yourself registering many event listeners at once and want the syntax to look a little less noisy.
For example, you might write something like this using the original API:
emitter.on('foo', function () {
console.log('foo')
})
emitter.on('bar', function () {
console.log('bar')
})
emitter.on('bar2', function () {
console.log('bar2')
})
This module will let you do this:
emitter.on({
foo: function () {
console.log('bar')
},
bar: function () {
console.log('bar')
},
bar2: function () {
console.log('bar2')
}
})
Also, you can make it look especially nice with the ES6 object method syntax:
emitter.on({
foo () {
console.log('bar')
},
bar () {
console.log('bar')
},
bar2 () {
console.log('bar2')
}
})