fxOnChange
Runs a callback only when its argument differs from the last one — uniqAdjacent, for a function.
Lecture
A value polled on a timer or recomputed every frame — an elapsed second
read four times a second, a coin count, a progress fraction — arrives
far more often than it changes. The guard is always the same three
lines: remember the last value, compare, return early.
fxOnChange(f) is those lines once. The first call always
runs; after that a call runs f only when its argument is
not equal to the argument of the last call that did.
Equality is == by default; pass equals to
decide what "changed" means — a rounding bucket, a field of a record, a
case-insensitive string. .reset() forgets the last value,
so the next call runs whatever it carries. Like
fxDebounce it has a method
spelling on the callback: setCoins.fxOnChange().
It is the callback twin of
uniqAdjacent: the same
rule, for a value pushed into a function rather than pulled from a
sequence. Unlike a debounce it adds no delay and drops nothing that
changed. fxdart extension (not part of FxTS).
Demo 1 · A polled value
Demo 2 · equals and reset()
Try it yourself
Exercise: stop writing a coin count that did not change.
uniqAdjacent — the same rule on a sequence ·
fxDebounce — wait for quiet ·
fxThrottle — at most once per window ·
fxMemoizeLast — skip recomputing for the same input