ESMitter (read: E-S-Emitter; a pun on ESM + emitter) is an event emitter compatible with Node.js, Deno and modern browsers. It is a fork of EventEmitter3, but natively TypeScript, ESM-only and with more modern tooling. The complete codebase has been converted to TypeScript and EcmaScript module syntax (ESM). ESMitter is currently less performant than EventEmitter3, but still fast with millions of operations per second.
npm install --save esm-itter
After installation the only thing you need to do is import the module and use it as a parent class. One of the main features of ESMitter (beside being ESM-only) is that it is strongly typed, so you will need to provide type definitions for your events. Here is a simple example:
import { ESMitter, type ESMitterEvent } from "esm-itter";
class MyClass extends ESMitter<{
success: ESMitterEvent<[{ foo: string; bar: string }]>;
error: ESMitterEvent<[number, string]>;
}> {}
const instance = new MyClass();
// Attach event listener
instance.on("success", ({ foo, bar }) => {
/* [...] */
});
deno add jsr:@tillsanders/esm-itter
After installation the only thing you need to do is import the module and use it as a parent class. One of the main features of ESMitter (beside being ESM-only) is that it is strongly typed, so you will need to provide type definitions for your events. Here is a simple example:
import { ESMitter, type ESMitterEvent } from "@tillsanders/esm-itter";
class MyClass extends ESMitter<{
success: ESMitterEvent<[{ foo: string; bar: string }]>;
error: ESMitterEvent<[number, string]>;
}> {}
const instance = new MyClass();
// Attach event listener
instance.on("success", ({ foo, bar }) => {
/* [...] */
});
This module is well tested. You can run:
-
npm test
to run the tests under Node.js. -
npm run coverage
to run the tests and generate a coverage report.
We also have a set of benchmarks to compare EventEmitter3 with some available
alternatives. To run the benchmarks run npm run benchmark
.
Tests and benchmarks are not included in the npm package. If you want to play
with them you have to clone the GitHub repository.
Note that you will have to run an additional npm i
in the benchmarks folder
before npm run benchmark
.
- The
listenerCount()
method requires an event name as the first argument and will thus not return the number of listeners for all events combined.
This library has been tested using unit tests and has achieved excellent code coverage. It is being used in production and is considered stable. Contributions are welcome to improve any aspect of the library, including performance, documentation, and features.
This module is licensed under MIT.
It is a fork of the wonderful EventEmitter3 by Arnout Kazemier and has been converted to TypeScript and ESM by Till Sanders. For ES3 and CommonJS support, as well as superior performance, please use EventEmitter3.