UNPKG

@ledgerhq/live-common

Version:
36 lines 1.36 kB
import { useEffect, useReducer, useRef } from "react"; /** * State machine for manual refresh lifecycle: * idle → (click) → waitingForSync → (syncStarted) → syncing → (syncDone) → idle */ function reducer(phase, event) { switch (event) { case "click": return "waitingForSync"; case "syncStarted": return phase === "waitingForSync" ? "syncing" : phase; case "syncDone": return phase === "syncing" || phase === "waitingForSync" ? "idle" : phase; default: return phase; } } /** * Tracks whether a user-triggered manual refresh is in progress. * Latches on when a click is detected and releases once the sync completes. */ export function useManualRefresh(stableSyncPending, lastUserSyncClickTimestamp) { const [phase, dispatch] = useReducer(reducer, "idle"); const prevTimestampRef = useRef(lastUserSyncClickTimestamp); useEffect(() => { if (lastUserSyncClickTimestamp !== prevTimestampRef.current) { prevTimestampRef.current = lastUserSyncClickTimestamp; dispatch("click"); } }, [lastUserSyncClickTimestamp]); useEffect(() => { dispatch(stableSyncPending ? "syncStarted" : "syncDone"); }, [stableSyncPending]); return phase !== "idle"; } //# sourceMappingURL=useManualRefresh.js.map