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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Events

@foxify/events is a EventEmitter alternative for Node.js and browser that has been optimized for better performance compared to the native version.

Build Status Coverage Status NPM Version npm bundle size (minified) npm bundle size (minified + gzip) NPM Monthly Downloads NPM Total Downloads Dependencies Status Open Issues Pull Requests License

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.
  • Support for strict events in TypeScript.

Table of Contents

Installation

npm i @foxify/events

Usage

JavaScript:

const EventEmitter = require("@foxify/events").default;

TypeScript:

import EventEmitter from "@foxify/events";

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

Strict events

"error" event is always defined by default because of its different behavior

first create events type (optional)

type Events = {
  foo: (bar: string) => void;
  withContextEnforcement: (this: number, bar: number) => void;
}

then create a new direct/extended instance

const eventEmitter = new EventEmitter<Events>();
class Emitter extends EventEmitter<Events> {
}

const eventEmitter = new Emitter();

then start emitting & listening to events

// Works just fine. so don't worry about "ImplicitAny" config, since type of "bar" is defined as "string"
eventEmitter.on("foo", bar => 1);

// This works fine as well
eventEmitter.on("withContextEnforcement", function (bar) {
  return this + bar;
}, 1);

// Throws an error (TS compile time), since this event requires the "bar" argument of type "string"
eventEmitter.emit("foo");

// Works just fine
eventEmitter.emit("foo", "bar");

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 listener() {
  console.log(this === context); // true
}

eventEmitter.on("event:1", listener, context);
eventEmitter.once("event:2", listener, context);
eventEmitter.addListener("event:3", listener, context);
eventEmitter.prependListener("event:4", listener, context);
eventEmitter.prependOnceListener("event:5", listener, context);

Benchmarks

npm run benchmarks

Tests

npm test

Coverage

npm run test:coverage

Versioning

We use SemVer for versioning. For the versions available, see the releases 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