diff --git a/packages/rstream/src/from/raf.ts b/packages/rstream/src/from/raf.ts index a40be31905..e0d6b66f26 100644 --- a/packages/rstream/src/from/raf.ts +++ b/packages/rstream/src/from/raf.ts @@ -1,14 +1,29 @@ +import { isNode } from "@thi.ng/checks/is-node"; + import { Stream } from "../stream"; +import { fromInterval } from "./interval"; +/** + * Yields a stream of monotonically increasing counter, + * triggered by a `requestAnimationFrame()` loop. + * Only available in browser environments. In NodeJS, + * this function falls back to `fromInterval(16)`, yielding + * a similar (approximately 60fps) stream. + * + * Subscribers to this stream will be processed during + * that same loop iteration. + */ export function fromRAF() { - return new Stream((o) => { - let i = 0, id, - isActive = true, - loop = () => { - isActive && o.next(i++); - isActive && (id = requestAnimationFrame(loop)); - }; - id = requestAnimationFrame(loop); - return () => (isActive = false, cancelAnimationFrame(id)); - }, `raf-${Stream.NEXT_ID++}`); + return isNode() ? + fromInterval(16) : + new Stream((o) => { + let i = 0, id, + isActive = true, + loop = () => { + isActive && o.next(i++); + isActive && (id = requestAnimationFrame(loop)); + }; + id = requestAnimationFrame(loop); + return () => (isActive = false, cancelAnimationFrame(id)); + }, `raf-${Stream.NEXT_ID++}`); }