Skip to content

Strongly typed event emitter that runs in node and in the browser.

Notifications You must be signed in to change notification settings

hornta/event-emitter

Repository files navigation

Event emitter

npm (scoped) npm bundle size (scoped)

  • Works in Node.JS and the browser
  • Zero dependencies
  • ESM, CJS & UMD
  • Strongly typed events
  • Fully tested

Install

npm install @hornta/event-emitter

Usage

import { EventEmitter } from "@hornta/event-emitter";

interface Events {
  message: (message: string) => void;
}

const emitter = new EventEmitter<Events>();

emitter.addListener("message", (message) => {
  console.log(message);
});

emitter.emit("message", "👋 🌍!"); // Will log '👋 🌍!' to the console

/* These will trigger TypeScript compilation errors */
emitter.emit("unknown");
emitter.emit("message", { message: "👋 🌍!" });
emitter.addListener("unknown", () => {});