@zlikemario/helper 0.0.12 → 0.0.13

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.
package/dist/number.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * 数字相关工具函数
3
3
  */
4
4
  import BigNumber from "bignumber.js";
5
+ import type { Numberish, NumberString, MaybeUndefined } from "./types";
5
6
  export { BigNumber };
6
7
  export default BigNumber;
7
8
  /**
@@ -1,3 +1,4 @@
1
1
  export { default as useCatchError } from "./useCatchError";
2
2
  export { default as useLoadingEvent } from "./useLoadingEvent";
3
3
  export { default as useSyncedRef } from "./useSyncedRef";
4
+ export { default as useAsyncData } from "./useAsyncData";
@@ -0,0 +1,6 @@
1
+ import type { MaybeUndefined } from "../types";
2
+ declare const useAsyncData: <R>(getDataFunc: () => Promise<R>, options?: Partial<{
3
+ defaultData: R;
4
+ onError: (error: unknown) => any;
5
+ }>) => [MaybeUndefined<R>, () => void];
6
+ export default useAsyncData;
@@ -1,3 +1,4 @@
1
+ import type { MaybePromise, MaybeUndefined } from "../types";
1
2
  declare const useCatchError: <Args extends any[], R, IsThrowError extends boolean = false>(execute: (...args: Args) => MaybePromise<R>, options?: Partial<{
2
3
  target: string;
3
4
  timeout: number;
package/dist/react.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react"),m=require("./utils.cjs"),a=n=>{const e=o.useRef(n);return e.current=n,o.useMemo(()=>Object.freeze({get current(){return e.current}}),[])},f=(n,e)=>{const u=e?.timeout??2e4,r=a({options:{timeout:u,timeoutError:e?.timeoutError?e?.timeoutError:new Error(`${e?.target?`${e?.target} `:""}Timeout of ${u}ms exceeded`),isThrowError:e?.isThrowError??!1,...e},execute:n});return o.useCallback(async(...s)=>{try{return await m.preventTimeout(Promise.resolve(r.current.execute(...s)),{timeout:r.current.options.timeout,timeoutError:r.current.options.timeoutError})}catch(t){if(r.current.options.onError?.(t,r.current.options.target,...s),r.current.options.isThrowError)throw t;return}},[])},E=(n,e)=>{const[u,r]=o.useState(!1),s=o.useRef(0),t=o.useRef(void 0),c=a({options:{isAllowMulticall:!1,...e},execute:n,isLoading:u}),l=i=>{c.current.isLoading=i,r(i)};return[o.useCallback(async(...i)=>{if(c.current.isLoading&&t.current&&!c.current.options.isAllowMulticall)return t.current;l(!0);const d=s.current+=1;return t.current=c.current.execute(...i).finally(()=>{d===s.current&&(t.current=void 0,l(!1))}),t.current},[]),u]};exports.useCatchError=f;exports.useLoadingEvent=E;exports.useSyncedRef=a;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),d=require("./utils.cjs"),i=s=>{const e=n.useRef(s);return e.current=s,n.useMemo(()=>Object.freeze({get current(){return e.current}}),[])},m=(s,e)=>{const r=e?.timeout??2e4,o=i({options:{timeout:r,timeoutError:e?.timeoutError?e?.timeoutError:new Error(`${e?.target?`${e?.target} `:""}Timeout of ${r}ms exceeded`),isThrowError:e?.isThrowError??!1,...e},execute:s});return n.useCallback(async(...u)=>{try{return await d.preventTimeout(Promise.resolve(o.current.execute(...u)),{timeout:o.current.options.timeout,timeoutError:o.current.options.timeoutError})}catch(t){if(o.current.options.onError?.(t,o.current.options.target,...u),o.current.options.isThrowError)throw t;return}},[])},E=(s,e)=>{const[r,o]=n.useState(!1),u=n.useRef(0),t=n.useRef(void 0),c=i({options:{isAllowMulticall:!1,...e},execute:s,isLoading:r}),l=a=>{c.current.isLoading=a,o(a)};return[n.useCallback(async(...a)=>{if(c.current.isLoading&&t.current&&!c.current.options.isAllowMulticall)return t.current;l(!0);const f=u.current+=1;return t.current=c.current.execute(...a).finally(()=>{f===u.current&&(t.current=void 0,l(!1))}),t.current},[]),r]},y=(s,e)=>{const r=i({execute:s,options:{...e}}),[o,u]=n.useState(r.current.options.defaultData),t=n.useCallback(()=>{d.tryCatchAsync(r.current.execute(),r.current.options.onError??(()=>r.current.options.defaultData)).then(c=>{u(c)})},[]);return n.useEffect(()=>{t()},[]),[o,t]};exports.useAsyncData=y;exports.useCatchError=m;exports.useLoadingEvent=E;exports.useSyncedRef=i;
package/dist/react.js CHANGED
@@ -1,67 +1,80 @@
1
- import { useRef as i, useMemo as f, useCallback as m, useState as E } from "react";
2
- import { preventTimeout as g } from "./utils.js";
3
- const l = (o) => {
4
- const r = i(o);
5
- return r.current = o, f(
1
+ import { useRef as i, useMemo as E, useCallback as a, useState as d, useEffect as p } from "react";
2
+ import { preventTimeout as h, tryCatchAsync as g } from "./utils.js";
3
+ const l = (n) => {
4
+ const t = i(n);
5
+ return t.current = n, E(
6
6
  () => Object.freeze({
7
7
  get current() {
8
- return r.current;
8
+ return t.current;
9
9
  }
10
10
  }),
11
11
  []
12
12
  );
13
- }, v = (o, r) => {
14
- const n = r?.timeout ?? 2e4, e = l({
13
+ }, L = (n, t) => {
14
+ const e = t?.timeout ?? 2e4, o = l({
15
15
  options: {
16
- timeout: n,
17
- timeoutError: r?.timeoutError ? r?.timeoutError : new Error(`${r?.target ? `${r?.target} ` : ""}Timeout of ${n}ms exceeded`),
18
- isThrowError: r?.isThrowError ?? !1,
19
- ...r
16
+ timeout: e,
17
+ timeoutError: t?.timeoutError ? t?.timeoutError : new Error(`${t?.target ? `${t?.target} ` : ""}Timeout of ${e}ms exceeded`),
18
+ isThrowError: t?.isThrowError ?? !1,
19
+ ...t
20
20
  },
21
- execute: o
21
+ execute: n
22
22
  });
23
- return m(async (...u) => {
23
+ return a(async (...s) => {
24
24
  try {
25
- return await g(Promise.resolve(e.current.execute(...u)), {
26
- timeout: e.current.options.timeout,
27
- timeoutError: e.current.options.timeoutError
25
+ return await h(Promise.resolve(o.current.execute(...s)), {
26
+ timeout: o.current.options.timeout,
27
+ timeoutError: o.current.options.timeoutError
28
28
  });
29
- } catch (t) {
30
- if (e.current.options.onError?.(t, e.current.options.target, ...u), e.current.options.isThrowError)
31
- throw t;
29
+ } catch (r) {
30
+ if (o.current.options.onError?.(r, o.current.options.target, ...s), o.current.options.isThrowError)
31
+ throw r;
32
32
  return;
33
33
  }
34
34
  }, []);
35
- }, p = (o, r) => {
36
- const [n, e] = E(!1), u = i(0), t = i(void 0), s = l({
35
+ }, v = (n, t) => {
36
+ const [e, o] = d(!1), s = i(0), r = i(void 0), u = l({
37
37
  options: {
38
38
  /**
39
39
  * 是否允许函数多次调用,默认: false
40
40
  * false 情况下,如果再次执行 execute 会引用上一次的 promise 直接返回
41
41
  */
42
42
  isAllowMulticall: !1,
43
- ...r
43
+ ...t
44
44
  },
45
- execute: o,
46
- isLoading: n
47
- }), a = (c) => {
48
- s.current.isLoading = c, e(c);
45
+ execute: n,
46
+ isLoading: e
47
+ }), m = (c) => {
48
+ u.current.isLoading = c, o(c);
49
49
  };
50
50
  return [
51
- m(async (...c) => {
52
- if (s.current.isLoading && t.current && !s.current.options.isAllowMulticall)
53
- return t.current;
54
- a(!0);
55
- const d = u.current += 1;
56
- return t.current = s.current.execute(...c).finally(() => {
57
- d === u.current && (t.current = void 0, a(!1));
58
- }), t.current;
51
+ a(async (...c) => {
52
+ if (u.current.isLoading && r.current && !u.current.options.isAllowMulticall)
53
+ return r.current;
54
+ m(!0);
55
+ const f = s.current += 1;
56
+ return r.current = u.current.execute(...c).finally(() => {
57
+ f === s.current && (r.current = void 0, m(!1));
58
+ }), r.current;
59
59
  }, []),
60
- n
60
+ e
61
61
  ];
62
+ }, D = (n, t) => {
63
+ const e = l({ execute: n, options: { ...t } }), [o, s] = d(e.current.options.defaultData), r = a(() => {
64
+ g(
65
+ e.current.execute(),
66
+ e.current.options.onError ?? (() => e.current.options.defaultData)
67
+ ).then((u) => {
68
+ s(u);
69
+ });
70
+ }, []);
71
+ return p(() => {
72
+ r();
73
+ }, []), [o, r];
62
74
  };
63
75
  export {
64
- v as useCatchError,
65
- p as useLoadingEvent,
76
+ D as useAsyncData,
77
+ L as useCatchError,
78
+ v as useLoadingEvent,
66
79
  l as useSyncedRef
67
80
  };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 公共类型定义
3
+ */
4
+ export type IntString = `${number}`;
5
+ export type DecimalsString = `${number}.${number}`;
6
+ export type NumberString = IntString | DecimalsString;
7
+ export type Numberish = NumberString | number | bigint;
8
+ export type MaybeNull<T> = T | null;
9
+ export type MaybeUndefined<T> = T | undefined;
10
+ export type MaybePromise<T> = T | PromiseLike<T>;
11
+ export type Defined<T> = T extends undefined ? never : T;
12
+ export type AnyFn = (...args: any[]) => any;
13
+ export type Noop = () => void;
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * 常用操作的工具函数集合
3
3
  */
4
+ import type { MaybeUndefined } from "./types";
4
5
  /**
5
6
  * 延迟指定的时间间隔(毫秒)
6
7
  * @param interval - 延迟的毫秒数,默认 1500ms
@@ -72,7 +73,7 @@ export declare function isEmail(text: string): boolean;
72
73
  * @param p - 要执行的 Promise
73
74
  * @param catchFn - 可选的错误处理函数
74
75
  */
75
- export declare const tryCatchAsync: <T, F extends ((error: unknown) => any) | undefined = undefined>(p: Promise<T>, catchFn?: F) => Promise<T | (F extends (...args: any) => any ? ReturnType<F> : undefined)>;
76
+ export declare const tryCatchAsync: <T, FR = any, F extends ((error: unknown) => FR) | undefined = undefined>(p: Promise<T>, catchFn?: F) => Promise<T | (F extends Function ? FR : undefined)>;
76
77
  /**
77
78
  * 执行回调函数或 Promise,并确保在指定的超时时间内完成
78
79
  * 如果操作未能及时完成,返回的 Promise 将以超时错误被拒绝
@@ -1,2 +1,3 @@
1
1
  export { default as useCatchError } from "./useCatchError";
2
2
  export { default as useLoadingEvent } from "./useLoadingEvent";
3
+ export { default as useAsyncData } from "./useAsyncData";
@@ -0,0 +1,7 @@
1
+ import { type ComputedRef, type MaybeRef } from "vue";
2
+ import type { MaybeUndefined } from "../types";
3
+ declare const useAsyncData: <R>(getDataFunc: () => Promise<R>, options?: Partial<{
4
+ defaultData: MaybeRef<R>;
5
+ onError: (error: unknown) => any;
6
+ }>) => [ComputedRef<MaybeUndefined<R>>, () => void];
7
+ export default useAsyncData;
@@ -1,3 +1,4 @@
1
+ import type { MaybePromise, MaybeUndefined } from "../types";
1
2
  declare const useCatchError: <Args extends any[], R, IsThrowError extends boolean = false>(execute: (...args: Args) => MaybePromise<R>, options?: Partial<{
2
3
  target: string;
3
4
  timeout: number;
package/dist/vue.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./utils.cjs"),s=require("vue"),c=(i,e)=>{const u=e?.timeout??2e4,r={timeout:u,timeoutError:e?.timeoutError?e?.timeoutError:new Error(`${e?.target?`${e?.target} `:""}Timeout of ${u}ms exceeded`),isThrowError:e?.isThrowError??!1,...e};return(async(...o)=>{try{return await n.preventTimeout(Promise.resolve(i(...o)),{timeout:r.timeout,timeoutError:r.timeoutError})}catch(t){if(r.onError?.(t,r.target,...o),r.isThrowError)throw t;return}})},m=(i,e)=>{const u={isAllowMulticall:!1,...e},r=s.ref(!1);let o=0,t;return[async(...l)=>{if(r.value&&t&&!u.isAllowMulticall)return t;r.value=!0;const a=o+=1;return t=i(...l).finally(()=>{a===o&&(t=void 0,r.value=!1)}),t},s.computed(()=>r.value)]};exports.useCatchError=c;exports.useLoadingEvent=m;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./utils.cjs"),a=require("vue"),l=(s,t)=>{const u=t?.timeout??2e4,e={timeout:u,timeoutError:t?.timeoutError?t?.timeoutError:new Error(`${t?.target?`${t?.target} `:""}Timeout of ${u}ms exceeded`),isThrowError:t?.isThrowError??!1,...t};return(async(...o)=>{try{return await i.preventTimeout(Promise.resolve(s(...o)),{timeout:e.timeout,timeoutError:e.timeoutError})}catch(r){if(e.onError?.(r,e.target,...o),e.isThrowError)throw r;return}})},m=(s,t)=>{const u={isAllowMulticall:!1,...t},e=a.ref(!1);let o=0,r;return[async(...n)=>{if(e.value&&r&&!u.isAllowMulticall)return r;e.value=!0;const c=o+=1;return r=s(...n).finally(()=>{c===o&&(r=void 0,e.value=!1)}),r},a.computed(()=>e.value)]},d=(s,t)=>{const u=a.toRef(()=>a.unref(t?.defaultData)),e=a.ref(u.value),o=async()=>{await i.tryCatchAsync(s(),t?.onError??(()=>u.value)).then(r=>{e.value=r})};return o(),[a.computed(()=>e.value),o]};exports.useAsyncData=d;exports.useCatchError=l;exports.useLoadingEvent=m;
package/dist/vue.js CHANGED
@@ -1,47 +1,55 @@
1
- import { preventTimeout as a } from "./utils.js";
2
- import { ref as m, computed as c } from "vue";
3
- const v = (u, e) => {
4
- const i = e?.timeout ?? 2e4, r = {
5
- timeout: i,
6
- timeoutError: e?.timeoutError ? e?.timeoutError : new Error(`${e?.target ? `${e?.target} ` : ""}Timeout of ${i}ms exceeded`),
7
- isThrowError: e?.isThrowError ?? !1,
8
- ...e
1
+ import { preventTimeout as c, tryCatchAsync as m } from "./utils.js";
2
+ import { ref as s, computed as i, toRef as f, unref as v } from "vue";
3
+ const h = (u, t) => {
4
+ const a = t?.timeout ?? 2e4, r = {
5
+ timeout: a,
6
+ timeoutError: t?.timeoutError ? t?.timeoutError : new Error(`${t?.target ? `${t?.target} ` : ""}Timeout of ${a}ms exceeded`),
7
+ isThrowError: t?.isThrowError ?? !1,
8
+ ...t
9
9
  };
10
10
  return (async (...o) => {
11
11
  try {
12
- return await a(Promise.resolve(u(...o)), {
12
+ return await c(Promise.resolve(u(...o)), {
13
13
  timeout: r.timeout,
14
14
  timeoutError: r.timeoutError
15
15
  });
16
- } catch (t) {
17
- if (r.onError?.(t, r.target, ...o), r.isThrowError)
18
- throw t;
16
+ } catch (e) {
17
+ if (r.onError?.(e, r.target, ...o), r.isThrowError)
18
+ throw e;
19
19
  return;
20
20
  }
21
21
  });
22
- }, d = (u, e) => {
23
- const i = {
22
+ }, y = (u, t) => {
23
+ const a = {
24
24
  /**
25
25
  * 是否允许函数多次调用,默认: false
26
26
  * false 情况下,如果再次执行 execute 会引用上一次的 promise 直接返回
27
27
  */
28
28
  isAllowMulticall: !1,
29
- ...e
30
- }, r = m(!1);
31
- let o = 0, t;
29
+ ...t
30
+ }, r = s(!1);
31
+ let o = 0, e;
32
32
  return [
33
- async (...s) => {
34
- if (r.value && t && !i.isAllowMulticall) return t;
33
+ async (...l) => {
34
+ if (r.value && e && !a.isAllowMulticall) return e;
35
35
  r.value = !0;
36
- const l = o += 1;
37
- return t = u(...s).finally(() => {
38
- l === o && (t = void 0, r.value = !1);
39
- }), t;
36
+ const n = o += 1;
37
+ return e = u(...l).finally(() => {
38
+ n === o && (e = void 0, r.value = !1);
39
+ }), e;
40
40
  },
41
- c(() => r.value)
41
+ i(() => r.value)
42
42
  ];
43
+ }, T = (u, t) => {
44
+ const a = f(() => v(t?.defaultData)), r = s(a.value), o = async () => {
45
+ await m(u(), t?.onError ?? (() => a.value)).then((e) => {
46
+ r.value = e;
47
+ });
48
+ };
49
+ return o(), [i(() => r.value), o];
43
50
  };
44
51
  export {
45
- v as useCatchError,
46
- d as useLoadingEvent
52
+ T as useAsyncData,
53
+ h as useCatchError,
54
+ y as useLoadingEvent
47
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlikemario/helper",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "A utility library with number operations and common helper functions",
5
5
  "keywords": [
6
6
  "utility",
@@ -81,6 +81,9 @@
81
81
  "types": "./dist/react.d.ts",
82
82
  "import": "./dist/react.js",
83
83
  "require": "./dist/react.cjs"
84
+ },
85
+ "./types": {
86
+ "types": "./dist/types.d.ts"
84
87
  }
85
88
  },
86
89
  "packageManager": "yarn@4.9.4+sha512.7b1cb0b62abba6a537b3a2ce00811a843bea02bcf53138581a6ae5b1bf563f734872bd47de49ce32a9ca9dcaff995aa789577ffb16811da7c603dcf69e73750b",