UNPKG

evnty

Version:

0-Deps, simple, fast, for browser and node js reactive anonymous event library

77 lines (75 loc) 2.13 kB
import { Callable } from "./callable.js"; import { Signal } from "./signal.js"; export class Sequence extends Callable { abortSignal; sequence; nextSignal; constructor(abortSignal){ super((value)=>{ if (this.abortSignal?.aborted) { this.nextSignal(false); return false; } else { this.sequence.push(value); if (this.sequence.length === 1) { this.nextSignal(true); } return true; } }), this.abortSignal = abortSignal; this.sequence = []; this.nextSignal = new Signal(this.abortSignal); this.abortSignal?.addEventListener('abort', ()=>{ this.nextSignal(false); }, { once: true }); } get [Symbol.toStringTag]() { return `Sequence(${this.abortSignal?.aborted ? 'stopped' : 'active'})`; } get promise() { return this.next(); } async next() { if (!this.sequence.length) { await this.nextSignal; } return this.sequence.shift(); } catch(onrejected) { return this.promise.catch(onrejected); } finally(onfinally) { return this.promise.finally(onfinally); } then(onfulfilled, onrejected) { return this.promise.then(onfulfilled, onrejected); } [Symbol.asyncIterator]() { return { next: async ()=>{ try { const value = await this; return { value, done: false }; } catch { return { value: undefined, done: true }; } }, return: ()=>{ this.nextSignal(false); return Promise.resolve({ value: undefined, done: true }); } }; } } //# sourceMappingURL=sequence.js.map