@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.
- package/dist/react/useCatchError.d.ts +1 -0
- package/dist/react.cjs +1 -1
- package/dist/react.js +27 -25
- package/dist/vue/useCatchError.d.ts +1 -0
- package/dist/vue.cjs +1 -1
- package/dist/vue.js +13 -11
- package/package.json +1 -1
|
@@ -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
|
|
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
|
|
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
|
|
14
|
+
const o = t?.timeout ?? 2e4, r = l({
|
|
15
15
|
options: {
|
|
16
|
-
timeout:
|
|
17
|
-
timeoutError: t?.timeoutError ? t?.timeoutError : new Error(`${t?.target ? `${t?.target} ` : ""}Timeout of ${
|
|
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
|
|
26
|
-
timeout:
|
|
27
|
-
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 (
|
|
30
|
-
if (
|
|
31
|
-
throw
|
|
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 [
|
|
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:
|
|
48
|
+
isLoading: o
|
|
47
49
|
}), m = (c) => {
|
|
48
|
-
u.current.isLoading = c,
|
|
50
|
+
u.current.isLoading = c, r(c);
|
|
49
51
|
};
|
|
50
52
|
return [
|
|
51
53
|
a(async (...c) => {
|
|
52
|
-
if (u.current.isLoading &&
|
|
53
|
-
return
|
|
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
|
|
57
|
-
f === s.current && (
|
|
58
|
-
}),
|
|
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
|
-
|
|
62
|
+
o
|
|
61
63
|
];
|
|
62
64
|
}, D = (n, t) => {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
73
|
-
}, []), [
|
|
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
|
|
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
|
|
3
|
-
const
|
|
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
|
-
},
|
|
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 =
|
|
32
|
+
}, r = i(!1);
|
|
31
33
|
let o = 0, e;
|
|
32
34
|
return [
|
|
33
|
-
async (...
|
|
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(...
|
|
39
|
+
return e = u(...s).finally(() => {
|
|
38
40
|
n === o && (e = void 0, r.value = !1);
|
|
39
41
|
}), e;
|
|
40
42
|
},
|
|
41
|
-
|
|
43
|
+
l(() => r.value)
|
|
42
44
|
];
|
|
43
45
|
}, T = (u, t) => {
|
|
44
|
-
const a = f(() => v(t?.defaultData)), r =
|
|
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(), [
|
|
51
|
+
return o(), [l(() => r.value), o];
|
|
50
52
|
};
|
|
51
53
|
export {
|
|
52
54
|
T as useAsyncData,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
w as useCatchError,
|
|
56
|
+
h as useLoadingEvent
|
|
55
57
|
};
|