UNPKG

evnty

Version:

Async-first, reactive event handling library for complex event flows in browser and Node.js

45 lines (43 loc) 1.17 kB
import { Async } from "./async.js"; export class Signal extends Async { #rx; [Symbol.toStringTag] = 'Signal'; static merge(target, ...signals) { if (target.disposed) { return; } for (const source of signals){ void (async ()=>{ try { const sink = target.sink; for await (const value of source){ if (!sink(value) && target.disposed) { return; } } } catch {} })(); } } constructor(abortSignal){ super(abortSignal); } emit(value) { if (!this.#rx) return false; this.#rx.resolve(value); this.#rx = undefined; return true; } receive() { if (this.disposed) { return Promise.reject(new Error('Disposed')); } this.#rx ??= Promise.withResolvers(); return this.#rx.promise; } dispose() { this.#rx?.reject(new Error('Disposed')); this.#rx = undefined; } } //# sourceMappingURL=signal.js.map