@jelper/hooks 0.2.0 → 1.0.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 (69) hide show
  1. package/README.md +31 -31
  2. package/es/index.js +25 -1779
  3. package/es/useAsync.js +73 -0
  4. package/es/useBoolState.js +23 -0
  5. package/es/useCreate.js +11 -0
  6. package/es/useDebounce.js +18 -0
  7. package/es/useIgnoreAbortErrCb.js +16 -0
  8. package/es/useInterval.js +29 -0
  9. package/es/useIsMounted.js +18 -0
  10. package/es/useIsUnmounted.js +17 -0
  11. package/es/useMount.js +11 -0
  12. package/es/useOption.js +19 -0
  13. package/es/useResizeObserver.js +50 -0
  14. package/es/useRtCb.js +15 -0
  15. package/es/useRtRef.js +13 -0
  16. package/es/useSafeRunner.js +20 -0
  17. package/es/useThrottle.js +18 -0
  18. package/es/useTimeout.js +29 -0
  19. package/es/useUnmount.js +17 -0
  20. package/es/useUpdateEff.js +17 -0
  21. package/es/useValue.js +29 -0
  22. package/lib/index.js +48 -1773
  23. package/lib/useAsync.js +90 -0
  24. package/lib/useBoolState.js +25 -0
  25. package/lib/useCreate.js +13 -0
  26. package/lib/useDebounce.js +27 -0
  27. package/lib/useIgnoreAbortErrCb.js +19 -0
  28. package/lib/useInterval.js +35 -0
  29. package/lib/useIsMounted.js +20 -0
  30. package/lib/useIsUnmounted.js +19 -0
  31. package/lib/useMount.js +13 -0
  32. package/lib/useOption.js +33 -0
  33. package/lib/useResizeObserver.js +53 -0
  34. package/lib/useRtCb.js +25 -0
  35. package/lib/useRtRef.js +15 -0
  36. package/lib/useSafeRunner.js +25 -0
  37. package/lib/useThrottle.js +27 -0
  38. package/lib/useTimeout.js +35 -0
  39. package/lib/useUnmount.js +22 -0
  40. package/lib/useUpdateEff.js +19 -0
  41. package/lib/useValue.js +34 -0
  42. package/package.json +13 -5
  43. package/types/index.d.ts +8 -10
  44. package/types/useAsync.d.ts +22 -8
  45. package/types/useBoolState.d.ts +7 -1
  46. package/types/useCreate.d.ts +7 -1
  47. package/types/useDebounce.d.ts +6 -0
  48. package/types/useIgnoreAbortErrCb.d.ts +12 -0
  49. package/types/useInterval.d.ts +4 -1
  50. package/types/useIsMounted.d.ts +8 -0
  51. package/types/useIsUnmounted.d.ts +8 -0
  52. package/types/useMount.d.ts +7 -1
  53. package/types/useOption.d.ts +15 -0
  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 +5 -6
  58. package/types/useTimeout.d.ts +8 -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/types/useIsUnmount.d.ts +0 -2
  63. package/types/useListener.d.ts +0 -2
  64. package/types/useParamsState.d.ts +0 -6
  65. package/types/useRtEffect.d.ts +0 -2
  66. package/types/useRtState.d.ts +0 -6
  67. package/types/useSafeCb.d.ts +0 -2
  68. package/types/useSafeState.d.ts +0 -3
  69. package/types/useUpdateEffect.d.ts +0 -1
@@ -1,13 +1,27 @@
1
- interface Opt<T extends (...arg: any) => any, R> {
2
- defParam?: Parameters<T>;
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-18 23:44:38
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-19 22:53:22
6
+ */
7
+ type Parameter<T extends (p: any) => any> = Parameters<T>[0];
8
+ interface Opt<T extends (p: any) => any, R> {
9
+ defParam?: Parameter<T>;
3
10
  immediate?: boolean;
4
11
  format?: (p: ReturnType<T>) => R;
5
12
  catchParam?: boolean;
13
+ onAbort?: (this: AbortSignal, ev: Event) => any;
6
14
  }
7
- declare const useAsync: <T extends (...arg: any) => Promise<any>, R extends Object>(asyncFn: T, opt?: Opt<T, R>) => {
8
- data: R | null | undefined;
9
- run: ReturnFn<(runParam?: Partial<Parameters<T>> | undefined) => Promise<void>>;
10
- loading: boolean | undefined;
11
- error: null | undefined;
12
- };
15
+ declare const useAsync: <T extends (p: any, opt?: {
16
+ signal: AbortController['signal'];
17
+ }) => Promise<any>, R = any>(asyncFn: T, opt?: Opt<T, R>) => [
18
+ R | undefined,
19
+ {
20
+ run: (runParam?: Partial<Parameter<T>>) => Promise<void>;
21
+ refresh: () => Promise<void>;
22
+ loading: boolean;
23
+ error?: Error;
24
+ param: Partial<Parameter<T>>;
25
+ }
26
+ ];
13
27
  export default useAsync;
@@ -1,2 +1,8 @@
1
- declare const useBoolState: (value: boolean) => (boolean | ((switchValue?: React.SetStateAction<boolean>) => void) | undefined)[];
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:16:34
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-16 11:21:54
6
+ */
7
+ declare const useBoolState: (value: boolean) => (boolean | ((switchValue?: React.SetStateAction<boolean>) => void))[];
2
8
  export default useBoolState;
@@ -1,2 +1,8 @@
1
- declare const useCreate: (cb: Function) => void;
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
+ */
7
+ declare const useCreate: (cb: () => void) => void;
2
8
  export default useCreate;
@@ -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-03-19 22:55:14
6
+ */
1
7
  import { DebounceSettings, DebouncedFunc } from 'lodash-es';
2
8
  declare const useDebounce: <T extends (...args: any) => any>(cb: T, wait?: number, opts?: DebounceSettings) => DebouncedFunc<T>;
3
9
  export default useDebounce;
@@ -0,0 +1,12 @@
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
+ /// <reference types="react" />
8
+ interface Opts<T> {
9
+ onAbort: (err: Error) => T;
10
+ }
11
+ declare const useIgnoreAbortErrCb: <T = any>(cb: (err: Error) => T, deps?: import("react").DependencyList, opts?: Opts<T>) => (err: Error) => T | undefined;
12
+ export default useIgnoreAbortErrCb;
@@ -1,2 +1,5 @@
1
- declare const useInterval: (callback: DefFn, delay: number) => () => void;
1
+ declare const useInterval: () => [
2
+ (callback: Function, timeout?: number, ...arg: any[]) => void,
3
+ () => void
4
+ ];
2
5
  export default useInterval;
@@ -0,0 +1,8 @@
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
+ declare const useIsMounted: () => (() => boolean);
8
+ export default useIsMounted;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 21:33:24
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-16 21:35:46
6
+ */
7
+ declare const useIsUnmounted: () => (() => boolean);
8
+ export default useIsUnmounted;
@@ -1,2 +1,8 @@
1
- declare const useMount: (cb: Function) => void;
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
+ */
7
+ declare const useMount: (cb: () => void) => void;
2
8
  export default useMount;
@@ -0,0 +1,15 @@
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
+ declare const useOption: <T extends {}>(defOpt?: Partial<T>) => [
8
+ Partial<T>,
9
+ {
10
+ reset: () => void;
11
+ setOption: React.Dispatch<React.SetStateAction<Partial<T>>>;
12
+ update: React.Dispatch<React.SetStateAction<Partial<T>>>;
13
+ }
14
+ ];
15
+ export default useOption;
@@ -1,2 +1,9 @@
1
- declare const useRtCb: <T extends DefFn, D extends any[]>(cb: T, deps?: D | undefined) => ReturnFn<T>;
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 { DependencyList } from 'react';
8
+ declare const useRtCb: <T extends Function>(cb: T, deps?: DependencyList) => T;
2
9
  export default useRtCb;
@@ -1,2 +1,9 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 22:19:57
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-17 00:10:45
6
+ */
1
7
  /// <reference types="react" />
2
- export default function useRtRef<T>(val: T): import("react").MutableRefObject<T | undefined>;
8
+ declare const useRtRef: <T>(val: T) => import("react").MutableRefObject<T>;
9
+ export default useRtRef;
@@ -0,0 +1,8 @@
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
+ declare const useSafeRunner: () => (cb: () => void, unmountCb?: () => void) => void;
8
+ export default useSafeRunner;
@@ -1,10 +1,9 @@
1
- import { ThrottleSettings } from 'lodash-es';
2
1
  /**
3
- * 用于节流函数的钩子函数
4
- * @param cb - 要节流的函数
5
- * @param wait - 节流延迟时间,默认为0
6
- * @param opts - 节流选项对象,默认为{}
7
- * @returns 节流函数
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-19 22:55:28
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-19 22:55:48
8
6
  */
7
+ import { ThrottleSettings } from 'lodash-es';
9
8
  declare const useThrottle: <T extends (...args: any) => any>(cb: T, wait?: number, opts?: ThrottleSettings) => import("lodash-es").DebouncedFunc<(...arg: Parameters<T>) => void>;
10
9
  export default useThrottle;
@@ -1,8 +1,11 @@
1
1
  /**
2
- * 使用定时器在指定延迟后执行回调函数
3
- * @param callback - 回调函数
4
- * @param delay - 延迟时间(毫秒)
5
- * @returns clearTimer - 清除定时器的函数
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-19 23:10:13
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-19 23:10:13
6
6
  */
7
- declare const useTimeout: (callback: DefFn, delay: number) => () => void;
7
+ declare const useTimeout: () => [
8
+ (callback: Function, timeout?: number, ...arg: any[]) => void,
9
+ () => void
10
+ ];
8
11
  export default useTimeout;
@@ -1,2 +1,8 @@
1
- declare const useUnmount: (cb: Function) => void;
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
+ declare const useUnmount: (cb: () => void) => void;
2
8
  export default useUnmount;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @Author: apathyjade
3
+ * @Date: 2025-03-16 11:35:13
4
+ * @Last Modified by: apathyjade
5
+ * @Last Modified time: 2025-03-16 11:35:13
6
+ */
7
+ declare const useUpdateEffect: (cb: React.EffectCallback, deps?: React.DependencyList) => void;
8
+ export default useUpdateEffect;
@@ -0,0 +1,8 @@
1
+ interface Options<T> {
2
+ isEqual?: (a: T, b: T) => boolean;
3
+ }
4
+ declare const useValue: <T = any>(value: T, onChange?: (value: T) => void, opts?: Options<T>) => [
5
+ T,
6
+ React.Dispatch<React.SetStateAction<T>>
7
+ ];
8
+ export default useValue;
@@ -1,2 +0,0 @@
1
- declare const useIsUnmount: () => (() => boolean);
2
- export default useIsUnmount;
@@ -1,2 +0,0 @@
1
- declare const useListener: (type: keyof WindowEventMap, listener: () => any, options?: boolean | AddEventListenerOptions) => () => void;
2
- export default useListener;
@@ -1,6 +0,0 @@
1
- /// <reference types="react" />
2
- export default function useParamsState<T extends object>(defData: T): [
3
- d: T | undefined,
4
- u: React.Dispatch<React.SetStateAction<Partial<T | undefined>>>,
5
- s: React.Dispatch<React.SetStateAction<T | undefined>>
6
- ];
@@ -1,2 +0,0 @@
1
- declare const useRtEffect: (cb: any, deps?: any[]) => void;
2
- export default useRtEffect;
@@ -1,6 +0,0 @@
1
- /// <reference types="react" />
2
- interface Options<T> {
3
- isEqual?: (a: T, b: T) => boolean;
4
- }
5
- export default function useRtState<T = any>(value: T, onChange?: (value: T) => void, opts?: Options<T>): (T | import("react").Dispatch<import("react").SetStateAction<T>>)[];
6
- export {};
@@ -1,2 +0,0 @@
1
- declare const useSafeCb: <T extends DefFn, D extends any[]>(cb: T, deps?: D | undefined) => T;
2
- export default useSafeCb;
@@ -1,3 +0,0 @@
1
- /// <reference types="react" />
2
- declare const useSafeState: <T>(value?: T | undefined) => [s: T | undefined, setFn: import("react").Dispatch<import("react").SetStateAction<T | undefined>>];
3
- export default useSafeState;
@@ -1 +0,0 @@
1
- export default function useUpdateEffect(cb: () => void, deps: any[]): void;