|
| 1 | +// src/hooks/transactions/useTransactionsTracker/usePollingWithWebsocketFallback.ts |
| 2 | +import { useEffect, useRef, useState } from 'react'; |
| 3 | +import { accountSelector } from 'reduxStore/selectors'; |
| 4 | +import { store } from 'reduxStore/store'; |
| 5 | +import { useGetAccount } from '../../account/useGetAccount'; |
| 6 | +import { |
| 7 | + websocketConnection, |
| 8 | + WebsocketConnectionStatusEnum |
| 9 | +} from '../../websocketListener/websocketConnection'; |
| 10 | +import { useGetPollingInterval } from '../useGetPollingInterval'; |
| 11 | + |
| 12 | +interface UseWebsocketPollingFallbackParamsType { |
| 13 | + onPoll: () => void | Promise<void>; |
| 14 | + pollingInterval?: number; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * A hook that handles polling with websocket fallback mechanism. |
| 19 | + * It will start polling when: |
| 20 | + * 1. There is an address present |
| 21 | + * 2. Websocket is not in COMPLETED state |
| 22 | + * |
| 23 | + * It will stop polling when: |
| 24 | + * 1. Address is removed |
| 25 | + * 2. Websocket becomes COMPLETED |
| 26 | + */ |
| 27 | +export const useWebsocketPollingFallback = ({ |
| 28 | + onPoll, |
| 29 | + pollingInterval |
| 30 | +}: UseWebsocketPollingFallbackParamsType) => { |
| 31 | + const defaultPollingInterval = useGetPollingInterval(); |
| 32 | + const usedPollingInterval = pollingInterval ?? defaultPollingInterval; |
| 33 | + const pollingIntervalTimer = useRef<NodeJS.Timeout | null>(null); |
| 34 | + const { address } = useGetAccount(); |
| 35 | + const [websocketStatus, setWebsocketStatus] = useState( |
| 36 | + websocketConnection.status |
| 37 | + ); |
| 38 | + |
| 39 | + const clearFallbackTimer = () => { |
| 40 | + if (pollingIntervalTimer.current) { |
| 41 | + clearInterval(pollingIntervalTimer.current); |
| 42 | + pollingIntervalTimer.current = null; |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + const unsubscribe = websocketConnection.subscribe((status) => { |
| 48 | + console.info('Websocket status changed:', status); |
| 49 | + setWebsocketStatus(status); |
| 50 | + }); |
| 51 | + |
| 52 | + return () => unsubscribe(); |
| 53 | + }, []); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + const isWebsocketCompleted = |
| 57 | + websocketStatus === WebsocketConnectionStatusEnum.COMPLETED; |
| 58 | + |
| 59 | + if (!address || isWebsocketCompleted) { |
| 60 | + clearFallbackTimer(); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (pollingIntervalTimer.current) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + console.info('Websocket connection failed. Starting polling fallback...'); |
| 69 | + pollingIntervalTimer.current = setInterval(() => { |
| 70 | + const currentStatus = websocketConnection.status; |
| 71 | + const isNowCompleted = |
| 72 | + currentStatus === WebsocketConnectionStatusEnum.COMPLETED; |
| 73 | + |
| 74 | + // Get the address from store directly to avoid closure issues related to hooks |
| 75 | + const { address: currentAddress } = accountSelector(store.getState()); |
| 76 | + |
| 77 | + if (!currentAddress || isNowCompleted) { |
| 78 | + clearFallbackTimer(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + onPoll(); |
| 83 | + }, usedPollingInterval); |
| 84 | + |
| 85 | + return () => { |
| 86 | + clearFallbackTimer(); |
| 87 | + }; |
| 88 | + }, [address, websocketStatus, onPoll, usedPollingInterval]); |
| 89 | + |
| 90 | + return clearFallbackTimer; |
| 91 | +}; |
0 commit comments