UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

45 lines (44 loc) 1.15 kB
import { peek } from "@thi.ng/arrays/peek"; import { map } from "@thi.ng/transducers/map"; import { State } from "./api.js"; import { ASidechain } from "./asidechain.js"; import { __optsWithID } from "./idgen.js"; import { fromRAF } from "./raf.js"; const sidechainPartition = (src, side, opts) => src.subscribe(new SidechainPartition(side, opts)); const sidechainPartitionRAF = (src) => sidechainPartition(src, fromRAF()).transform(map(peek)); class SidechainPartition extends ASidechain { buf; constructor(side, opts) { opts = __optsWithID("sidepart", opts); super(opts); const pred = opts.pred || (() => true); this.buf = []; this.sideSub = side.subscribe({ next: (x) => { if (this.buf.length && pred(x)) { this.dispatch(this.buf); this.buf = []; } }, done: () => { if (this.buf.length) { this.dispatch(this.buf); } this.done(); delete this.buf; } }); } next(x) { if (this.state < State.DONE) { this.buf.push(x); } } } export { SidechainPartition, sidechainPartition, sidechainPartitionRAF };