Skip to main content

Built and signed on GitHub Actions

📆 Strictly typed event emitter with asynciterator support

This package works with Bun, Cloudflare Workers, Node.js, Deno, Browsers
JSR Score
100%
Published
4 months 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
View transparency log

Add Package

deno add @denosaurs/event

Import symbol

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

Add Package

npx jsr add @denosaurs/event

Import symbol

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

Add Package

yarn dlx jsr add @denosaurs/event

Import symbol

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

Add Package

pnpm dlx jsr add @denosaurs/event

Import symbol

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

Add Package

bunx jsr add @denosaurs/event

Import symbol

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