@jelper/hooks 0.3.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/es/index.js +15 -16
  2. package/es/useAsync.js +54 -14
  3. package/es/useBoolState.js +13 -5
  4. package/es/useCreate.js +6 -0
  5. package/es/useDebounce.js +7 -1
  6. package/es/useIgnoreAbortErrCb.js +16 -0
  7. package/es/useInterval.js +20 -7
  8. package/es/useIsMounted.js +18 -0
  9. package/es/useIsUnmount.js +9 -2
  10. package/es/useMount.js +7 -6
  11. package/es/useOption.js +19 -0
  12. package/es/useResizeObserver.js +25 -14
  13. package/es/useRtCb.js +10 -5
  14. package/es/useRtRef.js +11 -5
  15. package/es/useSafeRunner.js +20 -0
  16. package/es/useThrottle.js +7 -8
  17. package/es/useTimeout.js +24 -19
  18. package/es/useUnmount.js +9 -3
  19. package/es/useUpdateEff.js +17 -0
  20. package/es/useValue.js +26 -0
  21. package/lib/index.js +24 -23
  22. package/lib/useAsync.js +56 -16
  23. package/lib/useBoolState.js +12 -7
  24. package/lib/useCreate.js +6 -0
  25. package/lib/useDebounce.js +7 -1
  26. package/lib/useIgnoreAbortErrCb.js +19 -0
  27. package/lib/useInterval.js +41 -8
  28. package/lib/useIsMounted.js +20 -0
  29. package/lib/useIsUnmount.js +9 -2
  30. package/lib/useMount.js +7 -9
  31. package/lib/useOption.js +33 -0
  32. package/lib/useResizeObserver.js +27 -17
  33. package/lib/useRtCb.js +12 -15
  34. package/lib/useRtRef.js +9 -4
  35. package/lib/useSafeRunner.js +25 -0
  36. package/lib/useThrottle.js +7 -8
  37. package/lib/useTimeout.js +45 -20
  38. package/lib/useUnmount.js +11 -2
  39. package/lib/useUpdateEff.js +19 -0
  40. package/lib/{useRtState.js → useValue.js} +13 -16
  41. package/package.json +11 -5
  42. package/types/index.d.ts +11 -12
  43. package/types/useAsync.d.ts +23 -8
  44. package/types/useBoolState.d.ts +10 -1
  45. package/types/useCreate.d.ts +7 -1
  46. package/types/useDebounce.d.ts +6 -0
  47. package/types/useIgnoreAbortErrCb.d.ts +12 -0
  48. package/types/useInterval.d.ts +5 -1
  49. package/types/useIsMounted.d.ts +8 -0
  50. package/types/useIsUnmount.d.ts +6 -0
  51. package/types/useMount.d.ts +7 -1
  52. package/types/useOption.d.ts +15 -0
  53. package/types/useResizeObserver.d.ts +3 -1
  54. package/types/useRtCb.d.ts +8 -1
  55. package/types/useRtRef.d.ts +8 -1
  56. package/types/useSafeRunner.d.ts +8 -0
  57. package/types/useThrottle.d.ts +6 -7
  58. package/types/useTimeout.d.ts +9 -5
  59. package/types/useUnmount.d.ts +7 -1
  60. package/types/useUpdateEff.d.ts +8 -0
  61. package/types/useValue.d.ts +8 -0
  62. package/es/useListener.js +0 -17
  63. package/es/useParamsState.js +0 -9
  64. package/es/useRtEffect.js +0 -7
  65. package/es/useRtState.js +0 -28
  66. package/es/useSafeCb.js +0 -11
  67. package/es/useSafeState.js +0 -8
  68. package/es/useUpdateEffect.js +0 -12
  69. package/lib/useListener.js +0 -22
  70. package/lib/useParamsState.js +0 -26
  71. package/lib/useRtEffect.js +0 -12
  72. package/lib/useSafeCb.js +0 -20
  73. package/lib/useSafeState.js +0 -13
  74. package/lib/useUpdateEffect.js +0 -15
  75. package/types/useListener.d.ts +0 -2
  76. package/types/useParamsState.d.ts +0 -6
  77. package/types/useRtEffect.d.ts +0 -2
  78. package/types/useRtState.d.ts +0 -6
  79. package/types/useSafeCb.d.ts +0 -2
  80. package/types/useSafeState.d.ts +0 -5
  81. package/types/useUpdateEffect.d.ts +0 -1
package/es/index.js CHANGED
@@ -1,28 +1,27 @@
1
- // 预防组件销毁更新
2
- export { default as useSafeState } from './useSafeState';
3
- export { default as useSafeCb } from './useSafeCb';
1
+ // 特殊state
2
+ export { default as useBoolState } from './useBoolState';
3
+ export { default as useValue } from './useValue';
4
+ export { default as useOption } from './useOption';
4
5
  // 传入参数实时更新,保证内部最新数据
5
6
  export { default as useRtRef } from './useRtRef';
6
- export { default as useRtState } from './useRtState';
7
7
  export { default as useRtCb } from './useRtCb';
8
- export { default as useRtEffect } from './useRtEffect'; // 保证卸载回调能拿到最新数据
9
- // 特殊state
10
- export { default as useBoolState } from './useBoolState';
11
- export { default as useParamsState } from './useParamsState';
12
8
  // 特殊 Effect
13
- export { default as useUpdateEffect } from './useUpdateEffect';
14
- // 生命周期
9
+ export { default as useUpdateEff } from './useUpdateEff';
10
+ // 模拟生命周期
11
+ export { default as useIsMounted } from './useIsMounted';
15
12
  export { default as useIsUnmount } from './useIsUnmount';
16
13
  export { default as useCreate } from './useCreate';
17
14
  export { default as useMount } from './useMount';
18
15
  export { default as useUnmount } from './useUnmount';
19
- // 节流防抖
20
- export { default as useThrottle } from './useThrottle';
16
+ // callback
17
+ export { default as useIgnoreAbortErrCb } from './useIgnoreAbortErrCb';
18
+ // 防抖节流
21
19
  export { default as useDebounce } from './useDebounce';
20
+ export { default as useThrottle } from './useThrottle';
22
21
  // 使用异步
22
+ export { default as useSafeRunner } from './useSafeRunner';
23
23
  export { default as useAsync } from './useAsync';
24
24
  // 浏览器api
25
- export { default as useTimeout } from './useTimeout';
26
- export { default as useInterval } from './useInterval';
27
- export { default as useListener } from './useListener';
28
- export { default as useResizeObserver } from './useResizeObserver';
25
+ export { default as useTimeout, useTimeoutHandler } from './useTimeout';
26
+ export { default as useInterval, useIntervalHandler } from './useInterval';
27
+ export { default as useResizeObserver, useResizeObserverHandler } from './useResizeObserver';
package/es/useAsync.js CHANGED
@@ -1,33 +1,73 @@
1
- import useSafeState from './useSafeState';
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-18 23:44:38
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-29 17:58:54
6
+ */
7
+ import { useEffect, useState } from 'react';
2
8
  import useRtCb from './useRtCb';
3
9
  import useCreate from './useCreate';
4
- const useAsync = (asyncFn, opt = {
10
+ import useIsUnmounted from './useIsUnmount';
11
+ import useUnmount from './useUnmount';
12
+ const defOpt = {
5
13
  immediate: false,
6
14
  catchParam: false,
7
- }) => {
8
- const [data, setData] = useSafeState(null);
9
- const [param, setParam] = useSafeState(opt.defParam || {});
10
- const [loading, setLoading] = useSafeState(false);
11
- const [error, setError] = useSafeState(null);
15
+ };
16
+ const useAsync = (asyncFn, opt = defOpt) => {
17
+ const [data, setData] = useState();
18
+ const [param, setParam] = useState(opt.defParam || {});
19
+ const [loading, setLoading] = useState(false);
20
+ const [error, setError] = useState();
21
+ const [controller, setController] = useState();
22
+ const isUnmounted = useIsUnmounted();
23
+ useEffect(() => {
24
+ if (!controller || !opt.onAbort) {
25
+ return;
26
+ }
27
+ const onAbort = opt.onAbort;
28
+ controller.signal.addEventListener('abort', onAbort);
29
+ return () => {
30
+ controller.signal.removeEventListener('abort', onAbort);
31
+ };
32
+ }, [controller]);
12
33
  const run = useRtCb((runParam) => {
13
34
  const currParam = opt.catchParam ? Object.assign(Object.assign({}, param), (runParam || {})) : runParam;
14
35
  setLoading(true);
15
- setError(null);
16
- return asyncFn(currParam)
17
- .then(opt.format)
36
+ setError(undefined);
37
+ if (controller) {
38
+ controller.abort();
39
+ }
40
+ const abortController = new AbortController();
41
+ setController(abortController);
42
+ return asyncFn(currParam, { signal: abortController.signal })
18
43
  .then((resData) => {
19
- setData(resData);
44
+ if (isUnmounted() || abortController.signal.aborted)
45
+ return;
46
+ setData(opt.format ? opt.format(resData) : resData);
20
47
  setParam(currParam);
21
- }, setError)
22
- .finally(() => {
23
48
  setLoading(false);
49
+ }, (err) => {
50
+ if (isUnmounted() || abortController.signal.aborted)
51
+ return Promise.reject(err);
52
+ setError(err);
53
+ setLoading(false);
54
+ }).finally(() => {
55
+ if (isUnmounted())
56
+ return;
57
+ setController(undefined);
24
58
  });
25
59
  });
60
+ const refresh = useRtCb(() => run(param));
26
61
  useCreate(() => {
27
62
  if (opt.immediate) {
28
63
  run(param);
29
64
  }
30
65
  });
31
- return { data, run, loading, error };
66
+ useUnmount(() => {
67
+ if (controller) {
68
+ controller.abort();
69
+ }
70
+ });
71
+ return [data, { run, refresh, loading, error, param, controller }];
32
72
  };
33
73
  export default useAsync;
@@ -1,13 +1,21 @@
1
- import { useCallback } from 'react';
2
- import useSafeState from './useSafeState';
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:16:34
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-29 18:08:43
6
+ */
7
+ import { useState, useCallback } from 'react';
3
8
  const useBoolState = (value) => {
4
- const [val, setVal] = useSafeState(value);
9
+ const [val, setVal] = useState(value);
5
10
  const switchFn = useCallback((switchValue) => {
6
- if (typeof switchValue === 'boolean') {
11
+ if (switchValue === undefined || switchValue === null) {
12
+ setVal((oldVal) => !oldVal);
13
+ }
14
+ else if (typeof switchValue === 'boolean' || typeof switchValue === 'function') {
7
15
  setVal(switchValue);
8
16
  }
9
17
  else {
10
- setVal((oldVal) => !oldVal);
18
+ setVal(!!switchValue);
11
19
  }
12
20
  }, []);
13
21
  return [val, switchFn];
package/es/useCreate.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:34:59
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-19 22:53:33
6
+ */
1
7
  import { useState } from 'react';
2
8
  const useCreate = (cb) => {
3
9
  useState(cb);
package/es/useDebounce.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-19 22:55:14
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-29 18:56:32
6
+ */
1
7
  import { debounce } from 'lodash-es';
2
8
  import { useMemo } from 'react';
3
9
  import useRtCb from './useRtCb';
@@ -7,6 +13,6 @@ const useDebounce = (cb, wait, opts) => {
7
13
  return debounce((...arg) => {
8
14
  rtCb(...arg);
9
15
  }, wait, opts);
10
- }, []);
16
+ }, [wait, opts]);
11
17
  };
12
18
  export default useDebounce;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-19 22:28:14
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-19 22:57:23
6
+ */
7
+ import { useCallback } from "react";
8
+ const useIgnoreAbortErrCb = (cb, deps = [], opts) => {
9
+ return useCallback((err) => {
10
+ var _a;
11
+ if (err.name === 'AbortError')
12
+ return (_a = opts === null || opts === void 0 ? void 0 : opts.onAbort) === null || _a === void 0 ? void 0 : _a.call(opts, err);
13
+ return cb(err);
14
+ }, deps);
15
+ };
16
+ export default useIgnoreAbortErrCb;
package/es/useInterval.js CHANGED
@@ -2,24 +2,37 @@
2
2
  * @Author: apathyjade
3
3
  * @Date: 2023-12-14 16:51:29
4
4
  * @Last Modified by: apathyjade
5
- * @Last Modified time: 2024-03-14 16:32:55
5
+ * @Last Modified time: 2025-05-30 18:24:00
6
6
  */
7
7
  import { useCallback, useEffect, useRef } from 'react';
8
8
  import useRtCb from './useRtCb';
9
- const useInterval = (callback, delay) => {
9
+ export const useIntervalHandler = () => {
10
10
  const timerRef = useRef();
11
11
  const clearTimer = useCallback(() => {
12
12
  if (timerRef.current) {
13
13
  window.clearInterval(timerRef.current);
14
+ timerRef.current = undefined;
14
15
  }
15
16
  }, []);
17
+ const bindTimer = useCallback((callback, timeout, ...arg) => {
18
+ clearTimer();
19
+ timerRef.current = window.setInterval(() => {
20
+ callback(...arg);
21
+ }, timeout);
22
+ }, []);
23
+ useEffect(() => clearTimer);
24
+ return [
25
+ bindTimer,
26
+ clearTimer,
27
+ ];
28
+ };
29
+ export const useInterval = (callback, timeout, ...arg) => {
30
+ const [bindTimer, clearTimer] = useIntervalHandler();
16
31
  const cb = useRtCb(callback);
17
32
  useEffect(() => {
18
- timerRef.current = window.setInterval(cb, delay);
19
- return () => {
20
- clearTimer();
21
- };
22
- }, [delay]);
33
+ bindTimer(cb, timeout, ...arg);
34
+ return clearTimer;
35
+ }, [cb, timeout, ...arg]);
23
36
  return clearTimer;
24
37
  };
25
38
  export default useInterval;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 21:34:27
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-16 21:37:21
6
+ */
7
+ import { useCallback, useEffect, useRef } from 'react';
8
+ const useIsMounted = () => {
9
+ const ref = useRef(false);
10
+ useEffect(() => {
11
+ ref.current = true;
12
+ return () => {
13
+ ref.current = false;
14
+ };
15
+ }, []);
16
+ return useCallback(() => ref.current, []);
17
+ };
18
+ export default useIsMounted;
@@ -1,11 +1,18 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 21:33:24
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-29 20:15:50
6
+ */
1
7
  import { useCallback, useEffect, useRef } from 'react';
2
8
  const useIsUnmount = () => {
3
9
  const ref = useRef(true);
4
10
  useEffect(() => {
11
+ ref.current = false;
5
12
  return () => {
6
- ref.current = false;
13
+ ref.current = true;
7
14
  };
8
15
  }, []);
9
- return useCallback(() => !ref.current, []);
16
+ return useCallback(() => ref.current, []);
10
17
  };
11
18
  export default useIsUnmount;
package/es/useMount.js CHANGED
@@ -1,10 +1,11 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-18 00:21:16
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-18 00:21:16
6
+ */
1
7
  import { useEffect } from 'react';
2
- import useRtRef from './useRtRef';
3
8
  const useMount = (cb) => {
4
- const ref = useRtRef(cb);
5
- useEffect(() => {
6
- var _a;
7
- (_a = ref.current) === null || _a === void 0 ? void 0 : _a.call(ref);
8
- });
9
+ useEffect(cb, []);
9
10
  };
10
11
  export default useMount;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:35:37
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-16 21:31:54
6
+ */
7
+ import { useCallback, useState } from 'react';
8
+ const useOption = (defOpt = {}) => {
9
+ const [initOpt] = useState(defOpt);
10
+ const [option, setOption] = useState(initOpt);
11
+ const reset = useCallback(() => {
12
+ setOption(initOpt);
13
+ }, [initOpt]);
14
+ const update = useCallback((newOpts) => {
15
+ setOption((oldOpts) => (Object.assign(Object.assign({}, (oldOpts || {})), (typeof newOpts === 'function' ? newOpts(oldOpts) : newOpts || {}))));
16
+ }, []);
17
+ return [option, { setOption, reset, update }];
18
+ };
19
+ export default useOption;
@@ -2,10 +2,9 @@
2
2
  * @Author: apathyjade
3
3
  * @Date: 2023-11-24 10:52:51
4
4
  * @Last Modified by: apathyjade
5
- * @Last Modified time: 2024-03-14 16:34:42
5
+ * @Last Modified time: 2025-05-30 14:35:56
6
6
  */
7
- import { useEffect } from 'react';
8
- import useSafeCb from './useSafeCb';
7
+ import { useCallback, useEffect } from 'react';
9
8
  let resizeObserver = null;
10
9
  let observeCatch = null;
11
10
  function init() {
@@ -20,10 +19,14 @@ function init() {
20
19
  });
21
20
  });
22
21
  }
22
+ ;
23
23
  const observe = (target, cb) => {
24
24
  if (!resizeObserver) {
25
25
  init();
26
26
  }
27
+ if (!target) {
28
+ return;
29
+ }
27
30
  observeCatch === null || observeCatch === void 0 ? void 0 : observeCatch.set(target, cb);
28
31
  resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(target);
29
32
  };
@@ -31,20 +34,28 @@ const unobserve = (target) => {
31
34
  if (!resizeObserver) {
32
35
  init();
33
36
  }
37
+ if (!target) {
38
+ return;
39
+ }
34
40
  resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.unobserve(target);
35
41
  observeCatch === null || observeCatch === void 0 ? void 0 : observeCatch.delete(target);
36
42
  };
37
- export default function useResizeObserver(dom, cb) {
38
- const selfObserve = useSafeCb(() => {
39
- observe(dom, cb);
40
- }, [dom, cb]);
41
- const selfUnobserve = useSafeCb(() => {
42
- unobserve(dom);
43
- }, [dom]);
43
+ export const useResizeObserverHandler = () => {
44
+ return [observe, unobserve];
45
+ };
46
+ export const useResizeObserver = (domRef, cb) => {
47
+ const selfUnobserve = useCallback(() => {
48
+ if (domRef.current) {
49
+ unobserve(domRef.current);
50
+ }
51
+ }, [domRef.current]);
44
52
  useEffect(() => {
53
+ if (!domRef.current)
54
+ return;
55
+ const dom = domRef.current;
45
56
  observe(dom, cb);
46
57
  return () => unobserve(dom);
47
- }, [dom, cb]);
48
- return [selfObserve, selfUnobserve];
49
- }
50
- ;
58
+ }, [domRef.current, cb]);
59
+ return selfUnobserve;
60
+ };
61
+ export default useResizeObserver;
package/es/useRtCb.js CHANGED
@@ -1,10 +1,15 @@
1
- import useSafeCb from './useSafeCb';
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-17 00:22:11
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-17 00:22:56
6
+ */
7
+ import { useCallback } from 'react';
2
8
  import useRtRef from './useRtRef';
3
- const useRtCb = (cb, deps) => {
9
+ const useRtCb = (cb, deps = []) => {
4
10
  const ref = useRtRef(cb);
5
- return useSafeCb((...arg) => {
6
- var _a;
7
- return (_a = ref.current) === null || _a === void 0 ? void 0 : _a.call(ref, ...arg);
11
+ return useCallback((...args) => {
12
+ return ref.current(...args);
8
13
  }, deps);
9
14
  };
10
15
  export default useRtCb;
package/es/useRtRef.js CHANGED
@@ -1,7 +1,13 @@
1
- import { useRef } from "react";
2
- export default function useRtRef(val) {
3
- const ref = useRef();
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 22:19:57
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-30 15:05:26
6
+ */
7
+ import { useRef } from 'react';
8
+ const useRtRef = (val) => {
9
+ const ref = useRef(val);
4
10
  ref.current = val;
5
11
  return ref;
6
- }
7
- ;
12
+ };
13
+ export default useRtRef;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:36:09
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-19 22:41:39
6
+ */
7
+ import { useCallback } from 'react';
8
+ import useIsUnmount from "./useIsUnmount";
9
+ const useSafeRunner = () => {
10
+ const isUnmount = useIsUnmount();
11
+ return useCallback((cb, unmountCb) => {
12
+ if (isUnmount()) {
13
+ unmountCb === null || unmountCb === void 0 ? void 0 : unmountCb();
14
+ }
15
+ else {
16
+ cb();
17
+ }
18
+ }, []);
19
+ };
20
+ export default useSafeRunner;
package/es/useThrottle.js CHANGED
@@ -1,19 +1,18 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-19 22:55:28
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-30 15:31:34
6
+ */
1
7
  import { throttle } from 'lodash-es';
2
8
  import { useMemo } from 'react';
3
9
  import useRtCb from './useRtCb';
4
- /**
5
- * 用于节流函数的钩子函数
6
- * @param cb - 要节流的函数
7
- * @param wait - 节流延迟时间,默认为0
8
- * @param opts - 节流选项对象,默认为{}
9
- * @returns 节流函数
10
- */
11
10
  const useThrottle = (cb, wait, opts) => {
12
11
  const rtCb = useRtCb(cb);
13
12
  return useMemo(() => {
14
13
  return throttle((...arg) => {
15
14
  rtCb(...arg);
16
15
  }, wait, opts);
17
- }, []);
16
+ }, [wait, opts]);
18
17
  };
19
18
  export default useThrottle;
package/es/useTimeout.js CHANGED
@@ -1,34 +1,39 @@
1
- /*
1
+ /**
2
2
  * @Author: apathyjade
3
- * @Date: 2023-12-14 16:51:29
3
+ * @Date: 2025-03-19 23:10:13
4
4
  * @Last Modified by: apathyjade
5
- * @Last Modified time: 2023-12-14 17:15:27
5
+ * @Last Modified time: 2025-05-30 18:24:42
6
6
  */
7
7
  import { useCallback, useEffect, useRef } from 'react';
8
8
  import useRtCb from './useRtCb';
9
- /**
10
- * 使用定时器在指定延迟后执行回调函数
11
- * @param callback - 回调函数
12
- * @param delay - 延迟时间(毫秒)
13
- * @returns clearTimer - 清除定时器的函数
14
- */
15
- const useTimeout = (callback, delay) => {
9
+ export const useTimeoutHandler = () => {
16
10
  const timerRef = useRef();
17
- /**
18
- * 清除定时器
19
- */
20
11
  const clearTimer = useCallback(() => {
21
12
  if (timerRef.current) {
22
- clearTimeout(timerRef.current);
13
+ window.clearTimeout(timerRef.current);
14
+ timerRef.current = undefined;
23
15
  }
24
16
  }, []);
17
+ const bindTimer = useCallback((callback, timeout, ...arg) => {
18
+ clearTimer();
19
+ timerRef.current = window.setTimeout(() => {
20
+ timerRef.current = undefined;
21
+ callback(...arg);
22
+ }, timeout);
23
+ }, []);
24
+ useEffect(() => clearTimer);
25
+ return [
26
+ bindTimer,
27
+ clearTimer,
28
+ ];
29
+ };
30
+ export const useTimeout = (callback, timeout, ...arg) => {
31
+ const [bindTimer, clearTimer] = useTimeoutHandler();
25
32
  const cb = useRtCb(callback);
26
33
  useEffect(() => {
27
- timerRef.current = window.setTimeout(cb, delay);
28
- return () => {
29
- clearTimer();
30
- };
31
- }, [delay]);
34
+ bindTimer(cb, timeout, ...arg);
35
+ return clearTimer;
36
+ }, [cb, timeout, ...arg]);
32
37
  return clearTimer;
33
38
  };
34
39
  export default useTimeout;
package/es/useUnmount.js CHANGED
@@ -1,7 +1,13 @@
1
- import { useEffect, useRef } from 'react';
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:35:20
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-18 00:17:09
6
+ */
7
+ import { useEffect } from 'react';
8
+ import useRtRef from './useRtRef';
2
9
  const useUnmount = (cb) => {
3
- const ref = useRef(cb);
4
- ref.current = cb;
10
+ const ref = useRtRef(cb);
5
11
  useEffect(() => {
6
12
  return () => {
7
13
  ref.current();
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:35:13
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-05-30 15:51:14
6
+ */
7
+ import { useRef, useEffect } from 'react';
8
+ const useUpdateEff = (cb, deps) => {
9
+ const ref = useRef(false);
10
+ useEffect(() => {
11
+ if (ref.current) {
12
+ return cb();
13
+ }
14
+ ref.current = true;
15
+ }, deps);
16
+ };
17
+ export default useUpdateEff;
package/es/useValue.js ADDED
@@ -0,0 +1,26 @@
1
+ /*
2
+ * @Author: jade <apathyjade@outlook.com>
3
+ * @Version: 0.0.1
4
+ * @Date: 2023-04-05 14:39:52
5
+ * @Last Modified by: jade
6
+ * @Last Modified Time: 2023-04-05 14:39:52
7
+ */
8
+ import { useState } from 'react';
9
+ import useUpdateEff from './useUpdateEff';
10
+ const defEqual = (a, b) => a === b;
11
+ const useValue = (value, onChange, opts) => {
12
+ const { isEqual = defEqual } = opts || {};
13
+ const [val, setVal] = useState(value);
14
+ useUpdateEff(() => {
15
+ if (!isEqual(value, val)) {
16
+ onChange === null || onChange === void 0 ? void 0 : onChange(val);
17
+ }
18
+ }, [val]);
19
+ useUpdateEff(() => {
20
+ if (!isEqual(value, val)) {
21
+ setVal(value);
22
+ }
23
+ }, [value]);
24
+ return [val, setVal];
25
+ };
26
+ export default useValue;