UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

34 lines (33 loc) 860 B
import { isNumber } from "@thi.ng/checks/is-number"; import { dedupe } from "@thi.ng/transducers/dedupe"; import { reducer } from "@thi.ng/transducers/reduce"; import { scan } from "@thi.ng/transducers/scan"; import { fromInterval } from "./interval.js"; import { fromRAF } from "./raf.js"; import { sync } from "./sync.js"; const tween = (src, initial, mix, stop, clock) => sync({ src: { src, _: clock == null ? fromRAF() : isNumber(clock) ? fromInterval(clock) : clock }, closeIn: "first" }).transform( scan( reducer( () => initial, (acc, { src: src2 }) => mix(acc, src2) ) ), dedupe(stop || (() => false)) ); const tweenNumber = (src, init = 0, speed = 0.05, eps = 1e-3, clock) => tween( src, init, (a, b) => a + (b - a) * speed, (a, b) => Math.abs(a - b) < eps, clock ); export { tween, tweenNumber };