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.
@@ -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?: 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 { }
@@ -1,70 +1,112 @@
1
- var w = Object.defineProperty, A = Object.defineProperties;
2
- var v = Object.getOwnPropertyDescriptors;
3
- var y = Object.getOwnPropertySymbols;
4
- var E = Object.prototype.hasOwnProperty, T = Object.prototype.propertyIsEnumerable;
5
- var l = (n, t, o) => t in n ? w(n, t, { enumerable: !0, configurable: !0, writable: !0, value: o }) : n[t] = o, h = (n, t) => {
6
- for (var o in t || (t = {}))
7
- E.call(t, o) && l(n, o, t[o]);
8
- if (y)
9
- for (var o of y(t))
10
- T.call(t, o) && l(n, o, t[o]);
11
- return n;
12
- }, d = (n, t) => A(n, v(t));
13
- import { ref as m, watch as O } from "vue";
14
- function $(...n) {
15
- var p;
16
- if (!Array.isArray(n) || !n.length) throw TypeError("参数错误:未传递");
17
- const { name: t, fn: o, options: i } = typeof n[0] == "function" ? { name: "method", fn: n[0], options: n[1] } : { name: n[0] || "method", fn: n[1], options: n[2] };
18
- if (typeof t != "string") throw TypeError("参数错误:name");
19
- if (typeof o != "function") throw TypeError("参数错误:fn");
20
- const r = m(!1), c = m();
21
- function a(...e) {
22
- const s = () => {
23
- r.value = !0, c.value = e;
24
- }, f = () => {
25
- r.value = !1, c.value = void 0;
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
- s();
28
- const u = o(...e);
29
- return u instanceof Promise ? u.finally(() => f()) : f(), u;
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 (i) {
32
- const e = () => {
33
- }, s = Object.assign(
47
+ if (r) {
48
+ const a = () => {
49
+ }, l = Object.assign(
34
50
  {},
35
- "immediate" in i ? { immediate: i.immediate } : {},
36
- (p = i.watchOptions) != null ? p : {}
37
- ), { watch: f } = i;
38
- O(f != null ? f : e, () => a(), s);
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
- [t]: a,
42
- [`${t}Loading`]: r,
43
- [`${t}Arguments`]: c
66
+ [n]: m,
67
+ [`${n}Loading`]: i,
68
+ [`${n}Arguments`]: u,
69
+ [`${n}Error`]: h
44
70
  };
45
71
  }
46
- function b(n) {
47
- if (!n) return "";
48
- const t = n[0], o = n.slice(1);
49
- return t.toUpperCase() + o;
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 j(...n) {
52
- if (!Array.isArray(n) || !n.length) throw TypeError("参数错误:未传递");
53
- const { name: t, fn: o, options: i } = typeof n[0] == "function" ? { name: "data", fn: n[0], options: n[1] } : { name: n[0] || "data", fn: n[1], options: n[2] };
54
- if (typeof t != "string") throw TypeError("参数错误:name");
55
- if (typeof o != "function") throw TypeError("参数错误:fn");
56
- const r = m();
57
- function c(...p) {
58
- const e = o(...p);
59
- return e instanceof Promise ? e.then((s) => r.value = s) : r.value = e, e;
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 a = $(`query${b(t)}`, c, i);
62
- return d(h({}, a), {
63
- [t]: r
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
- $ as useAsync,
68
- j as useAsyncData,
69
- $ as useAsyncFunction
109
+ j as useAsync,
110
+ k as useAsyncData,
111
+ j as useAsyncFunction
70
112
  };
@@ -1 +1 @@
1
- (function(t,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.VueAsyncx={},t.Vue))})(this,function(t,e){"use strict";var E=Object.defineProperty,b=Object.defineProperties;var O=Object.getOwnPropertyDescriptors;var l=Object.getOwnPropertySymbols;var j=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable;var h=(t,e,o)=>e in t?E(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,A=(t,e)=>{for(var o in e||(e={}))j.call(e,o)&&h(t,o,e[o]);if(l)for(var o of l(e))P.call(e,o)&&h(t,o,e[o]);return t},w=(t,e)=>b(t,O(e));function o(...n){var d;if(!Array.isArray(n)||!n.length)throw TypeError("参数错误:未传递");const{name:i,fn:r,options:s}=typeof n[0]=="function"?{name:"method",fn:n[0],options:n[1]}:{name:n[0]||"method",fn:n[1],options:n[2]};if(typeof i!="string")throw TypeError("参数错误:name");if(typeof r!="function")throw TypeError("参数错误:fn");const c=e.ref(!1),p=e.ref();function y(...f){const a=()=>{c.value=!0,p.value=f},u=()=>{c.value=!1,p.value=void 0};a();const m=r(...f);return m instanceof Promise?m.finally(()=>u()):u(),m}if(s){const f=()=>{},a=Object.assign({},"immediate"in s?{immediate:s.immediate}:{},(d=s.watchOptions)!=null?d:{}),{watch:u}=s;e.watch(u!=null?u:f,()=>y(),a)}return{[i]:y,[`${i}Loading`]:c,[`${i}Arguments`]:p}}function T(n){if(!n)return"";const i=n[0],r=n.slice(1);return i.toUpperCase()+r}function v(...n){if(!Array.isArray(n)||!n.length)throw TypeError("参数错误:未传递");const{name:i,fn:r,options:s}=typeof n[0]=="function"?{name:"data",fn:n[0],options:n[1]}:{name:n[0]||"data",fn:n[1],options:n[2]};if(typeof i!="string")throw TypeError("参数错误:name");if(typeof r!="function")throw TypeError("参数错误:fn");const c=e.ref();function p(...d){const f=r(...d);return f instanceof Promise?f.then(a=>c.value=a):c.value=f,f}const y=o(`query${T(i)}`,p,s);return w(A({},y),{[i]:c})}t.useAsync=o,t.useAsyncData=v,t.useAsyncFunction=o,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-asyncx",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "dist/vue-asyncx.d.ts",