@cush/events
Tiny event emitter
Last updated 2 years ago by aleclarson .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @cush/events 
SYNC missed versions from official npm registry.

@cush/events v1.0.2

Tiny event emitter

Supports non-invasive subclassing. The listener cache is non-enumerable and assigned via an internal Symbol value.

Events are emitted asynchronously by default. Under the hood, a promise is used to queue a microtask.

const Emitter = require('@cush/events');

// An initial set of listeners can be passed.
const ee = new Emitter({
  foo: () => {},  // values can be functions
  bar: [],        // or arrays of functions
});

// Add a listener and return it.
const foo = ee.on('foo', () => {});

// Remove a listener.
ee.off('foo', foo);

// Emit as many arguments as you like.
ee.emit('foo', 1, 2);

// Emit synchronously.
ee.emitSync('foo');

// Emit synchronously by default.
ee.emit = ee.emitSync;

// Emit asynchronously (the default behavior).
ee.emitAsync('foo');

// Remove all listeners for an event.
ee.clear('foo');

// Remove all listeners for all events.
ee.clear();

// Count the number of listeners for an event.
ee.listenerCount('foo'); // => 0

// Access the listener cache. (useful for subclassing)
ee[Emitter.ev];

Debugging

Run export DEBUG="events" in your terminal (or call Emitter.debug) to make every instance emit an emit event whenever emitSync or emitAsync is called. The Arguments object is passed, which you can alter to change the event being debugged.

Pass nothing to Emitter.debug to enable debugging for all emitters. If you pass an emitter, debugging will be enabled only for it.

Current Tags

  • 1.0.2                                ...           latest (2 years ago)

5 Versions

  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (2)

Copyright 2014 - 2016 © taobao.org |