包详细信息

event-emitter

medikoo32mMIT0.3.5

Environment agnostic event emitter

event, events, trigger, observer

自述文件

event-emitter

Environment agnostic event emitter

Installation

$ npm install event-emitter

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Usage

var ee = require('event-emitter');

var MyClass = function () { /* .. */ };
ee(MyClass.prototype); // All instances of MyClass will expose event-emitter interface

var emitter = new MyClass(), listener;

emitter.on('test', listener = function (args) {
  // … react to 'test' event
});

emitter.once('test', function (args) {
  // … react to first 'test' event (invoked only once!)
});

emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked
emitter.emit('test', arg1, arg2/*…args*/); // Only first listener invoked

emitter.off('test', listener);              // Removed first listener
emitter.emit('test', arg1, arg2/*…args*/); // No listeners invoked

Additional utilities

allOff(obj) (event-emitter/all-off)

Removes all listeners from given event emitter object

hasListeners(obj[, name]) (event-emitter/has-listeners)

Whether object has some listeners attached to the object. When name is provided, it checks listeners for specific event name

var emitter = ee();
var hasListeners = require('event-emitter/has-listeners');
var listener = function () {};

hasListeners(emitter); // false

emitter.on('foo', listener);
hasListeners(emitter); // true
hasListeners(emitter, 'foo'); // true
hasListeners(emitter, 'bar'); // false

emitter.off('foo', listener);
hasListeners(emitter, 'foo'); // false

pipe(source, target[, emitMethodName]) (event-emitter/pipe)

Pipes all events from source emitter onto target emitter (all events from source emitter will be emitted also on target emitter, but not other way).
Returns pipe object which exposes pipe.close function. Invoke it to close configured pipe.
It works internally by redefinition of emit method, if in your interface this method is referenced differently, provide its name (or symbol) with third argument.

unify(emitter1, emitter2) (event-emitter/unify)

Unifies event handling for two objects. Events emitted on emitter1 would be also emitted on emitter2, and other way back.
Non reversible.

var eeUnify = require('event-emitter/unify');

var emitter1 = ee(), listener1, listener3;
var emitter2 = ee(), listener2, listener4;

emitter1.on('test', listener1 = function () { });
emitter2.on('test', listener2 = function () { });

emitter1.emit('test'); // Invoked listener1
emitter2.emit('test'); // Invoked listener2

var unify = eeUnify(emitter1, emitter2);

emitter1.emit('test'); // Invoked listener1 and listener2
emitter2.emit('test'); // Invoked listener1 and listener2

emitter1.on('test', listener3 = function () { });
emitter2.on('test', listener4 = function () { });

emitter1.emit('test'); // Invoked listener1, listener2, listener3 and listener4
emitter2.emit('test'); // Invoked listener1, listener2, listener3 and listener4

Tests Build Status

$ npm test

更新日志

v0.3.5 -- 2017.03.15

  • Improve documentation
  • Update dependencies

v0.3.4 -- 2015.10.02

  • Add emitError extension

v0.3.3 -- 2015.01.30

  • Fix reference to module in benchmarks

v0.3.2 -- 2015.01.20

  • Improve documentation
  • Configure lint scripts
  • Fix spelling of LICENSE

v0.3.1 -- 2014.04.25

  • Fix redefinition of emit method in pipe
  • Allow custom emit method name in pipe

v0.3.0 -- 2014.04.24

  • Move out from lib folder
  • Do not expose all utilities on main module
  • Support objects which do not inherit from Object.prototype
  • Improve arguments validation
  • Improve internals
  • Remove Makefile
  • Improve documentation

v0.2.2 -- 2013.06.05

  • unify functionality

v0.2.1 -- 2012.09.21

  • hasListeners module
  • Simplified internal id (improves performance a little), now it starts with underscore (hint it's private). Abstracted it to external module to have it one place
  • Documentation cleanup

v0.2.0 -- 2012.09.19

  • Trashed poor implementation of v0.1 and came up with something solid

Changes:

  • Improved performance
  • Fixed bugs event-emitter is now cross-prototype safe and not affected by unexpected methods attached to Object.prototype
  • Removed support for optional "emitter" argument in emit method, it was cumbersome to use, and should be solved just with event objects

v0.1.5 -- 2012.08.06

  • (maintanance) Do not use descriptors for internal objects, it exposes V8 bugs (only Node v0.6 branch)

v0.1.4 -- 2012.06.13

  • Fix detachment of listeners added with 'once'

v0.1.3 -- 2012.05.28

  • Updated es5-ext to latest version (v0.8)
  • Cleared package.json so it's in npm friendly format

v0.1.2 -- 2012.01.22

  • Support for emitter argument in emit function, this allows some listeners not to be notified about event
  • allOff - removes all listeners from object
  • All methods returns self object
  • Internal fixes
  • Travis CI integration

v0.1.1 -- 2011.08.08

  • Added TAD test suite to devDependencies, configured test commands. Tests can be run with 'make test' or 'npm test'

v0.1.0 -- 2011.08.08 Initial version