UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

42 lines (41 loc) 998 B
import { isNode } from "@thi.ng/checks/is-node"; import { State } from "./api.js"; import { __optsWithID } from "./idgen.js"; import { Subscription } from "./subscription.js"; const syncRAF = (src, opts) => src.subscribe(new SyncRAF(__optsWithID(`syncraf-${src.id}`, opts))); class SyncRAF extends Subscription { queued; raf; constructor(opts) { super(void 0, opts); } next(x) { if (this.state >= State.DONE) return; this.queued = x; if (!this.raf) { const update = () => { if (this.state < State.DONE) super.next(this.queued); this._clean(); }; this.raf = isNode() ? setTimeout(update, 16) : requestAnimationFrame(update); } } done() { this._clean(); super.done(); } error(e) { this._clean(); return super.error(e); } _clean() { if (this.raf) { isNode() ? clearTimeout(this.raf) : cancelAnimationFrame(this.raf); } this.raf = this.queued = void 0; } } export { SyncRAF, syncRAF };