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

alibaba / hooks / #33

pending completion
#33

push

travis-pro

web-flow
fix useWebSocket error (#2176) (#2177)

* fix: useWebSocket don't call connect when socketUrl is empty (#2176)

* fix: useWebSocket unlimited reconnect when update socketUrl (#2176)

* fix: disconnect not works

* docs: modify useWebSocket docs

1029 of 1308 branches covered (78.67%)

Branch coverage included in aggregate %.

9 of 9 new or added lines in 1 file covered. (100.0%)

2517 of 2665 relevant lines covered (94.45%)

89.11 hits per line

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

90.38
/packages/hooks/src/useRequest/src/useRequestImplement.ts
1
import useCreation from '../../useCreation';
14✔
2
import useLatest from '../../useLatest';
14✔
3
import useMemoizedFn from '../../useMemoizedFn';
14✔
4
import useMount from '../../useMount';
14✔
5
import useUnmount from '../../useUnmount';
14✔
6
import useUpdate from '../../useUpdate';
14✔
7
import isDev from '../../utils/isDev';
14✔
8

9
import Fetch from './Fetch';
14✔
10
import type { Options, Plugin, Result, Service } from './types';
11

12
function useRequestImplement<TData, TParams extends any[]>(
13
  service: Service<TData, TParams>,
14
  options: Options<TData, TParams> = {},
338!
15
  plugins: Plugin<TData, TParams>[] = [],
338!
16
) {
17
  const { manual = false, ...rest } = options;
338✔
18

19
  if (isDev) {
338✔
20
    if (options.defaultParams && !Array.isArray(options.defaultParams)) {
338!
21
      console.warn(`expected defaultParams is array, got ${typeof options.defaultParams}`);
×
22
    }
23
  }
24

25
  const fetchOptions = {
338✔
26
    manual,
27
    ...rest,
28
  };
29

30
  const serviceRef = useLatest(service);
338✔
31

32
  const update = useUpdate();
338✔
33

34
  const fetchInstance = useCreation(() => {
338✔
35
    const initState = plugins.map((p) => p?.onInit?.(fetchOptions)).filter(Boolean);
616!
36

37
    return new Fetch<TData, TParams>(
77✔
38
      serviceRef,
39
      fetchOptions,
40
      update,
41
      Object.assign({}, ...initState),
42
    );
43
  }, []);
44
  fetchInstance.options = fetchOptions;
338✔
45
  // run all plugins hooks
46
  fetchInstance.pluginImpls = plugins.map((p) => p(fetchInstance, fetchOptions));
2,704✔
47

48
  useMount(() => {
338✔
49
    if (!manual) {
77✔
50
      // useCachePlugin can set fetchInstance.state.params from cache when init
51
      const params = fetchInstance.state.params || options.defaultParams || [];
54✔
52
      // @ts-ignore
53
      fetchInstance.run(...params);
54✔
54
    }
55
  });
56

57
  useUnmount(() => {
338✔
58
    fetchInstance.cancel();
77✔
59
  });
60

61
  return {
338✔
62
    loading: fetchInstance.state.loading,
63
    data: fetchInstance.state.data,
64
    error: fetchInstance.state.error,
65
    params: fetchInstance.state.params || [],
415✔
66
    cancel: useMemoizedFn(fetchInstance.cancel.bind(fetchInstance)),
67
    refresh: useMemoizedFn(fetchInstance.refresh.bind(fetchInstance)),
68
    refreshAsync: useMemoizedFn(fetchInstance.refreshAsync.bind(fetchInstance)),
69
    run: useMemoizedFn(fetchInstance.run.bind(fetchInstance)),
70
    runAsync: useMemoizedFn(fetchInstance.runAsync.bind(fetchInstance)),
71
    mutate: useMemoizedFn(fetchInstance.mutate.bind(fetchInstance)),
72
  } as Result<TData, TParams>;
73
}
74

75
export default useRequestImplement;
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