useThrottledHistory
A hook that tracks Observable change history with throttle. A thin wrapper around useHistory with throttleFilter applied — for rapidly changing values like sliders or drag interactions, it records snapshots at fixed intervals instead of on every change.
Sampled slider
Up to 1 commit / 300ms
Drag aggressively and notice how the timeline keeps only periodic checkpoints.
Value: 50
Throttled timeline
Newest snapshot is pinned at the top.
#1
50
import { import useObservable
useObservable, import useThrottledHistory
useThrottledHistory } from "@usels/web";
function function Component(): JSX.Element
Component() { const const slider$: any
slider$ = import useObservable
useObservable(50); // Record at most once every 300ms const { const undo: any
undo, const redo: any
redo, const canUndo$: any
canUndo$ } = import useThrottledHistory
useThrottledHistory(const slider$: any
slider$, { throttle: number
throttle: 300 });
return <JSX.IntrinsicElements.input: DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>
input InputHTMLAttributes<HTMLInputElement>.type?: HTMLInputTypeAttribute | undefined
type="range" InputHTMLAttributes<HTMLInputElement>.value?: string | number | {} | undefined
value={const slider$: any
slider$.any
get()} InputHTMLAttributes<HTMLInputElement>.onChange?: ChangeEventHandler<HTMLInputElement, HTMLInputElement> | undefined
onChange={(e: ChangeEvent<HTMLInputElement, HTMLInputElement>
e) => const slider$: any
slider$.any
set(+e: ChangeEvent<HTMLInputElement, HTMLInputElement>
e.ChangeEvent<HTMLInputElement, HTMLInputElement>.target: EventTarget & HTMLInputElement
target.any
value)} />;}import { createThrottledHistory, observable } from "@usels/web";
function Component() { "use scope" const slider$ = observable(50); const { undo, redo, canUndo$ } = createThrottledHistory(slider$, { throttle: 300 });
return <input type="range" value={slider$.get()} onChange={(e) => slider$.set(+e.target.value)} />;}leading / trailing edges
Section titled “leading / trailing edges”By default both leading and trailing edges fire. Disable either to customize behavior.
import { useObservable, useThrottledHistory } from "@usels/web";
function Component() { const value$ = useObservable(0); // Only record on the trailing edge (end of throttle window) const { undo } = useThrottledHistory(value$, { throttle: 500, leading: false, trailing: true, });}import { createThrottledHistory, observable } from "@usels/web";
function Component() { "use scope" const value$ = observable(0); const { undo } = createThrottledHistory(value$, { throttle: 500, leading: false, trailing: true, });}Combined with capacity
Section titled “Combined with capacity”import { useObservable, useThrottledHistory } from "@usels/web";
function Component() { const position$ = useObservable({ x: 0, y: 0 }); const { undo, redo } = useThrottledHistory(position$, { throttle: 200, capacity: 20, // keep at most 20 throttled snapshots });}import { createThrottledHistory, observable } from "@usels/web";
function Component() { "use scope" const position$ = observable({ x: 0, y: 0 }); const { undo, redo } = createThrottledHistory(position$, { throttle: 200, capacity: 20, });}Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
source$ | Observable<Raw> | - The Observable to track. |
options | ThrottledHistoryOptions<Raw, Serialized> (optional) | - Throttle timing and history configuration. |
ThrottledHistoryOptions
Section titled “ThrottledHistoryOptions”Returns
Section titled “Returns”DataHistoryReturn<Raw, Serialized>
| Name | Type | Description |
|---|---|---|
isTracking$ | ReadonlyObservable<boolean> | Whether auto-tracking is currently active. |
pause | Fn | Stop auto-committing. |
resume | (commitCurrent?: boolean | undefined) => void | Restart auto-committing. If commitCurrent is true, immediately commit current value. |
transaction | (fn: (cancel: Fn) => void) => void | Group multiple mutations into a single history record. Call cancel() inside fn to abort. |
canUndo$ | ReadonlyObservable<boolean> | Whether undo is possible (undoStack is non-empty). |
canRedo$ | ReadonlyObservable<boolean> | Whether redo is possible (redoStack is non-empty). |
history$ | ReadonlyObservable<UseHistoryRecord<Serialized>[]> | Full history array, newest first: [current, previous, ...]. |
last$ | ReadonlyObservable<UseHistoryRecord<Serialized>> | The most recent (current) history record. |
commit | Fn | Record the current source value as a new history point. Clears redo stack. |
undo | Fn | Restore the previous committed value. |
redo | Fn | Re-apply the next value after an undo. |
clear | Fn | Wipe all history and create a fresh initial record from current source. |
reset | Fn | Restore source to the last committed value without modifying stacks. |