https://github.com/cushjs/events
Tiny event emitter
https://github.com/cushjs/events
eventemitter events
Last synced: about 2 months ago
JSON representation
Tiny event emitter
- Host: GitHub
- URL: https://github.com/cushjs/events
- Owner: cushJS
- License: mit
- Created: 2018-07-16T01:38:32.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-02T17:12:10.000Z (almost 8 years ago)
- Last Synced: 2025-10-06T23:12:31.210Z (8 months ago)
- Topics: eventemitter, events
- Language: JavaScript
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @cush/events v1.1.0
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.
```js
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.