@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 +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/useAsyncData.d.ts +6 -0
- package/dist/react/useCatchError.d.ts +1 -0
- package/dist/react.cjs +1 -1
- package/dist/react.js +51 -38
- package/dist/types.d.ts +13 -0
- package/dist/utils.d.ts +2 -1
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/useAsyncData.d.ts +7 -0
- package/dist/vue/useCatchError.d.ts +1 -0
- package/dist/vue.cjs +1 -1
- package/dist/vue.js +34 -26
- package/package.json +4 -1
package/dist/number.d.ts
CHANGED
package/dist/react/index.d.ts
CHANGED
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 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
|
|
2
|
-
import { preventTimeout as g } from "./utils.js";
|
|
3
|
-
const l = (
|
|
4
|
-
const
|
|
5
|
-
return
|
|
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
|
|
8
|
+
return t.current;
|
|
9
9
|
}
|
|
10
10
|
}),
|
|
11
11
|
[]
|
|
12
12
|
);
|
|
13
|
-
},
|
|
14
|
-
const
|
|
13
|
+
}, L = (n, t) => {
|
|
14
|
+
const e = t?.timeout ?? 2e4, o = l({
|
|
15
15
|
options: {
|
|
16
|
-
timeout:
|
|
17
|
-
timeoutError:
|
|
18
|
-
isThrowError:
|
|
19
|
-
...
|
|
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:
|
|
21
|
+
execute: n
|
|
22
22
|
});
|
|
23
|
-
return
|
|
23
|
+
return a(async (...s) => {
|
|
24
24
|
try {
|
|
25
|
-
return await
|
|
26
|
-
timeout:
|
|
27
|
-
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 (
|
|
30
|
-
if (
|
|
31
|
-
throw
|
|
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
|
-
},
|
|
36
|
-
const [
|
|
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
|
-
...
|
|
43
|
+
...t
|
|
44
44
|
},
|
|
45
|
-
execute:
|
|
46
|
-
isLoading:
|
|
47
|
-
}),
|
|
48
|
-
|
|
45
|
+
execute: n,
|
|
46
|
+
isLoading: e
|
|
47
|
+
}), m = (c) => {
|
|
48
|
+
u.current.isLoading = c, o(c);
|
|
49
49
|
};
|
|
50
50
|
return [
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
return
|
|
57
|
-
|
|
58
|
-
}),
|
|
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
|
-
|
|
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
|
-
|
|
65
|
-
|
|
76
|
+
D as useAsyncData,
|
|
77
|
+
L as useCatchError,
|
|
78
|
+
v as useLoadingEvent,
|
|
66
79
|
l as useSyncedRef
|
|
67
80
|
};
|
package/dist/types.d.ts
ADDED
|
@@ -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) =>
|
|
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 将以超时错误被拒绝
|
package/dist/vue/index.d.ts
CHANGED
|
@@ -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;
|
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 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
|
|
2
|
-
import { ref as
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
timeout:
|
|
6
|
-
timeoutError:
|
|
7
|
-
isThrowError:
|
|
8
|
-
...
|
|
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
|
|
12
|
+
return await c(Promise.resolve(u(...o)), {
|
|
13
13
|
timeout: r.timeout,
|
|
14
14
|
timeoutError: r.timeoutError
|
|
15
15
|
});
|
|
16
|
-
} catch (
|
|
17
|
-
if (r.onError?.(
|
|
18
|
-
throw
|
|
16
|
+
} catch (e) {
|
|
17
|
+
if (r.onError?.(e, r.target, ...o), r.isThrowError)
|
|
18
|
+
throw e;
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
|
-
},
|
|
23
|
-
const
|
|
22
|
+
}, y = (u, t) => {
|
|
23
|
+
const a = {
|
|
24
24
|
/**
|
|
25
25
|
* 是否允许函数多次调用,默认: false
|
|
26
26
|
* false 情况下,如果再次执行 execute 会引用上一次的 promise 直接返回
|
|
27
27
|
*/
|
|
28
28
|
isAllowMulticall: !1,
|
|
29
|
-
...
|
|
30
|
-
}, r =
|
|
31
|
-
let o = 0,
|
|
29
|
+
...t
|
|
30
|
+
}, r = s(!1);
|
|
31
|
+
let o = 0, e;
|
|
32
32
|
return [
|
|
33
|
-
async (...
|
|
34
|
-
if (r.value &&
|
|
33
|
+
async (...l) => {
|
|
34
|
+
if (r.value && e && !a.isAllowMulticall) return e;
|
|
35
35
|
r.value = !0;
|
|
36
|
-
const
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
}),
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
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.
|
|
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",
|