Skip to content

useConnectivityStore

This store manages the online/offline status and pending sync tasks for the Sign-In App.

📄 View source on GitHub

Used by

State

NameTypeDescription
isOnlinerefHolds the online/offline status
hasPendingSyncTasksrefHolds the pending sync tasks
cacheIsStaleref
currentIntervalrefHolds the current polling interval

Initialization

init 🚀

Initializes the connectivity store by persisting the current state and starting the polling of the online status. It also sets up event listeners for changes in network connectivity and document visibility to manage polling behavior accordingly.

Actions

checkOnlineStatus

Checks if the application has an active internet connection by making a HEAD request to the backend's /ping endpoint. If the response is successful, the function returns true, otherwise it returns false.

handleVisibilityChange

Handles the visibility change event for the document.
When the document (tab/window) becomes visible, it restarts the polling of the online status. When the document becomes hidden, it stops the polling.

persist

Persists the state of the connectivity store to local storage. This operation is asynchronous and ensures that the current online/offline status and pending sync tasks are saved.

setOffline

Manually set the connectivity status to offline. This is useful for testing purposes or when the browser's online/offline status is not updating correctly.

setPendingTasksCleared

Set the pending sync task flag to false, indicating that there are no more tasks pending to be synced.

setTasksPending

Sets the pending sync task flag to true, indicating that there are tasks waiting to be synced.

checkPendingTasks

Checks if there are any pending tasks in any of the registered queues. If tasks are found, sets the hasPendingSyncTasks flag to true and persists it. If no tasks are found, sets the hasPendingSyncTasks flag to false and persists it.

checkIfApiCacheIsStale

Checks if the API cache for run-info-complex entries is stale. This function opens the 'api-cache' and retrieves all cached requests. It filters the requests to find those starting with the specified backendPath and checks their 'x-cached-at' headers to determine the newest cached entry. If no valid cache entries are found or if the cache is older than 5 days, the cache is considered stale.

pollStatus

Polls the online status of the application by making a HEAD request to the backend's /ping endpoint. If the status changes, the function updates the isOnline flag and resets the polling interval to the minimum value. If the status does not change, the function adjusts the polling interval depending on the current status:
- If the application is already online, the interval is increased up to the maximum value.
- If the application is already offline, the interval is kept at the minimum value and the pending tasks are checked again.
Finally, the function schedules the next poll using the adjusted interval.

restartPolling

Stops any currently scheduled polling and schedules a new poll at the current interval.

stopPolling

Cancels any currently scheduled polling. If polling is not active, this is a no-op.

destroy

Cleans up the connectivity store by stopping the polling of the online status, and removing event listeners for changes in network connectivity and document visibility.

Getters/Setters

(anonymous) (watch)

Watch for changes in the online/offline status. If the status has changed to online, trigger the task sync process.

Released under the MIT License.