diff --git a/packages/rstream/src/metastream.ts b/packages/rstream/src/metastream.ts index 1651d61c76..8700783a58 100644 --- a/packages/rstream/src/metastream.ts +++ b/packages/rstream/src/metastream.ts @@ -49,7 +49,7 @@ export interface MetaStreamOpts extends CommonOpts { * // transform each received odd number into a stream * // producing 3 copies of that number in the metastream * // even numbers are ignored - * a = metastream( + * a = metaStream( * (x) => (x & 1) * ? fromIterable(tx.repeat(x, 3), { delay: 100 }) * : null diff --git a/packages/rstream/src/raf.ts b/packages/rstream/src/raf.ts index 4713f63841..afd70cd4c8 100644 --- a/packages/rstream/src/raf.ts +++ b/packages/rstream/src/raf.ts @@ -1,4 +1,5 @@ import { isNode } from "@thi.ng/checks/is-node"; +import { isNumber } from "@thi.ng/checks/is-number"; import type { CommonOpts } from "./api.js"; import { __optsWithID } from "./idgen.js"; import { fromInterval } from "./interval.js"; @@ -13,6 +14,14 @@ export interface FromRAFOpts extends CommonOpts { * @defaultValue false */ timestamp: boolean; + /** + * Only used if {@link FromRAFOpts.timestamp} is enabled. If given as + * number, the value will be subtracted from all emitted timestamps. If this + * option is set to true, the timestamps will be automatically zero-adjusted + * such that the first emitted value will be zero. If undefined (default), + * the browser supplied timestamps will be used as is. + */ + t0: number | boolean; } /** @@ -34,7 +43,12 @@ export const fromRAF = (opts: Partial = {}) => : stream((stream) => { let i = 0; let isActive = true; + let t0 = isNumber(opts.t0) ? opts.t0 : undefined; const loop: FrameRequestCallback = (time) => { + if (opts.timestamp && opts.t0) { + if (t0 === undefined) t0 = time; + time -= t0; + } isActive && stream.next(opts.timestamp ? time : i++); isActive && (id = requestAnimationFrame(loop)); };