UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

27 lines (26 loc) 810 B
import { isNode } from "@thi.ng/checks/is-node"; import { isNumber } from "@thi.ng/checks/is-number"; import { __optsWithID } from "./idgen.js"; import { fromInterval } from "./interval.js"; import { stream } from "./stream.js"; const fromRAF = (opts = {}) => isNode() ? fromInterval(16, opts) : stream((stream2) => { let i = 0; let isActive = true; let t0 = isNumber(opts.t0) ? opts.t0 : void 0; const loop = (time) => { if (opts.timestamp && opts.t0) { if (t0 === void 0) t0 = time; time -= t0; } isActive && stream2.next(opts.timestamp ? time : i++); isActive && (id = requestAnimationFrame(loop)); }; let id = requestAnimationFrame(loop); return () => { isActive = false; cancelAnimationFrame(id); }; }, __optsWithID("raf", opts)); export { fromRAF };