vue-asyncx 1.2.0 → 1.4.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.
- package/dist/vue-asyncx.d.ts +16 -7
- package/dist/vue-asyncx.js +99 -57
- package/dist/vue-asyncx.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/vue-asyncx.d.ts
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
import { Ref } from 'vue';
|
|
2
|
+
import { WatchCallback } from 'vue';
|
|
2
3
|
import { WatchOptions } from 'vue';
|
|
3
4
|
import { WatchSource } from 'vue';
|
|
4
5
|
|
|
5
6
|
declare type StringDefaultWhenEmpty<S extends string, D extends string> = S extends '' ? D : S;
|
|
6
7
|
|
|
7
|
-
declare function useAsync<Fn extends (...args: any) => any>(fn: Fn, options?: UseAsyncOptions): UseAsyncResult<Fn, 'method'>;
|
|
8
|
+
declare function useAsync<Fn extends (...args: any) => any>(fn: Fn, options?: UseAsyncOptions<Fn>): UseAsyncResult<Fn, 'method'>;
|
|
8
9
|
|
|
9
|
-
declare function useAsync<Fn extends (...args: any) => any, Name extends string = string>(name: Name, fn: Fn, options?: UseAsyncOptions): UseAsyncResult<Fn, Name>;
|
|
10
|
+
declare function useAsync<Fn extends (...args: any) => any, Name extends string = string>(name: Name, fn: Fn, options?: UseAsyncOptions<Fn>): UseAsyncResult<Fn, Name>;
|
|
10
11
|
export { useAsync }
|
|
11
12
|
export { useAsync as useAsyncFunction }
|
|
12
13
|
|
|
13
|
-
export declare function useAsyncData<Data = any, Fn extends (...args: any) => Data | Promise<Data> | PromiseLike<Data> = (...args: any) => Data | Promise<Data> | PromiseLike<Data>>(fn: Fn, options?: UseAsyncDataOptions): UseAsyncDataResult<Fn, 'data'>;
|
|
14
|
+
export declare function useAsyncData<Data = any, Fn extends (...args: any) => Data | Promise<Data> | PromiseLike<Data> = (...args: any) => Data | Promise<Data> | PromiseLike<Data>>(fn: Fn, options?: UseAsyncDataOptions<Fn>): UseAsyncDataResult<Fn, 'data'>;
|
|
14
15
|
|
|
15
|
-
export declare function useAsyncData<Data = any, Fn extends (...args: any) => Data | Promise<Data> | PromiseLike<Data> = (...args: any) => Data | Promise<Data> | PromiseLike<Data>, DataName extends string = string>(name: DataName, fn: Fn, options?: UseAsyncDataOptions): UseAsyncDataResult<Fn, DataName>;
|
|
16
|
+
export declare function useAsyncData<Data = any, Fn extends (...args: any) => Data | Promise<Data> | PromiseLike<Data> = (...args: any) => Data | Promise<Data> | PromiseLike<Data>, DataName extends string = string>(name: DataName, fn: Fn, options?: UseAsyncDataOptions<Fn>): UseAsyncDataResult<Fn, DataName>;
|
|
16
17
|
|
|
17
|
-
declare type UseAsyncDataOptions = UseAsyncOptions
|
|
18
|
+
declare type UseAsyncDataOptions<Fn extends (...args: any) => any> = UseAsyncOptions<Fn>;
|
|
18
19
|
|
|
19
20
|
declare type UseAsyncDataResult<Fn extends (...args: any) => any, DataName extends string> = UseAsyncResult<Fn, `query${Capitalize<(StringDefaultWhenEmpty<DataName, 'data'>)>}`> & {
|
|
20
21
|
[K in (StringDefaultWhenEmpty<DataName, 'data'>)]: Ref<Awaited<ReturnType<Fn>>>;
|
|
22
|
+
} & {
|
|
23
|
+
[K in `${StringDefaultWhenEmpty<DataName, 'data'>}Expired`]: Ref<boolean>;
|
|
21
24
|
};
|
|
22
25
|
|
|
23
|
-
declare type UseAsyncOptions = {
|
|
26
|
+
declare type UseAsyncOptions<Fn extends (...args: any) => any> = {
|
|
24
27
|
watch?: WatchSource;
|
|
25
|
-
watchOptions?:
|
|
28
|
+
watchOptions?: UseAsyncWatchOptions<Fn>;
|
|
26
29
|
immediate?: boolean;
|
|
27
30
|
};
|
|
28
31
|
|
|
@@ -32,6 +35,12 @@ declare type UseAsyncResult<Fn extends (...args: any) => any, Name extends strin
|
|
|
32
35
|
[K in `${StringDefaultWhenEmpty<Name, 'method'>}Loading`]: Ref<boolean>;
|
|
33
36
|
} & {
|
|
34
37
|
[K in `${StringDefaultWhenEmpty<Name, 'method'>}Arguments`]: Ref<Parameters<Fn>>;
|
|
38
|
+
} & {
|
|
39
|
+
[K in `${StringDefaultWhenEmpty<Name, 'method'>}Error`]: Ref<any>;
|
|
35
40
|
};
|
|
36
41
|
|
|
42
|
+
declare interface UseAsyncWatchOptions<Fn extends (...args: any) => any> extends WatchOptions {
|
|
43
|
+
handlerCreator?: (fn: Fn) => WatchCallback;
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
export { }
|
package/dist/vue-asyncx.js
CHANGED
|
@@ -1,70 +1,112 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
var L = Object.defineProperty, O = Object.defineProperties;
|
|
2
|
+
var b = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var A = Object.getOwnPropertySymbols;
|
|
4
|
+
var $ = Object.prototype.hasOwnProperty, C = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var T = (e, n, t) => n in e ? L(e, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[n] = t, F = (e, n) => {
|
|
6
|
+
for (var t in n || (n = {}))
|
|
7
|
+
$.call(n, t) && T(e, t, n[t]);
|
|
8
|
+
if (A)
|
|
9
|
+
for (var t of A(n))
|
|
10
|
+
C.call(n, t) && T(e, t, n[t]);
|
|
11
|
+
return e;
|
|
12
|
+
}, x = (e, n) => O(e, b(n));
|
|
13
|
+
var H = (e, n) => {
|
|
14
|
+
var t = {};
|
|
15
|
+
for (var r in e)
|
|
16
|
+
$.call(e, r) && n.indexOf(r) < 0 && (t[r] = e[r]);
|
|
17
|
+
if (e != null && A)
|
|
18
|
+
for (var r of A(e))
|
|
19
|
+
n.indexOf(r) < 0 && C.call(e, r) && (t[r] = e[r]);
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
import { ref as v, watch as P, computed as _ } from "vue";
|
|
23
|
+
function j(...e) {
|
|
24
|
+
var w;
|
|
25
|
+
if (!Array.isArray(e) || !e.length) throw TypeError("参数错误:未传递");
|
|
26
|
+
const { name: n, fn: t, options: r } = typeof e[0] == "function" ? { name: "method", fn: e[0], options: e[1] } : { name: e[0] || "method", fn: e[1], options: e[2] };
|
|
27
|
+
if (typeof n != "string") throw TypeError("参数错误:name");
|
|
28
|
+
if (typeof t != "function") throw TypeError("参数错误:fn");
|
|
29
|
+
const i = v(!1), u = v(), h = v(), s = { called: 0, finished: 0 };
|
|
30
|
+
function m(...a) {
|
|
31
|
+
const o = ++s.called, d = (c) => {
|
|
32
|
+
h.value = void 0, i.value = !0, u.value = c;
|
|
33
|
+
}, f = (c, { scene: y, sn: p }) => {
|
|
34
|
+
p > s.finished && (s.finished = p), s.called === s.finished && (y === "error" && (h.value = c), i.value = !1, u.value = void 0);
|
|
26
35
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
d(a);
|
|
37
|
+
try {
|
|
38
|
+
const c = t(...a);
|
|
39
|
+
return c instanceof Promise ? c.then(
|
|
40
|
+
() => f(void 0, { scene: "normal", sn: o }),
|
|
41
|
+
(y) => f(y, { scene: "error", sn: o })
|
|
42
|
+
) : f(void 0, { scene: "normal", sn: o }), c;
|
|
43
|
+
} catch (c) {
|
|
44
|
+
throw f(c, { scene: "error", sn: o }), c;
|
|
45
|
+
}
|
|
30
46
|
}
|
|
31
|
-
if (
|
|
32
|
-
const
|
|
33
|
-
},
|
|
47
|
+
if (r) {
|
|
48
|
+
const a = () => {
|
|
49
|
+
}, l = Object.assign(
|
|
34
50
|
{},
|
|
35
|
-
"immediate" in
|
|
36
|
-
(
|
|
37
|
-
), { watch: f } =
|
|
38
|
-
|
|
51
|
+
"immediate" in r ? { immediate: r.immediate } : {},
|
|
52
|
+
(w = r.watchOptions) != null ? w : {}
|
|
53
|
+
), { handlerCreator: o } = l, d = H(l, ["handlerCreator"]), { watch: f } = r, y = (() => {
|
|
54
|
+
const p = () => m();
|
|
55
|
+
if (typeof o != "function") return p;
|
|
56
|
+
try {
|
|
57
|
+
const E = o(m);
|
|
58
|
+
return typeof E == "function" ? E : p;
|
|
59
|
+
} catch (E) {
|
|
60
|
+
return p;
|
|
61
|
+
}
|
|
62
|
+
})();
|
|
63
|
+
P(f != null ? f : a, y, d);
|
|
39
64
|
}
|
|
40
65
|
return {
|
|
41
|
-
[
|
|
42
|
-
[`${
|
|
43
|
-
[`${
|
|
66
|
+
[n]: m,
|
|
67
|
+
[`${n}Loading`]: i,
|
|
68
|
+
[`${n}Arguments`]: u,
|
|
69
|
+
[`${n}Error`]: h
|
|
44
70
|
};
|
|
45
71
|
}
|
|
46
|
-
function
|
|
47
|
-
if (!
|
|
48
|
-
const
|
|
49
|
-
return
|
|
72
|
+
function q(e) {
|
|
73
|
+
if (!e) return "";
|
|
74
|
+
const n = e[0], t = e.slice(1);
|
|
75
|
+
return n.toUpperCase() + t;
|
|
50
76
|
}
|
|
51
|
-
function
|
|
52
|
-
if (!Array.isArray(
|
|
53
|
-
const { name:
|
|
54
|
-
if (typeof
|
|
55
|
-
if (typeof
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
function k(...e) {
|
|
78
|
+
if (!Array.isArray(e) || !e.length) throw TypeError("参数错误:未传递");
|
|
79
|
+
const { name: n, fn: t, options: r } = typeof e[0] == "function" ? { name: "data", fn: e[0], options: e[1] } : { name: e[0] || "data", fn: e[1], options: e[2] };
|
|
80
|
+
if (typeof n != "string") throw TypeError("参数错误:name");
|
|
81
|
+
if (typeof t != "function") throw TypeError("参数错误:fn");
|
|
82
|
+
const i = v({
|
|
83
|
+
// 调用序号
|
|
84
|
+
called: 0,
|
|
85
|
+
// 调用完成序号
|
|
86
|
+
finished: 0,
|
|
87
|
+
// 更新序号
|
|
88
|
+
updated: 0
|
|
89
|
+
}), u = v(), h = _(() => i.value.updated < i.value.finished);
|
|
90
|
+
function s(l, { scene: a, sn: o }) {
|
|
91
|
+
o > i.value.finished && (i.value.finished = o), a === "update" && o > i.value.updated && (u.value = l, i.value.updated = o);
|
|
92
|
+
}
|
|
93
|
+
function m(...l) {
|
|
94
|
+
const a = ++i.value.called;
|
|
95
|
+
try {
|
|
96
|
+
const o = t(...l);
|
|
97
|
+
return o instanceof Promise ? o.then((d) => s(d, { scene: "update", sn: a }), (d) => s(d, { scene: "error", sn: a })) : s(o, { scene: "update", sn: a }), o;
|
|
98
|
+
} catch (o) {
|
|
99
|
+
throw s(o, { scene: "error", sn: a }), o;
|
|
100
|
+
}
|
|
60
101
|
}
|
|
61
|
-
const
|
|
62
|
-
return
|
|
63
|
-
[
|
|
102
|
+
const w = j(`query${q(n)}`, m, r);
|
|
103
|
+
return x(F({}, w), {
|
|
104
|
+
[n]: u,
|
|
105
|
+
[`${n}Expired`]: h
|
|
64
106
|
});
|
|
65
107
|
}
|
|
66
108
|
export {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
109
|
+
j as useAsync,
|
|
110
|
+
k as useAsyncData,
|
|
111
|
+
j as useAsyncFunction
|
|
70
112
|
};
|
package/dist/vue-asyncx.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(e,n){typeof exports=="object"&&typeof module!="undefined"?n(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],n):(e=typeof globalThis!="undefined"?globalThis:e||self,n(e.VueAsyncx={},e.Vue))})(this,function(e,n){"use strict";var P=Object.defineProperty,q=Object.defineProperties;var D=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,O=Object.prototype.propertyIsEnumerable;var C=(e,n,i)=>n in e?P(e,n,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[n]=i,j=(e,n)=>{for(var i in n||(n={}))F.call(n,i)&&C(e,i,n[i]);if(b)for(var i of b(n))O.call(n,i)&&C(e,i,n[i]);return e},x=(e,n)=>q(e,D(n));var H=(e,n)=>{var i={};for(var f in e)F.call(e,f)&&n.indexOf(f)<0&&(i[f]=e[f]);if(e!=null&&b)for(var f of b(e))n.indexOf(f)<0&&O.call(e,f)&&(i[f]=e[f]);return i};function i(...t){var T;if(!Array.isArray(t)||!t.length)throw TypeError("参数错误:未传递");const{name:r,fn:l,options:p}=typeof t[0]=="function"?{name:"method",fn:t[0],options:t[1]}:{name:t[0]||"method",fn:t[1],options:t[2]};if(typeof r!="string")throw TypeError("参数错误:name");if(typeof l!="function")throw TypeError("参数错误:fn");const s=n.ref(!1),y=n.ref(),w=n.ref(),d={called:0,finished:0};function A(...c){const o=++d.called,h=a=>{w.value=void 0,s.value=!0,y.value=a},u=(a,{scene:E,sn:v})=>{v>d.finished&&(d.finished=v),d.called===d.finished&&(E==="error"&&(w.value=a),s.value=!1,y.value=void 0)};h(c);try{const a=l(...c);return a instanceof Promise?a.then(()=>u(void 0,{scene:"normal",sn:o}),E=>u(E,{scene:"error",sn:o})):u(void 0,{scene:"normal",sn:o}),a}catch(a){throw u(a,{scene:"error",sn:o}),a}}if(p){const c=()=>{},m=Object.assign({},"immediate"in p?{immediate:p.immediate}:{},(T=p.watchOptions)!=null?T:{}),{handlerCreator:o}=m,h=H(m,["handlerCreator"]),{watch:u}=p,E=(()=>{const v=()=>A();if(typeof o!="function")return v;try{const $=o(A);return typeof $=="function"?$:v}catch($){return v}})();n.watch(u!=null?u:c,E,h)}return{[r]:A,[`${r}Loading`]:s,[`${r}Arguments`]:y,[`${r}Error`]:w}}function f(t){if(!t)return"";const r=t[0],l=t.slice(1);return r.toUpperCase()+l}function L(...t){if(!Array.isArray(t)||!t.length)throw TypeError("参数错误:未传递");const{name:r,fn:l,options:p}=typeof t[0]=="function"?{name:"data",fn:t[0],options:t[1]}:{name:t[0]||"data",fn:t[1],options:t[2]};if(typeof r!="string")throw TypeError("参数错误:name");if(typeof l!="function")throw TypeError("参数错误:fn");const s=n.ref({called:0,finished:0,updated:0}),y=n.ref(),w=n.computed(()=>s.value.updated<s.value.finished);function d(m,{scene:c,sn:o}){o>s.value.finished&&(s.value.finished=o),c==="update"&&o>s.value.updated&&(y.value=m,s.value.updated=o)}function A(...m){const c=++s.value.called;try{const o=l(...m);return o instanceof Promise?o.then(h=>d(h,{scene:"update",sn:c}),h=>d(h,{scene:"error",sn:c})):d(o,{scene:"update",sn:c}),o}catch(o){throw d(o,{scene:"error",sn:c}),o}}const T=i(`query${f(r)}`,A,p);return x(j({},T),{[r]:y,[`${r}Expired`]:w})}e.useAsync=i,e.useAsyncData=L,e.useAsyncFunction=i,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
|