@denosaurs/event@2.0.2Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
denosaurs/event📆 Strictly typed event emitter with asynciterator support
This package works with Cloudflare Workers, Node.js, Deno, Bun, 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.
Example 1
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
Add Package
deno add jsr:@denosaurs/event
Import symbol
import * as event from "@denosaurs/event";
Import directly with a jsr specifier
import * as event from "jsr:@denosaurs/event";
Add Package
pnpm i jsr:@denosaurs/event
pnpm dlx jsr add @denosaurs/event
Import symbol
import * as event from "@denosaurs/event";
Add Package
yarn add jsr:@denosaurs/event
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";