UNPKG

evnty

Version:

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

69 lines (67 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Sequence", { enumerable: true, get: function() { return Sequence; } }); const _asynccjs = require("./async.cjs"); const _signalcjs = require("./signal.cjs"); const _ringbuffercjs = require("./ring-buffer.cjs"); class Sequence extends _asynccjs.Async { #queue; #nextSignal; #sendSignal; [Symbol.toStringTag] = 'Sequence'; static merge(target, ...sequences) { for (const source of sequences){ queueMicrotask(async ()=>{ if (!target.disposed) try { const sink = target.sink; for await (const value of source){ if (!sink(value)) { return; } } } catch {} }); } } constructor(abortSignal){ super(abortSignal); this.#queue = new _ringbuffercjs.RingBuffer(); this.#nextSignal = new _signalcjs.Signal(abortSignal); this.#sendSignal = new _signalcjs.Signal(abortSignal); } get size() { return this.#queue.length; } async reserve(capacity) { while(this.#queue.length > capacity){ await this.#sendSignal; } } emit(value) { const ok = !this.disposed; if (ok) { this.#queue.push(value); } this.#nextSignal.emit(); return ok; } async receive() { while(!this.#queue.length){ await this.#nextSignal; } this.#sendSignal.emit(); return this.#queue.shift(); } dispose() { this.#sendSignal[Symbol.dispose](); this.#nextSignal[Symbol.dispose](); } } //# sourceMappingURL=sequence.cjs.map