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

alovajs / alova / #190

22 Jul 2024 07:20AM UTC coverage: 93.187% (-0.1%) from 93.292%
#190

push

github

web-flow
Merge pull request #470 from alovajs/changeset-release/next

ci: release next (beta)

1579 of 1740 branches covered (90.75%)

Branch coverage included in aggregate %.

9473 of 10120 relevant lines covered (93.61%)

58.27 hits per line

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

99.19
/packages/client/src/hooks/useAutoRequest.ts
1
import useRequest from '@/hooks/core/useRequest';
1✔
2
import { noop, statesHookHelper } from '@alova/shared/function';
1✔
3
import { falseValue, isSSR, trueValue } from '@alova/shared/vars';
1✔
4
import { AlovaGenerics, Method, promiseStatesHook } from 'alova';
1✔
5
import {
1✔
6
  AlovaMethodHandler,
1✔
7
  AutoRequestHookConfig,
1✔
8
  NotifyHandler,
1✔
9
  UnbindHandler,
1✔
10
  UseHookExposure
1✔
11
} from '~/typings/clienthook';
1✔
12

1✔
13
interface AutoRequestHook {
1✔
14
  <AG extends AlovaGenerics>(
1✔
15
    handler: Method<AG> | AlovaMethodHandler<AG>,
1✔
16
    config?: AutoRequestHookConfig<AG>
1✔
17
  ): UseHookExposure<AG>;
1✔
18
  onNetwork<AG extends AlovaGenerics = AlovaGenerics>(
1✔
19
    notify: NotifyHandler,
1✔
20
    config: AutoRequestHookConfig<AG>
1✔
21
  ): UnbindHandler;
1✔
22
  onPolling<AG extends AlovaGenerics = AlovaGenerics>(
1✔
23
    notify: NotifyHandler,
1✔
24
    config: AutoRequestHookConfig<AG>
1✔
25
  ): UnbindHandler;
1✔
26
  onVisibility<AG extends AlovaGenerics = AlovaGenerics>(
1✔
27
    notify: NotifyHandler,
1✔
28
    config: AutoRequestHookConfig<AG>
1✔
29
  ): UnbindHandler;
1✔
30
  onFocus<AG extends AlovaGenerics = AlovaGenerics>(
1✔
31
    notify: NotifyHandler,
1✔
32
    config: AutoRequestHookConfig<AG>
1✔
33
  ): UnbindHandler;
1✔
34
}
1✔
35

1✔
36
export const defaultConfig: AutoRequestHookConfig<AlovaGenerics> = {
1✔
37
  enableFocus: trueValue,
1✔
38
  enableNetwork: trueValue,
1✔
39
  throttle: 1000
1✔
40
};
1✔
41
const useAutoRequest: AutoRequestHook = (handler, config = {}) => {
1✔
42
  let notifiable = trueValue;
18✔
43
  const {
18✔
44
    enableFocus = trueValue,
18✔
45
    enableVisibility = trueValue,
18✔
46
    enableNetwork = trueValue,
18✔
47
    pollingTime = 0,
18✔
48
    throttle = 1000
18✔
49
  } = config;
18✔
50
  const { onMounted, onUnmounted, __referingObj: referingObject } = statesHookHelper(promiseStatesHook());
18✔
51
  const states = useRequest(handler, {
18✔
52
    ...config,
18✔
53
    __referingObj: referingObject
18✔
54
  });
18✔
55
  const notify = () => {
18✔
56
    if (notifiable) {
34✔
57
      states.send();
34✔
58
      if (throttle > 0) {
34✔
59
        notifiable = falseValue;
1✔
60
        setTimeout(() => {
1✔
61
          notifiable = trueValue;
×
62
        }, throttle);
1✔
63
      }
1✔
64
    }
34✔
65
  };
18✔
66

18✔
67
  let offNetwork = noop;
18✔
68
  let offFocus = noop;
18✔
69
  let offVisiblity = noop;
18✔
70
  let offPolling = noop;
18✔
71
  onMounted(() => {
18✔
72
    if (!isSSR) {
5✔
73
      offNetwork = enableNetwork ? useAutoRequest.onNetwork(notify, config) : offNetwork;
5✔
74
      offFocus = enableFocus ? useAutoRequest.onFocus(notify, config) : offFocus;
5✔
75
      offVisiblity = enableVisibility ? useAutoRequest.onVisibility(notify, config) : offVisiblity;
5✔
76
      offPolling = pollingTime > 0 ? useAutoRequest.onPolling(notify, config) : offPolling;
5✔
77
    }
5✔
78
  });
18✔
79
  onUnmounted(() => {
18✔
80
    offNetwork();
5✔
81
    offFocus();
5✔
82
    offVisiblity();
5✔
83
    offPolling();
5✔
84
  });
18✔
85
  return states;
18✔
86
};
18✔
87

1✔
88
const on = (type: string, handler: NotifyHandler) => {
1✔
89
  window.addEventListener(type, handler);
9✔
90
  return () => window.removeEventListener(type, handler);
9✔
91
};
9✔
92
useAutoRequest.onNetwork = notify => on('online', notify);
1✔
93
useAutoRequest.onFocus = notify => on('focus', notify);
1✔
94
useAutoRequest.onVisibility = notify => {
1✔
95
  const handle = () => document.visibilityState === 'visible' && notify();
3✔
96
  return on('visibilitychange', handle);
3✔
97
};
3✔
98
useAutoRequest.onPolling = (notify, config) => {
1✔
99
  const timer = setInterval(notify, config.pollingTime);
2✔
100
  return () => clearInterval(timer);
2✔
101
};
2✔
102

1✔
103
export default useAutoRequest;
1✔
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

© 2025 Coveralls, Inc