prototype-emitter
v0.1.0
Published
Define EventEmitter listeners on a class instead of each individual instance
Downloads
3
Readme
prototype-emitter
Define EventEmitter listeners on a class to save on memory consumption in exchange for a little bit of extra work emitting the events themselves.
Suitable when you've got a lot of mostly similar EventEmitters, and you're not emitting enough events for it to be a performance bottleneck.
See also: bindle
Usage
Emitter([BaseClass])
Returns a new prototype-emitter
class. Optionally you can pass a BaseClass
in as the first argument to mixin prototype-emitter
functionality.
Once created or mixed in, you can add event listeners directly on the
class before (or after) creating instances of it. These listeners will
be available on each instance, so calling ee.emit
will trigger these
listeners too.
var PrototypeEmitter = require('prototype-emitter')
var CustomEmitter = PrototypeEmitter()
CustomEmitter.once('data', function() {
console.log('data recieved: ')
})
CustomEmitter.on('data', function(data) {
console.log('>>', data)
})
var emitter = new CustomEmitter
emitter.on('data', function(data) {
console.log(' >', data)
})
emitter.emit('data', 1)
emitter.emit('data', 2)
emitter.emit('data', 3)
// data recieved:
// >> 1
// > 1
// >> 2
// > 2
// >> 3
// > 3
License
MIT. See LICENSE.md for details.