DelayedIndicator
View as MarkdownDelays 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.
import { DelayedIndicator, extbase } from 'fluid-primitives';
const indicator = new DelayedIndicator({
isTransient: value => value === true,
onChange: visible => {
spinnerEl.hidden = !visible;
},
});
async function searchProducts(query: string) {
indicator.set(true);
try {
const response = await extbase.post(searchUrl, { q: query });
renderResults(await response.json());
} finally {
indicator.set(false);
}
}
Call indicator.destroy() on teardown to cancel a pending timer.
The options of a DelayedIndicator.
| Name | Description | Required | Default |
|---|---|---|---|
showDelayMs | numberHow long a transient value must persist before it is shown at all. Must be finite and >= 0. | No | 150 |
minVisibleMs | numberOnce 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) => booleanMarks 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) => voidCalled with each value once it is ready to be shown. | Yes | - |
- A transient value reaches
onChangeaftershowDelayMs. - Once shown, it stays current for at least
minVisibleMs, even if a settled value arrives sooner. - A settled value that arrives before a transient value was shown goes to
onChangeright away, so the transient value never appears. - A settled value that arrives while a transient value is shown waits for that value's
minVisibleMs, then goes toonChange. - A transient value that arrives while another one is shown updates the display right away and does not restart the
minVisibleMsclock. set()passes repeated values on: the same value again can callonChangeagain.
Update a spinner and a status text together
Permalink to heading "Update a spinner and a status text together"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:
type SearchStatus = 'loading' | 'error' | 'empty' | 'idle' | 'results';
const status = new DelayedIndicator<SearchStatus>({
isTransient: value => value === 'loading',
onChange: value => {
spinnerEl.hidden = value !== 'loading';
statusEl.textContent = statusTexts[value];
},
});
status.set('loading');
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.
class DelayedIndicator<T>
Constructor
Permalink to heading "Constructor"| Name | Description | Required | Default |
|---|---|---|---|
options | DelayedIndicatorOptions<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().
set(value: T): void
| Name | Description | Required | Default |
|---|---|---|---|
value | TThe 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.
destroy(): void