({ fetch, success, error }: Props, dependencies?: any[])
| 10 | const POLL_INTERVAL = POLL_INTERVAL_IN_SECONDS * 1000; |
| 11 | |
| 12 | function usePolling({ fetch, success, error }: Props, dependencies?: any[]) { |
| 13 | const [loading, setLoading] = useState(false); |
| 14 | |
| 15 | useEffect(() => { |
| 16 | let mounted = true; |
| 17 | setLoading(true); |
| 18 | const fetchInbox = () => |
| 19 | fetch() |
| 20 | .then((data: any) => { |
| 21 | if (mounted) { |
| 22 | success(data); |
| 23 | } |
| 24 | }) |
| 25 | .catch(() => { |
| 26 | if (mounted) { |
| 27 | error(); |
| 28 | } |
| 29 | }) |
| 30 | .finally(() => { |
| 31 | if (mounted) { |
| 32 | setLoading(false); |
| 33 | } |
| 34 | }); |
| 35 | fetchInbox(); |
| 36 | |
| 37 | const intervalId = setInterval(() => { |
| 38 | fetchInbox(); |
| 39 | }, POLL_INTERVAL); |
| 40 | |
| 41 | return () => { |
| 42 | clearInterval(intervalId); |
| 43 | mounted = false; |
| 44 | }; |
| 45 | }, dependencies || []); |
| 46 | |
| 47 | return [loading]; |
| 48 | } |
| 49 | |
| 50 | export default usePolling; |
nothing calls this directly
no test coverage detected