Skip to content

Latest commit

 

History

History
38 lines (27 loc) · 894 Bytes

README.md

File metadata and controls

38 lines (27 loc) · 894 Bytes

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", () => {});