MCPcopy Create free account
hub / github.com/Linen-dev/linen.dev / usePolling

Function usePolling

apps/web/hooks/polling.tsx:12–48  ·  view source on GitHub ↗
({ fetch, success, error }: Props, dependencies?: any[])

Source from the content-addressed store, hash-verified

10const POLL_INTERVAL = POLL_INTERVAL_IN_SECONDS * 1000;
11
12function 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
50export default usePolling;

Callers

nothing calls this directly

Calls 1

fetchInboxFunction · 0.85

Tested by

no test coverage detected