Skip to main content

Benji's personal standard library. Collection of tools for manipulating strings, async iterables, shell scripting, and common data structures.

This package works with Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
76%
Published
3 weeks ago (1.5.1)
class EventStream
implements AsyncIterable<T>

Helper class to handle events and turn them into async iterables

const stream = new EventStream<MouseEvent>()

document.body.addEventListener('click', ev => stream.emit(ev))
wait(10e3).then(() => stream.end())

for await (const event of stream) {
    alert("Clicked!")
}

Constructors

new
EventStream()

Type Parameters

Methods

[Symbol.asyncIterator](): AsyncGenerator<Awaited<T>, void, undefined>
each(callback: (value: T) => void): Promise<void>

Iterate over the event stream, calling a callback for each event.

emit(event: T): void

Dispatches an event. For-await-of listeners of this class instance will recieve this event. Note that once an event is emitted, it cannot be cancelled.

end(): void

Stops the iterator. This terminates for-await-of loops listening for events, and any newly dispatched events will not be sent to the iterator.

once(predicate: (value: T) => boolean): Promise<Awaited<T> | undefined>

Waits for a specific event to be emitted according to a predicate.

Add Package

deno add @mindfulminun/pear

Import symbol

import { EventStream } from "@mindfulminun/pear/core/async";

---- OR ----

Import directly with a jsr specifier

import { EventStream } from "jsr:@mindfulminun/pear/core/async";

Add Package

npx jsr add @mindfulminun/pear

Import symbol

import { EventStream } from "@mindfulminun/pear/core/async";

Add Package

yarn dlx jsr add @mindfulminun/pear

Import symbol

import { EventStream } from "@mindfulminun/pear/core/async";

Add Package

pnpm dlx jsr add @mindfulminun/pear

Import symbol

import { EventStream } from "@mindfulminun/pear/core/async";

Add Package

bunx jsr add @mindfulminun/pear

Import symbol

import { EventStream } from "@mindfulminun/pear/core/async";