• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / hooks / #49

05 Nov 2025 09:55AM UTC coverage: 85.833% (-0.6%) from 86.39%
#49

push

travis-pro

952 of 1206 branches covered (78.94%)

Branch coverage included in aggregate %.

2241 of 2514 relevant lines covered (89.14%)

84.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/packages/hooks/src/useRequest/src/plugins/usePollingPlugin.ts
1
import { useRef } from 'react';
13✔
2
import useUpdateEffect from '../../../useUpdateEffect';
13✔
3
import type { Plugin, Timeout } from '../types';
4
import isDocumentVisible from '../utils/isDocumentVisible';
13✔
5
import subscribeReVisible from '../utils/subscribeReVisible';
13✔
6

7
const usePollingPlugin: Plugin<any, any[]> = (
13✔
8
  fetchInstance,
9
  { pollingInterval, pollingWhenHidden = true, pollingErrorRetryCount = -1 },
586✔
10
) => {
11
  const timerRef = useRef<Timeout>(undefined);
308✔
12
  const unsubscribeRef = useRef<() => void>(undefined);
308✔
13
  const countRef = useRef<number>(0);
308✔
14

15
  const stopPolling = () => {
308✔
16
    if (timerRef.current) {
13✔
17
      clearTimeout(timerRef.current);
11✔
18
    }
19
    unsubscribeRef.current?.();
13!
20
  };
21

22
  useUpdateEffect(() => {
308✔
23
    if (!pollingInterval) {
×
24
      stopPolling();
×
25
    }
26
  }, [pollingInterval]);
27

28
  if (!pollingInterval) {
308✔
29
    return {};
289✔
30
  }
31

32
  return {
19✔
33
    onBefore: () => {
34
      stopPolling();
10✔
35
    },
36
    onError: () => {
37
      countRef.current += 1;
5✔
38
    },
39
    onSuccess: () => {
40
      countRef.current = 0;
5✔
41
    },
42
    onFinally: () => {
43
      if (
10✔
44
        pollingErrorRetryCount === -1 ||
20✔
45
        // When an error occurs, the request is not repeated after pollingErrorRetryCount retries
46
        (pollingErrorRetryCount !== -1 && countRef.current <= pollingErrorRetryCount)
47
      ) {
48
        timerRef.current = setTimeout(() => {
9✔
49
          // if pollingWhenHidden = false && document is hidden, then stop polling and subscribe revisible
50
          if (!pollingWhenHidden && !isDocumentVisible()) {
6!
51
            unsubscribeRef.current = subscribeReVisible(() => {
×
52
              fetchInstance.refresh();
×
53
            });
54
          } else {
55
            fetchInstance.refresh();
6✔
56
          }
57
        }, pollingInterval);
58
      } else {
59
        countRef.current = 0;
1✔
60
      }
61
    },
62
    onCancel: () => {
63
      stopPolling();
3✔
64
    },
65
  };
66
};
67

68
export default usePollingPlugin;
13✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc