@jelper/hooks 0.1.2 → 0.3.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 (65) hide show
  1. package/README.md +31 -31
  2. package/es/index.js +27 -1805
  3. package/es/useAsync.js +33 -0
  4. package/es/useBoolState.js +15 -0
  5. package/es/useCreate.js +5 -0
  6. package/es/useDebounce.js +12 -0
  7. package/es/useInterval.js +25 -0
  8. package/es/useIsUnmount.js +11 -0
  9. package/es/useListener.js +17 -0
  10. package/es/useMount.js +10 -0
  11. package/es/useParamsState.js +9 -0
  12. package/es/useResizeObserver.js +50 -0
  13. package/es/useRtCb.js +10 -0
  14. package/es/useRtEffect.js +7 -0
  15. package/es/useRtRef.js +7 -0
  16. package/es/useRtState.js +28 -0
  17. package/es/useSafeCb.js +11 -0
  18. package/es/useSafeState.js +8 -0
  19. package/es/useThrottle.js +19 -0
  20. package/es/useTimeout.js +34 -0
  21. package/es/useUnmount.js +11 -0
  22. package/es/useUpdateEffect.js +12 -0
  23. package/lib/index.js +52 -1798
  24. package/lib/useAsync.js +50 -0
  25. package/lib/useBoolState.js +20 -0
  26. package/lib/useCreate.js +7 -0
  27. package/lib/useDebounce.js +21 -0
  28. package/lib/useInterval.js +30 -0
  29. package/lib/useIsUnmount.js +13 -0
  30. package/lib/useListener.js +22 -0
  31. package/lib/useMount.js +15 -0
  32. package/lib/useParamsState.js +26 -0
  33. package/lib/useResizeObserver.js +56 -0
  34. package/lib/useRtCb.js +28 -0
  35. package/lib/useRtEffect.js +12 -0
  36. package/lib/useRtRef.js +10 -0
  37. package/lib/useRtState.js +34 -0
  38. package/lib/useSafeCb.js +20 -0
  39. package/lib/useSafeState.js +13 -0
  40. package/lib/useThrottle.js +28 -0
  41. package/lib/useTimeout.js +39 -0
  42. package/lib/useUnmount.js +13 -0
  43. package/lib/useUpdateEffect.js +15 -0
  44. package/package.json +7 -3
  45. package/types/index.d.ts +20 -0
  46. package/types/useAsync.d.ts +13 -0
  47. package/types/useBoolState.d.ts +2 -0
  48. package/types/useCreate.d.ts +2 -0
  49. package/types/useDebounce.d.ts +3 -0
  50. package/types/useInterval.d.ts +2 -0
  51. package/types/useIsUnmount.d.ts +2 -0
  52. package/types/useListener.d.ts +2 -0
  53. package/types/useMount.d.ts +2 -0
  54. package/types/useParamsState.d.ts +6 -0
  55. package/types/useResizeObserver.d.ts +1 -0
  56. package/types/useRtCb.d.ts +2 -0
  57. package/types/useRtEffect.d.ts +2 -0
  58. package/types/useRtRef.d.ts +2 -0
  59. package/types/useRtState.d.ts +6 -0
  60. package/types/useSafeCb.d.ts +2 -0
  61. package/types/useSafeState.d.ts +5 -0
  62. package/types/useThrottle.d.ts +10 -0
  63. package/types/useTimeout.d.ts +8 -0
  64. package/types/useUnmount.d.ts +2 -0
  65. package/types/useUpdateEffect.d.ts +1 -0
package/es/useAsync.js ADDED
@@ -0,0 +1,33 @@
1
+ import useSafeState from './useSafeState';
2
+ import useRtCb from './useRtCb';
3
+ import useCreate from './useCreate';
4
+ const useAsync = (asyncFn, opt = {
5
+ immediate: false,
6
+ 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);
12
+ const run = useRtCb((runParam) => {
13
+ const currParam = opt.catchParam ? Object.assign(Object.assign({}, param), (runParam || {})) : runParam;
14
+ setLoading(true);
15
+ setError(null);
16
+ return asyncFn(currParam)
17
+ .then(opt.format)
18
+ .then((resData) => {
19
+ setData(resData);
20
+ setParam(currParam);
21
+ }, setError)
22
+ .finally(() => {
23
+ setLoading(false);
24
+ });
25
+ });
26
+ useCreate(() => {
27
+ if (opt.immediate) {
28
+ run(param);
29
+ }
30
+ });
31
+ return { data, run, loading, error };
32
+ };
33
+ export default useAsync;
@@ -0,0 +1,15 @@
1
+ import { useCallback } from 'react';
2
+ import useSafeState from './useSafeState';
3
+ const useBoolState = (value) => {
4
+ const [val, setVal] = useSafeState(value);
5
+ const switchFn = useCallback((switchValue) => {
6
+ if (typeof switchValue === 'boolean') {
7
+ setVal(switchValue);
8
+ }
9
+ else {
10
+ setVal((oldVal) => !oldVal);
11
+ }
12
+ }, []);
13
+ return [val, switchFn];
14
+ };
15
+ export default useBoolState;
@@ -0,0 +1,5 @@
1
+ import { useState } from 'react';
2
+ const useCreate = (cb) => {
3
+ useState(cb);
4
+ };
5
+ export default useCreate;
@@ -0,0 +1,12 @@
1
+ import { debounce } from 'lodash-es';
2
+ import { useMemo } from 'react';
3
+ import useRtCb from './useRtCb';
4
+ const useDebounce = (cb, wait, opts) => {
5
+ const rtCb = useRtCb(cb);
6
+ return useMemo(() => {
7
+ return debounce((...arg) => {
8
+ rtCb(...arg);
9
+ }, wait, opts);
10
+ }, []);
11
+ };
12
+ export default useDebounce;
@@ -0,0 +1,25 @@
1
+ /*
2
+ * @Author: apathyjade
3
+ * @Date: 2023-12-14 16:51:29
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2024-03-14 16:32:55
6
+ */
7
+ import { useCallback, useEffect, useRef } from 'react';
8
+ import useRtCb from './useRtCb';
9
+ const useInterval = (callback, delay) => {
10
+ const timerRef = useRef();
11
+ const clearTimer = useCallback(() => {
12
+ if (timerRef.current) {
13
+ window.clearInterval(timerRef.current);
14
+ }
15
+ }, []);
16
+ const cb = useRtCb(callback);
17
+ useEffect(() => {
18
+ timerRef.current = window.setInterval(cb, delay);
19
+ return () => {
20
+ clearTimer();
21
+ };
22
+ }, [delay]);
23
+ return clearTimer;
24
+ };
25
+ export default useInterval;
@@ -0,0 +1,11 @@
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+ const useIsUnmount = () => {
3
+ const ref = useRef(true);
4
+ useEffect(() => {
5
+ return () => {
6
+ ref.current = false;
7
+ };
8
+ }, []);
9
+ return useCallback(() => !ref.current, []);
10
+ };
11
+ export default useIsUnmount;
@@ -0,0 +1,17 @@
1
+ import { useCallback } from 'react';
2
+ import useRtCb from './useRtCb';
3
+ import useRtEffect from './useRtEffect';
4
+ const useListener = (type, listener, options) => {
5
+ const cb = useRtCb(listener);
6
+ const remove = useCallback(() => {
7
+ window.removeEventListener(type, cb);
8
+ }, []);
9
+ useRtEffect(() => {
10
+ window.addEventListener(type, cb, options);
11
+ return () => {
12
+ window.removeEventListener(type, cb);
13
+ };
14
+ });
15
+ return remove;
16
+ };
17
+ export default useListener;
package/es/useMount.js ADDED
@@ -0,0 +1,10 @@
1
+ import { useEffect } from 'react';
2
+ import useRtRef from './useRtRef';
3
+ 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
+ };
10
+ export default useMount;
@@ -0,0 +1,9 @@
1
+ import { useCallback } from 'react';
2
+ import useSafeState from './useSafeState';
3
+ export default function useParamsState(defData) {
4
+ const [data, setData] = useSafeState(defData);
5
+ const update = useCallback((params) => {
6
+ setData((oldVal) => (Object.assign(Object.assign({}, (oldVal || {})), params)));
7
+ }, []);
8
+ return [data, update, setData];
9
+ }
@@ -0,0 +1,50 @@
1
+ /*
2
+ * @Author: apathyjade
3
+ * @Date: 2023-11-24 10:52:51
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2024-03-14 16:34:42
6
+ */
7
+ import { useEffect } from 'react';
8
+ import useSafeCb from './useSafeCb';
9
+ let resizeObserver = null;
10
+ let observeCatch = null;
11
+ function init() {
12
+ if (resizeObserver) {
13
+ return;
14
+ }
15
+ observeCatch = new Map();
16
+ resizeObserver = new window.ResizeObserver((targets) => {
17
+ targets === null || targets === void 0 ? void 0 : targets.forEach((item) => {
18
+ var _a;
19
+ (_a = observeCatch === null || observeCatch === void 0 ? void 0 : observeCatch.get(item === null || item === void 0 ? void 0 : item.target)) === null || _a === void 0 ? void 0 : _a(item);
20
+ });
21
+ });
22
+ }
23
+ const observe = (target, cb) => {
24
+ if (!resizeObserver) {
25
+ init();
26
+ }
27
+ observeCatch === null || observeCatch === void 0 ? void 0 : observeCatch.set(target, cb);
28
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(target);
29
+ };
30
+ const unobserve = (target) => {
31
+ if (!resizeObserver) {
32
+ init();
33
+ }
34
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.unobserve(target);
35
+ observeCatch === null || observeCatch === void 0 ? void 0 : observeCatch.delete(target);
36
+ };
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]);
44
+ useEffect(() => {
45
+ observe(dom, cb);
46
+ return () => unobserve(dom);
47
+ }, [dom, cb]);
48
+ return [selfObserve, selfUnobserve];
49
+ }
50
+ ;
package/es/useRtCb.js ADDED
@@ -0,0 +1,10 @@
1
+ import useSafeCb from './useSafeCb';
2
+ import useRtRef from './useRtRef';
3
+ const useRtCb = (cb, deps) => {
4
+ 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);
8
+ }, deps);
9
+ };
10
+ export default useRtCb;
@@ -0,0 +1,7 @@
1
+ import { useEffect } from 'react';
2
+ import useRtCb from './useRtCb';
3
+ const useRtEffect = (cb, deps) => {
4
+ const rtCb = useRtCb(cb, deps);
5
+ useEffect(rtCb, [rtCb]);
6
+ };
7
+ export default useRtEffect;
package/es/useRtRef.js ADDED
@@ -0,0 +1,7 @@
1
+ import { useRef } from "react";
2
+ export default function useRtRef(val) {
3
+ const ref = useRef();
4
+ ref.current = val;
5
+ return ref;
6
+ }
7
+ ;
@@ -0,0 +1,28 @@
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, useEffect } from 'react';
9
+ import useRtCb from './useRtCb';
10
+ const defEqual = (a, b) => a === b;
11
+ export default function useRtState(value, onChange, opts) {
12
+ const { isEqual = defEqual } = opts || {};
13
+ const [val, setVal] = useState(value);
14
+ const updateValue = useRtCb((value) => {
15
+ setVal(value);
16
+ onChange === null || onChange === void 0 ? void 0 : onChange(value);
17
+ }, []);
18
+ const [oldValue, setOldValue] = useState(value);
19
+ useEffect(() => {
20
+ if (!isEqual(value, oldValue)) {
21
+ setOldValue(value);
22
+ if (!isEqual(value, val)) {
23
+ updateValue(value);
24
+ }
25
+ }
26
+ }, [value, oldValue, val]);
27
+ return [val, setVal];
28
+ }
@@ -0,0 +1,11 @@
1
+ import { useCallback } from 'react';
2
+ import useIsUnmount from './useIsUnmount';
3
+ const useSafeCb = (cb, deps) => {
4
+ const isUnmount = useIsUnmount();
5
+ return useCallback((...arg) => {
6
+ if (!isUnmount()) {
7
+ return cb(...arg);
8
+ }
9
+ }, deps || []);
10
+ };
11
+ export default useSafeCb;
@@ -0,0 +1,8 @@
1
+ import { useState } from 'react';
2
+ import useSafeCb from './useSafeCb';
3
+ const useSafeState = (value) => {
4
+ const [state, setState] = useState(value);
5
+ const setCb = useSafeCb(setState);
6
+ return [state, setCb];
7
+ };
8
+ export default useSafeState;
@@ -0,0 +1,19 @@
1
+ import { throttle } from 'lodash-es';
2
+ import { useMemo } from 'react';
3
+ import useRtCb from './useRtCb';
4
+ /**
5
+ * 用于节流函数的钩子函数
6
+ * @param cb - 要节流的函数
7
+ * @param wait - 节流延迟时间,默认为0
8
+ * @param opts - 节流选项对象,默认为{}
9
+ * @returns 节流函数
10
+ */
11
+ const useThrottle = (cb, wait, opts) => {
12
+ const rtCb = useRtCb(cb);
13
+ return useMemo(() => {
14
+ return throttle((...arg) => {
15
+ rtCb(...arg);
16
+ }, wait, opts);
17
+ }, []);
18
+ };
19
+ export default useThrottle;
@@ -0,0 +1,34 @@
1
+ /*
2
+ * @Author: apathyjade
3
+ * @Date: 2023-12-14 16:51:29
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2023-12-14 17:15:27
6
+ */
7
+ import { useCallback, useEffect, useRef } from 'react';
8
+ import useRtCb from './useRtCb';
9
+ /**
10
+ * 使用定时器在指定延迟后执行回调函数
11
+ * @param callback - 回调函数
12
+ * @param delay - 延迟时间(毫秒)
13
+ * @returns clearTimer - 清除定时器的函数
14
+ */
15
+ const useTimeout = (callback, delay) => {
16
+ const timerRef = useRef();
17
+ /**
18
+ * 清除定时器
19
+ */
20
+ const clearTimer = useCallback(() => {
21
+ if (timerRef.current) {
22
+ clearTimeout(timerRef.current);
23
+ }
24
+ }, []);
25
+ const cb = useRtCb(callback);
26
+ useEffect(() => {
27
+ timerRef.current = window.setTimeout(cb, delay);
28
+ return () => {
29
+ clearTimer();
30
+ };
31
+ }, [delay]);
32
+ return clearTimer;
33
+ };
34
+ export default useTimeout;
@@ -0,0 +1,11 @@
1
+ import { useEffect, useRef } from 'react';
2
+ const useUnmount = (cb) => {
3
+ const ref = useRef(cb);
4
+ ref.current = cb;
5
+ useEffect(() => {
6
+ return () => {
7
+ ref.current();
8
+ };
9
+ }, []);
10
+ };
11
+ export default useUnmount;
@@ -0,0 +1,12 @@
1
+ import { useRef, useEffect } from 'react';
2
+ export default function useUpdateEffect(cb, deps) {
3
+ const ref = useRef(true);
4
+ useEffect(() => {
5
+ if (ref.current) {
6
+ ref.current = false;
7
+ return;
8
+ }
9
+ return cb();
10
+ }, deps);
11
+ }
12
+ ;