UNPKG

evnty

Version:

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

55 lines (53 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Signal", { enumerable: true, get: function() { return Signal; } }); const _asynccjs = require("./async.cjs"); class Signal extends _asynccjs.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.cjs.map