Event emitter, but for hooks
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:
Widgetprototypeon'show'// do something
Obviously traditional event emitters do not allow that. They force you to do all subscriptions in constructor. That is
- 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
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 =protoon'foo'callspush'proto'var instance = Objectcreateprotoinstanceon'foo'callspush'emitter'instanceemit'foo'callsshouldeql'proto' 'emitter'protoemit'foo'callsshouldeql'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 =var instance = Objectcreateprotoprotoon'foo' listener // that's a bit funkyinstanceemit'foo'
npm install hooks-emitter
component install eldargab/hooks-emitter
var Emitter = require'emitter';var emitter = ;emitteremit'something';
As a mixin:
var Emitter = require'emitter';var user = name: 'tobi' ;Emitteruser;useremit'im a user';
Register a single-shot
removed immediately after it is invoked the
fn, or pass only the
name to remove all handlers for
event with variable option args.
Return an array of callbacks, or an empty array.
Check if this emitter has