diff --git a/packages/rstream/src/subs/sidechain-partition.ts b/packages/rstream/src/subs/sidechain-partition.ts index 0777b58f36..147d62d0cc 100644 --- a/packages/rstream/src/subs/sidechain-partition.ts +++ b/packages/rstream/src/subs/sidechain-partition.ts @@ -1,5 +1,8 @@ import type { Predicate } from "@thi.ng/api"; +import { peek } from "@thi.ng/arrays"; +import { map } from "@thi.ng/transducers"; import { CommonOpts, ISubscribable, State } from "../api"; +import { fromRAF } from "../from/raf"; import type { Subscription } from "../subscription"; import { optsWithID } from "../utils/idgen"; import { ASidechain } from "./asidechain"; @@ -40,6 +43,21 @@ export const sidechainPartition = ( opts?: Partial> ): Subscription => new SidechainPartition(side, opts); +/** + * Syntax sugar for one of most common {@link sidechainPartition} use cases, to + * synchronize downstream processing w/ `requestAnimationFrame()`. The returned + * subscription debounces any high frequency intra-frame input values and (if + * any present), passes only most recent one downstream *during* next RAF event + * processing. + * + * @param src + * @returns + */ +export const sidechainPartitionRAF = (src: ISubscribable) => + src + .subscribe(sidechainPartition(fromRAF())) + .transform(map(peek)); + export class SidechainPartition extends ASidechain { buf: T[];