Skip to content

A high-performance EventEmitter alternative for Node.js and browser

License

Notifications You must be signed in to change notification settings

foxifyjs/events

Repository files navigation

Events

@foxify/events is an EventEmitter alternative for Node.js and browser that has been optimized to be faster than the native version, (why not?!).

NPM Version TypeScript Version Tested With Jest Pull Requests License Build Status Coverage Status Package Quality Dependencies Status NPM Total Downloads NPM Monthly Downloads Open Issues Closed Issues known vulnerabilities Github Stars Github Forks

This module is API compatible with the EventEmitter that ships by default with Node.js but there are some slight differences:

  • The newListener and removeListener events have been removed as they are useful only in some uncommon use-cases.
  • The setMaxListeners and getMaxListeners methods are not available.
  • Support for custom context for events so there is no need to use bind.

Table of Contents

Installation

npm i @foxify/events

Usage

const { EventEmitter } = require("@foxify/events");

For the API documentation, please follow the official Node.js documentation.

Contextual emits

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);

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details