@zlikemario/helper 1.0.2 → 1.0.3

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.
@@ -15,5 +15,6 @@ declare const useCatchError: <Args extends any[], R, IsThrowError extends boolea
15
15
  timeoutError: any;
16
16
  isThrowError: IsThrowError;
17
17
  onError: (error: unknown, target: MaybeUndefined<string>, ...args: Args) => any;
18
+ onFinally: (target: MaybeUndefined<string>, ...args: Args) => any;
18
19
  }>) => (...args: Args) => Promise<R | (IsThrowError extends true ? never : undefined)>;
19
20
  export default useCatchError;
package/dist/react.cjs CHANGED
@@ -1 +1 @@
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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react"),d=require("./utils.cjs"),i=s=>{const e=o.useRef(s);return e.current=s,o.useMemo(()=>Object.freeze({get current(){return e.current}}),[])},m=(s,e)=>{const n=e?.timeout??2e4,t=i({options:{timeout:n,timeoutError:e?.timeoutError?e?.timeoutError:new Error(`${e?.target?`${e?.target} `:""}Timeout of ${n}ms exceeded`),isThrowError:e?.isThrowError??!1,...e},execute:s});return o.useCallback(async(...u)=>{try{return await d.preventTimeout(Promise.resolve(t.current.execute(...u)),{timeout:t.current.options.timeout,timeoutError:t.current.options.timeoutError})}catch(r){if(t.current.options.onError?.(r,t.current.options.target,...u),t.current.options.isThrowError)throw r;return}finally{t.current.options.onFinally?.(t.current.options.target,...u)}},[])},E=(s,e)=>{const[n,t]=o.useState(!1),u=o.useRef(0),r=o.useRef(void 0),c=i({options:{isAllowMulticall:!1,...e},execute:s,isLoading:n}),l=a=>{c.current.isLoading=a,t(a)};return[o.useCallback(async(...a)=>{if(c.current.isLoading&&r.current&&!c.current.options.isAllowMulticall)return r.current;l(!0);const f=u.current+=1;return r.current=c.current.execute(...a).finally(()=>{f===u.current&&(r.current=void 0,l(!1))}),r.current},[]),n]},y=(s,e)=>{const n=i({execute:s,options:{...e}}),[t,u]=o.useState(n.current.options.defaultData),r=o.useCallback(()=>{d.tryCatchAsync(n.current.execute(),n.current.options.onError??(()=>n.current.options.defaultData)).then(c=>{u(c)})},[]);return o.useEffect(()=>{r()},[]),[t,r]};exports.useAsyncData=y;exports.useCatchError=m;exports.useLoadingEvent=E;exports.useSyncedRef=i;
package/dist/react.js CHANGED
@@ -1,5 +1,5 @@
1
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";
2
+ import { preventTimeout as y, tryCatchAsync as h } from "./utils.js";
3
3
  const l = (n) => {
4
4
  const t = i(n);
5
5
  return t.current = n, E(
@@ -11,10 +11,10 @@ const l = (n) => {
11
11
  []
12
12
  );
13
13
  }, L = (n, t) => {
14
- const e = t?.timeout ?? 2e4, o = l({
14
+ const o = t?.timeout ?? 2e4, r = l({
15
15
  options: {
16
- timeout: e,
17
- timeoutError: t?.timeoutError ? t?.timeoutError : new Error(`${t?.target ? `${t?.target} ` : ""}Timeout of ${e}ms exceeded`),
16
+ timeout: o,
17
+ timeoutError: t?.timeoutError ? t?.timeoutError : new Error(`${t?.target ? `${t?.target} ` : ""}Timeout of ${o}ms exceeded`),
18
18
  isThrowError: t?.isThrowError ?? !1,
19
19
  ...t
20
20
  },
@@ -22,18 +22,20 @@ const l = (n) => {
22
22
  });
23
23
  return a(async (...s) => {
24
24
  try {
25
- return await h(Promise.resolve(o.current.execute(...s)), {
26
- timeout: o.current.options.timeout,
27
- timeoutError: o.current.options.timeoutError
25
+ return await y(Promise.resolve(r.current.execute(...s)), {
26
+ timeout: r.current.options.timeout,
27
+ timeoutError: r.current.options.timeoutError
28
28
  });
29
- } catch (r) {
30
- if (o.current.options.onError?.(r, o.current.options.target, ...s), o.current.options.isThrowError)
31
- throw r;
29
+ } catch (e) {
30
+ if (r.current.options.onError?.(e, r.current.options.target, ...s), r.current.options.isThrowError)
31
+ throw e;
32
32
  return;
33
+ } finally {
34
+ r.current.options.onFinally?.(r.current.options.target, ...s);
33
35
  }
34
36
  }, []);
35
37
  }, v = (n, t) => {
36
- const [e, o] = d(!1), s = i(0), r = i(void 0), u = l({
38
+ const [o, r] = d(!1), s = i(0), e = i(void 0), u = l({
37
39
  options: {
38
40
  /**
39
41
  * 是否允许函数多次调用,默认: false
@@ -43,34 +45,34 @@ const l = (n) => {
43
45
  ...t
44
46
  },
45
47
  execute: n,
46
- isLoading: e
48
+ isLoading: o
47
49
  }), m = (c) => {
48
- u.current.isLoading = c, o(c);
50
+ u.current.isLoading = c, r(c);
49
51
  };
50
52
  return [
51
53
  a(async (...c) => {
52
- if (u.current.isLoading && r.current && !u.current.options.isAllowMulticall)
53
- return r.current;
54
+ if (u.current.isLoading && e.current && !u.current.options.isAllowMulticall)
55
+ return e.current;
54
56
  m(!0);
55
57
  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;
58
+ return e.current = u.current.execute(...c).finally(() => {
59
+ f === s.current && (e.current = void 0, m(!1));
60
+ }), e.current;
59
61
  }, []),
60
- e
62
+ o
61
63
  ];
62
64
  }, 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)
65
+ const o = l({ execute: n, options: { ...t } }), [r, s] = d(o.current.options.defaultData), e = a(() => {
66
+ h(
67
+ o.current.execute(),
68
+ o.current.options.onError ?? (() => o.current.options.defaultData)
67
69
  ).then((u) => {
68
70
  s(u);
69
71
  });
70
72
  }, []);
71
73
  return p(() => {
72
- r();
73
- }, []), [o, r];
74
+ e();
75
+ }, []), [r, e];
74
76
  };
75
77
  export {
76
78
  D as useAsyncData,
@@ -15,5 +15,6 @@ declare const useCatchError: <Args extends any[], R, IsThrowError extends boolea
15
15
  timeoutError: any;
16
16
  isThrowError: IsThrowError;
17
17
  onError: (error: unknown, target: MaybeUndefined<string>, ...args: Args) => any;
18
+ onFinally: (target: MaybeUndefined<string>, ...args: Args) => any;
18
19
  }>) => (...args: Args) => Promise<IsThrowError extends true ? R : MaybeUndefined<R>>;
19
20
  export default useCatchError;
package/dist/vue.cjs CHANGED
@@ -1 +1 @@
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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./utils.cjs"),u=require("vue"),c=(s,t)=>{const a=t?.timeout??2e4,e={timeout:a,timeoutError:t?.timeoutError?t?.timeoutError:new Error(`${t?.target?`${t?.target} `:""}Timeout of ${a}ms exceeded`),isThrowError:t?.isThrowError??!1,...t};return(async(...o)=>{try{return await n.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}finally{e.onFinally?.(e.target,...o)}})},m=(s,t)=>{const a={isAllowMulticall:!1,...t},e=u.ref(!1);let o=0,r;return[async(...i)=>{if(e.value&&r&&!a.isAllowMulticall)return r;e.value=!0;const l=o+=1;return r=s(...i).finally(()=>{l===o&&(r=void 0,e.value=!1)}),r},u.computed(()=>e.value)]},d=(s,t)=>{const a=u.toRef(()=>u.unref(t?.defaultData)),e=u.ref(a.value),o=async()=>{await n.tryCatchAsync(s(),t?.onError??(()=>a.value)).then(r=>{e.value=r})};return o(),[u.computed(()=>e.value),o]};exports.useAsyncData=d;exports.useCatchError=c;exports.useLoadingEvent=m;
package/dist/vue.js CHANGED
@@ -1,6 +1,6 @@
1
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) => {
2
+ import { ref as i, computed as l, toRef as f, unref as v } from "vue";
3
+ const w = (u, t) => {
4
4
  const a = t?.timeout ?? 2e4, r = {
5
5
  timeout: a,
6
6
  timeoutError: t?.timeoutError ? t?.timeoutError : new Error(`${t?.target ? `${t?.target} ` : ""}Timeout of ${a}ms exceeded`),
@@ -17,9 +17,11 @@ const h = (u, t) => {
17
17
  if (r.onError?.(e, r.target, ...o), r.isThrowError)
18
18
  throw e;
19
19
  return;
20
+ } finally {
21
+ r.onFinally?.(r.target, ...o);
20
22
  }
21
23
  });
22
- }, y = (u, t) => {
24
+ }, h = (u, t) => {
23
25
  const a = {
24
26
  /**
25
27
  * 是否允许函数多次调用,默认: false
@@ -27,29 +29,29 @@ const h = (u, t) => {
27
29
  */
28
30
  isAllowMulticall: !1,
29
31
  ...t
30
- }, r = s(!1);
32
+ }, r = i(!1);
31
33
  let o = 0, e;
32
34
  return [
33
- async (...l) => {
35
+ async (...s) => {
34
36
  if (r.value && e && !a.isAllowMulticall) return e;
35
37
  r.value = !0;
36
38
  const n = o += 1;
37
- return e = u(...l).finally(() => {
39
+ return e = u(...s).finally(() => {
38
40
  n === o && (e = void 0, r.value = !1);
39
41
  }), e;
40
42
  },
41
- i(() => r.value)
43
+ l(() => r.value)
42
44
  ];
43
45
  }, T = (u, t) => {
44
- const a = f(() => v(t?.defaultData)), r = s(a.value), o = async () => {
46
+ const a = f(() => v(t?.defaultData)), r = i(a.value), o = async () => {
45
47
  await m(u(), t?.onError ?? (() => a.value)).then((e) => {
46
48
  r.value = e;
47
49
  });
48
50
  };
49
- return o(), [i(() => r.value), o];
51
+ return o(), [l(() => r.value), o];
50
52
  };
51
53
  export {
52
54
  T as useAsyncData,
53
- h as useCatchError,
54
- y as useLoadingEvent
55
+ w as useCatchError,
56
+ h as useLoadingEvent
55
57
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlikemario/helper",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A utility library with number operations and common helper functions",
5
5
  "keywords": [
6
6
  "utility",