Skip to main content

DelayedIndicator

View as Markdown

Delays showing a transient value long enough to avoid flicker, and once shown, keeps it visible for a minimum duration.

With the defaults, a spinner appears only when a request takes longer than 150 ms and stays on screen for at least 300 ms. Pass every new value to set(). Values that isTransient marks are held back before they appear, every other value passes through immediately. The value can be anything, not only a loading flag.

Call indicator.destroy() on teardown to cancel a pending timer.

The options of a DelayedIndicator.

NameDescriptionRequiredDefault
showDelayMsnumber
How long a transient value must persist before it is shown at all. Must be finite and >= 0.
No
150
minVisibleMsnumber
Once shown, how long a transient value stays current before a settled value can replace it. Must be finite and >= 0.
No
300
isTransient(value: T) => boolean
Marks which values are transient, e.g. a loading state. Every other value is treated as settled and passes through immediately once no transient value is held.
Yes
-
onChange(value: T) => void
Called with each value once it is ready to be shown.
Yes
-

One DelayedIndicator can drive several elements when onChange derives all of them from the same value, so they always change together. The Combobox Async Search example does this:

Delays showing a transient value long enough to avoid flicker, and keeps it visible for a minimum duration once shown. Feed it every new value through set(), typically straight from a subscribe callback, and it forwards them to onChange. The values isTransient marks are held back for showDelayMs, so a fast operation never flashes them. Once shown they stay for at least minVisibleMs, so they can't vanish the instant they appeared.

It works on any value, not only a boolean: one instance can drive a spinner and a status text from the same source, so they never disagree.

NameDescriptionRequiredDefault
optionsDelayedIndicatorOptions<T>Yes
-

set()

Passes a new value on, delayed as described above. It does not deduplicate: the same value again can still call onChange again, as this filters state over time, it is not a distinctUntilChanged().

NameDescriptionRequiredDefault
valueT
The new value.
Yes
-

destroy()

Cancels any timer that is running. Call it when the indicator is no longer needed, e.g. on teardown: otherwise a scheduled value can still reach onChange afterwards. It is safe to call when nothing is transient or shown.