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

alibaba / hooks / #48

25 Jun 2025 02:13PM UTC coverage: 89.557% (+7.6%) from 81.965%
#48

push

travis-pro

web-flow
fixes: useClickAway文档描述修改 (#2680)

1197 of 1493 branches covered (80.17%)

Branch coverage included in aggregate %.

2722 of 2883 relevant lines covered (94.42%)

88.97 hits per line

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

83.72
/packages/hooks/src/useRequest/src/plugins/useThrottlePlugin.ts
1
import type { DebouncedFunc, ThrottleSettings } from 'lodash';
2
import throttle from 'lodash/throttle';
14✔
3
import { useEffect, useRef } from 'react';
14✔
4
import type { Plugin } from '../types';
5

6
const useThrottlePlugin: Plugin<any, any[]> = (
14✔
7
  fetchInstance,
8
  { throttleWait, throttleLeading, throttleTrailing },
358✔
9
) => {
10
  const throttledRef = useRef<DebouncedFunc<any>>(undefined);
358✔
11

12
  const options: ThrottleSettings = {};
358✔
13

14
  if (throttleLeading !== undefined) {
358!
15
    options.leading = throttleLeading;
×
16
  }
17
  if (throttleTrailing !== undefined) {
358!
18
    options.trailing = throttleTrailing;
×
19
  }
20

21
  useEffect(() => {
358✔
22
    if (throttleWait) {
81✔
23
      const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance);
1✔
24

25
      throttledRef.current = throttle(
1✔
26
        (callback) => {
27
          callback();
2✔
28
        },
29
        throttleWait,
30
        options,
31
      );
32

33
      // throttle runAsync should be promise
34
      // https://github.com/lodash/lodash/issues/4400#issuecomment-834800398
35
      fetchInstance.runAsync = (...args) => {
12✔
36
        return new Promise((resolve, reject) => {
4✔
37
          throttledRef.current?.(() => {
4!
38
            _originRunAsync(...args)
2✔
39
              .then(resolve)
40
              .catch(reject);
41
          });
42
        });
43
      };
44

45
      return () => {
1✔
46
        fetchInstance.runAsync = _originRunAsync;
1✔
47
        throttledRef.current?.cancel();
1!
48
      };
49
    }
50
  }, [throttleWait, throttleLeading, throttleTrailing]);
51

52
  if (!throttleWait) {
358✔
53
    return {};
356✔
54
  }
55

56
  return {
2✔
57
    onCancel: () => {
58
      throttledRef.current?.cancel();
1!
59
    },
60
  };
61
};
62

63
export default useThrottlePlugin;
14✔
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