hooks-emitter

Event emitter, but for hooks

hooks-emitter

Often event emitters are used not for sending signals to the outside world but as a general purpose hooking API. The difference is that hooks are known in advance, they are not specific to concrete instance. The natural thing to do for such cases is:

Widget.prototype.on('show', function() {
  // do something 
})

Obviously traditional event emitters do not allow that. They force you to do all subscriptions in constructor. That is

  • slow
  • inflexible (I think Super.call(this) is a smell)

This project implements emitter in a different way. It allows you to add subscriptions on prototype and generally you should not think about clobbering or Emitter.call(this) calls. At the moment of writing it can be used as a drop in replacement for component/emitter.

var Emitter = require('hooks-emitter')
var calls = []
 
var proto = new Emitter
 
proto.on('foo', function () {
  calls.push('proto')
})
 
var instance = Object.create(proto)
 
instance.on('foo', function () {
  calls.push('emitter')
})
 
instance.emit('foo')
 
calls.should.eql(['proto', 'emitter'])
 
proto.emit('foo')
 
calls.should.eql(['proto', 'emitter', 'proto'])

Addition of handlers to prototype when child instance was already created is not supported. For such case behaviour is undefined.

var proto = new Emitter
var instance = Object.create(proto)
proto.on('foo', listener) // that's a bit funky 
instance.emit('foo')

Via npm

npm install hooks-emitter

Via component

component install eldargab/hooks-emitter

As an Emitter instance:

var Emitter = require('emitter');
var emitter = new Emitter;
emitter.emit('something');

As a mixin:

var Emitter = require('emitter');
var user = { name: 'tobi' };
Emitter(user);
 
user.emit('im a user');

Register an event handler fn.

Register a single-shot event handler fn, removed immediately after it is invoked the first time.

Remove event handler fn, or pass only the event name to remove all handlers for event.

Emit an event with variable option args.

Return an array of callbacks, or an empty array.

Check if this emitter has event handlers.

MIT