@deltares/fews-web-oc-composables
@deltares/fews-web-oc-composables / useRefreshCoordinator
Function: useRefreshCoordinator()
useRefreshCoordinator(
callback,options):RefreshCoordinator
Defined in: useRefreshCoordinator/index.ts:131
Beta
Coordinates automatic refreshes for data-fetching composables, driven by one or more RefreshPolicy triggers: a shared system tick, a fixed interval, document-visibility resume, or manual triggers.
Only one refresh runs at a time: if trigger() is called while a refresh is already in flight, a single refresh is queued to run immediately afterwards.
Parameters
callback
() => void | Promise<void>
The function to invoke on each refresh. May be async; while it is pending, further triggers are coalesced into one pending refresh.
options
| Member | Type | Description |
|---|---|---|
enabled? | Ref<boolean, boolean> | A reactive flag controlling whether automatic refreshes are currently enabled. When false, all triggers are ignored until it becomes true again. |
immediateCallback? | boolean | Whether to invoke the callback immediately when the coordinator is created. |
intervalMs? | number | The interval, in milliseconds, between automatic refreshes when 'onInterval' is included in policies. |
policies | RefreshPolicy[] | The policies that can trigger a refresh. Only policies included here have any effect; e.g. 'onInterval' must be included for intervalMs to take effect. |
systemTick? | Ref<Date | undefined, Date | undefined> | The system-time synchronization tick, used by the 'onSystemTick' policy. A refresh is triggered whenever this ref's value changes. |
Configuration for which policies trigger a refresh, and their parameters. See UseRefreshCoordinatorOptions.
Returns
| Member | Type | Description |
|---|---|---|
isActive | Readonly<Ref<boolean>> | Whether automatic refreshes are currently active. |
lastRefreshAt | Readonly<Ref<Date | undefined>> | The time at which the most recent refresh completed successfully. |
lastTriggerPolicy | Readonly<Ref<RefreshPolicy | undefined>> | The policy that caused the most recent refresh. |
pause | () => void | Pauses automatic refreshes. |
resume | () => void | Resumes automatic refreshes. |
trigger | (policy?: RefreshPolicy) => void | Requests a refresh. The refresh is ignored when the coordinator is paused or disabled. |
A RefreshCoordinator used to pause/resume automatic refreshes, trigger a refresh manually, and inspect the last refresh.
Example
import { useRefreshCoordinator } from '@deltares/fews-web-oc-composables'
const coordinator = useRefreshCoordinator(
async () => {
await fetchLatestData()
},
{
policies: ['onSystemTick', 'onInterval', 'onVisibilityResume'],
intervalMs: 60_000,
immediateCallback: true,
},
)
// Trigger a refresh manually, e.g. from a "Refresh now" button:
coordinator.trigger()
// Temporarily stop automatic refreshes, e.g. while a dialog is open:
coordinator.pause()
coordinator.resume()