UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

33 lines (32 loc) 795 B
import { SEMAPHORE } from "@thi.ng/api/api"; import { State } from "./api.js"; import { ASidechain } from "./asidechain.js"; import { __optsWithID } from "./idgen.js"; const sidechainTrigger = (src, side, opts) => src.subscribe(new SidechainTrigger(side, opts)); class SidechainTrigger extends ASidechain { buf = SEMAPHORE; constructor(side, opts) { opts = __optsWithID("sidetrigger", opts); super(opts); const pred = opts.pred || (() => true); this.sideSub = side.subscribe({ next: (x) => { if (this.buf !== SEMAPHORE && pred(x)) { this.dispatch(this.buf); } }, done: () => this.done() }); } next(x) { if (this.state < State.DONE) { this.buf = x; } } } export { SidechainTrigger, sidechainTrigger };