Skip to content

Commit

Permalink
feat(rstream): add sidechainPartitionRAF()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 6, 2021
1 parent fa87168 commit a101626
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/rstream/src/subs/sidechain-partition.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -40,6 +43,21 @@ export const sidechainPartition = <A, B>(
opts?: Partial<SidechainPartitionOpts<B>>
): Subscription<A, A[]> => new SidechainPartition<A, B>(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 = <T>(src: ISubscribable<T>) =>
src
.subscribe(sidechainPartition<T, number>(fromRAF()))
.transform(map(peek));

export class SidechainPartition<T, S> extends ASidechain<T, S, T[]> {
buf: T[];

Expand Down

0 comments on commit a101626

Please sign in to comment.