prototype-emitter

0.1.0 • Public • Published

prototype-emitter Flattr this!experimental

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

prototype-emitter

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.

Package Sidebar

Install

npm i prototype-emitter

Weekly Downloads

2

Version

0.1.0

License

MIT

Last publish

Collaborators

  • hughsk
  • timoxley