Skip to main content
Home

Built and signed on GitHub Actions

📆 Strictly typed event emitter with asynciterator support

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a year ago (2.0.2)

Event

Strictly typed event emitter with Symbol.asyncIterator support.

Events should be defined as a literal object type where the key is the event name, and the value is a tuple with any amount of elements of any type.

The constructor takes an optional argument which defines the maximum amount of listeners per event, which defaults to 10. If this limit is surpassed, an error is thrown.


⚠️ Events must be a type, and can't be an interface due to their design differences.


Examples

Example 1

type Events = {
  foo: [string];
  bar: [number, boolean];
};

class MyClass extends EventEmitter<Events> {}
const MyClassInstance = new MyClass();

function listener(num, bool) {}

// add a listener to the bar event
MyClassInstance.on("bar", listener);

// remove a listener from the bar event
MyClassInstance.off("bar", listener);

// remove all listeners from the bar event
MyClassInstance.off("bar");

// remove all listeners from the event emitter
MyClassInstance.off();

// add a one-time listener to the bar event
MyClassInstance.once("bar", listener);

// on, once, and off are chainable
MyClassInstance.on("bar", listener).off("bar", listener);

// emit the bar event with the wanted data
MyClassInstance.emit("bar", 42, true);

// listen to all events with an async iterator
for await (const event of MyClassInstance) {
  if (event.name === "bar") {
    // event.value is of type [number, boolean]
  }
}

// listen to a specific event with an async iterator
for await (const [num, bool] of MyClassInstance.on("bar")) {
}

// removes all listeners and closes async iterators
MyClassInstance.close("bar");
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@denosaurs/event

Import symbol

import * as event from "@denosaurs/event";
or

Import directly with a jsr specifier

import * as event from "jsr:@denosaurs/event";

Add Package

pnpm i jsr:@denosaurs/event
or (using pnpm 10.8 or older)
pnpm dlx jsr add @denosaurs/event

Import symbol

import * as event from "@denosaurs/event";

Add Package

yarn add jsr:@denosaurs/event
or (using Yarn 4.8 or older)
yarn dlx jsr add @denosaurs/event

Import symbol

import * as event from "@denosaurs/event";

Add Package

vlt install jsr:@denosaurs/event

Import symbol

import * as event from "@denosaurs/event";

Add Package

npx jsr add @denosaurs/event

Import symbol

import * as event from "@denosaurs/event";

Add Package

bunx jsr add @denosaurs/event

Import symbol

import * as event from "@denosaurs/event";