UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

45 lines (44 loc) 996 B
import { State } from "./api.js"; import { __optsWithID } from "./idgen.js"; import { LOGGER } from "./logger.js"; import { Subscription } from "./subscription.js"; const resolve = (opts) => new Resolver(opts); class Resolver extends Subscription { outstanding = 0; fail; constructor(opts = {}) { super(void 0, __optsWithID("resolve")); this.fail = opts.fail; } next(x) { this.outstanding++; x.then( (y) => { if (this.state < State.DONE) { this.dispatch(y); if (--this.outstanding === 0) { this.done(); } } else { LOGGER.warn(`resolved value in state ${this.state} (${x})`); } }, (e) => { if (this.fail) { this.fail(e); } else { this.error(e); } } ); } done() { if (this.parent && this.parent.getState() === State.DONE && this.outstanding === 0) { super.done(); } } } export { Resolver, resolve };