@foxify/events
is an EventEmitter alternative for Node.js and browser that has been optimized to be faster than the native version, (why not?!).
This module is API compatible with the EventEmitter that ships by default with Node.js but there are some slight differences:
- The
newListener
andremoveListener
events have been removed as they are useful only in some uncommon use-cases. - The
setMaxListeners
andgetMaxListeners
methods are not available. - Support for custom context for events so there is no need to use
bind
.
npm i @foxify/events
const { EventEmitter } = require("@foxify/events");
For the API documentation, please follow the official Node.js documentation.
We've upgraded the API of the on
, once
, addListener
, prependListener
and
prependOnceListener
to accept an extra argument which is the context
or this
value that should be set for the emitted events. This means you no
longer have the overhead of an event that required bind
in order to get a
custom this
value.
const eventEmitter = new EventEmitter();
const context = { foo: "bar" };
function emitted() {
console.log(this === context); // true
}
eventEmitter.on("event:1", emitted, context);
eventEmitter.once("event:2", emitted, context);
eventEmitter.addListener("event:3", emitted, context);
eventEmitter.prependListener("event:4", emitted, context);
eventEmitter.prependOnceListener("event:5", emitted, context);
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Ardalan Amini - Owner/Developer - @ardalanamini
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details