hooks/usePendingAction#
usePendingAction()#
Busy state for a user action: disabled while pending, one run at a time, and an inline progress flag once the action has taken long enough to need one.
Args: action: The work to do. May be sync or async; its result is ignored. options: Error reporting and the progress delay.
Returns:
The pending flags and a guarded run to wire to the control.
Type Parameters#
| Type Parameter |
|---|
A extends unknown[] |
Parameters#
| Parameter | Type |
|---|---|
action |
(...args) => unknown |
options? |
UsePendingActionOptions |
Returns#
PENDING_PROGRESS_DELAY_MS#
How long an action runs before its inline progress indicator appears. Shorter than this, the pressed and disabled states are acknowledgement enough and a spinner would only flicker.
PendingAction#
Type Parameters#
| Type Parameter |
|---|
A extends unknown[] |
Properties#
pending#
True from the moment the action starts until it settles. Disable the control on it.
run#
Starts the action, unless one is already running, in which case the call is dropped. Never rejects: a thrown error is reported and clears the busy state.
Parameters#
| Parameter | Type |
|---|---|
...args |
A |
Returns#
Promise<void>
showProgress#
True once the action has run longer than the progress delay. Show the inline indicator on it.
UsePendingActionOptions#
Properties#
errorMessage?#
What the user was trying to do, as a sentence in their language. Shown if
the action throws. Ignored when onError is given.
onError?#
Replaces the default error notification.
Parameters#
| Parameter | Type |
|---|---|
error |
unknown |
Returns#
void
progressDelayMs?#
Delay before showProgress turns on. Defaults to PENDING_PROGRESS_DELAY_MS.