vue3-request 1.0.20 → 1.0.22
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/vue3-request.cjs.js +1 -1
- package/dist/vue3-request.es.js +93 -93
- package/dist/vue3-request.umd.js +1 -1
- package/package.json +1 -1
package/dist/vue3-request.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue"),b=e=>e,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue"),b=e=>e,C=e=>e==null,x=typeof window>"u";function A(e){return C(e)?e:typeof e=="number"&&!isNaN(e)}function w(e,t){return A(e)===void 0?{is:!1,value:0}:A(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const O=(e,t)=>{let n=t;for(let s=e.length;s-- >0;){const r=e[s];n=r(n)}return n?.()},S=()=>new Promise(()=>{}),L=e=>e instanceof Function,R=new Map,j=(e,t,{data:n,params:s,time:r})=>{let u;const a=q(e),{is:o,value:l}=w(t,!0);o&&(a?.timer&&window.clearTimeout(a?.timer),l!==-1&&(u=window.setTimeout(()=>{y(e)},l)),R.set(e,{data:n,params:s,time:r,timer:u}))},q=e=>R.get(e),y=e=>{e&&typeof e=="string"?R.delete(e):R.clear()};class N{constructor(t,n){this.service=t,this.options=n,this.state=i.reactive({data:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{Object.assign(this.state,t)};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:O(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params,this.state.data,this.state.error),this.loading(!1),this.options.onFinally?.(this.state.params,this.state.data,this.state.error)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...u}=this.executePlugin("onBefore",t);if(!r)return S();if(this.setState({params:t}),this.loading(!0),s)return this.loading(u.loading||!1),u.data;this.options.onBefore?.(t);try{const a=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",a);o||(o=a());const l=await o;return n!==this.currentRequestId?S():(this.setState({data:l,error:void 0}),this.executePlugin("onSuccess",l,t),this.options.onSuccess?.(l,t),this.onFinished(),l)}catch(a){if(n!==this.currentRequestId)return S();const o=a;throw this.setState({data:void 0,error:o}),this.executePlugin("onError",o,t),this.options.onError?.(o,t),this.onFinished(),o}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);mutate=t=>{L(t)?t(this.state.data):this.setState({data:t}),this.executePlugin("onMutate",this.state.data)};cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function U(e,t,n){const s={manual:!1,...t},r=new N(e,s);r.pluginImpls=n.map(g=>g(r,s)),i.onMounted(()=>{if(!s.manual){const g=r.state.params;r.run(...g)}}),i.onUnmounted(r.cancel);const{run:u,cancel:a,refresh:o,runAsync:l,refreshAsync:v,abort:f,mutate:h}=r;return{...i.toRefs(r.state),run:u,cancel:a,refresh:o,runAsync:l,mutate:h,abort:f,refreshAsync:v,clearCache:y}}const D=b((e,{abortPrevious:t=!0})=>{let n=null;const s=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),p=new Map,W=(e,{data:t,params:n,time:s})=>{p.has(e)&&p.get(e)?.forEach(u=>u({data:t,params:n,time:s}))},E=(e,t)=>(p.has(e)?p.get(e)?.add(t):p.set(e,new Set),()=>{const n=p.get(e);n?.delete(t),n?.size===0&&p.delete(e)}),P=new Map,H=(e,t)=>{P.set(e,t),t.then(n=>{}).catch(()=>{}).finally(()=>{$(e)})},_=e=>P.get(e),$=e=>{e&&typeof e=="string"?P.delete(e):P.clear()},z=b((e,{cacheKey:t,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:u})=>{const a=i.ref(null);let o=null;const{is:l,value:v}=w(s,!0);if(!l)return{};const f=(c,d)=>{r?r(c,d):j(c,n,d),W(c,d)},h=c=>u?u(c):q(c);function g(){if(!t)return;const c=h(t);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),a.value=E(t,d=>{e.setState({data:d.data})})}return g(),i.onUnmounted(()=>{a.value?.()}),{onBefore:()=>{if(!t)return null;const c=h(t);return!c||!Reflect.has(c,"data")?{}:v===-1||Date.now()-c.time<v?{loading:!1,data:c.data,error:void 0,isReturn:!0}:{data:c.data,error:void 0}},onRequest:c=>{if(!t)return c;let d=_(t);return d&&d!==o?()=>d:(d=c(),o=d,H(t,d),()=>d)},onSuccess:(c,d)=>{if(!t)return;const m={data:c,params:d,time:Date.now()};a.value?.(),f(t,m),a.value=E(t,M=>{e.setState({data:M.data})})}}}),I=b((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),u=i.ref(!1),a=i.computed(()=>1e3*Math.pow(2,r.value)),o=()=>{let f=null;r.value++;const{value:h}=w(i.toValue(t),!0),g=h===-1,c=r.value<=h;if(g||c){let{is:d,value:m}=w(i.toValue(n));d||(m=i.toValue(Math.min(a.value,3e4))),f=window.setTimeout(()=>{u.value=!0,e.refresh()},m)}return()=>{f&&window.clearTimeout(f)}},l=()=>{s.value?.()},{is:v}=w(i.toValue(t),!0);return v?{onBefore:()=>{u.value||(r.value=0),u.value=!1,l()},onSuccess:()=>{r.value=0},onError:()=>{s.value=o()},onCancel:()=>{r.value=0,l()}}:{}}),T=()=>x||C(window.document?.visibilityState)?!0:document.visibilityState==="visible",F=new Set,J=e=>(F.add(e),()=>{F.delete(e)}),Q=()=>{T()&&F.forEach(e=>{e()})};window.addEventListener("visibilitychange",Q,!1);const X=b((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),u=i.ref(),a=()=>{let o;const{value:l}=w(i.toValue(s),!0);if(e.state.error&&l!==0)return;const{is:v,value:f}=w(i.toValue(t));if(!v)return;const h=f;return o=window.setTimeout(()=>{!i.toValue(n)&&!T()?r.value=J(e.refresh):e.refresh()},h),()=>{o&&window.clearTimeout(o),r.value?.()}};return i.watch([()=>i.toValue(t),()=>i.toValue(n)],()=>{u.value?.(),u.value=a()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{u.value?.()},onCancel:()=>{u.value?.()},onFinally:()=>{u.value=a()}}}),Y=b((e,{manual:t,refreshDeps:n,refreshDepsAction:s})=>{if(C(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{t||(s?s():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),Z=()=>(!x&&window.navigator?.onLine)??!0,V=new Set,G=e=>(V.add(e),()=>{V.delete(e)}),B=()=>{!T()||!Z()||V.forEach(e=>{e()})};window.addEventListener("visibilitychange",B,!1);window.addEventListener("focus",B,!1);const k=b((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=w(i.toValue(t));if(!r||C(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const u=(a,o)=>{let l=!1;return()=>{l||(l=!0,a(),window.setTimeout(()=>{l=!1},o))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=G(u(e.refresh,i.toValue(t))))}),i.onUnmounted(()=>{s.value?.()}),{}}),K=b((e,{manual:t,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),u=>{!t&&u&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function ee(e,t,n){var s=null,r=null,u=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),a=!1,o=null,l=u.leading,v=u.trailing,f=function(){s=null,r=null},h=function(){a=!0,e.apply(r,s),f()},g=function(){if(l===!0&&(o=null),a){f();return}if(v===!0){h();return}f()},c=function(){var m=o!==null;return m&&clearTimeout(o),f(),o=null,a=!1,m},d=function(){a=!1,s=arguments,r=this,o===null?l===!0&&h():clearTimeout(o),o=setTimeout(g,t)};return d.cancel=c,d}const te=b((e,{debounceWait:t,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let u=null;const a=e.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:o,value:l}=w(i.toValue(t));if(!o)return;const v=i.toValue(n);u=ee(f=>f(),l,v),e.runAsync=(...f)=>new Promise((h,g)=>{r.value?(r.value=!1,a(...f).then(h).catch(g)):u(()=>{a(...f).then(h).catch(g)})}),i.onWatcherCleanup(()=>{u?.cancel(),e.runAsync=a})}),{onCancel:()=>{u?.cancel()}}});function ne(e,t,n){var s=null,r=null,u=!1,a=null,o=Object.assign({leading:!0,trailing:!0},n),l=o.leading,v=o.trailing,f=function(){s=null,r=null},h=function(){u=!0,e.apply(r,s),a=setTimeout(g,t),f()},g=function(){if(a=null,u){f();return}if(v===!0){h();return}f()},c=function(){var m=a!==null;return m&&clearTimeout(a),f(),a=null,u=!1,m},d=function(){if(s=arguments,r=this,u=!1,a===null&&l===!0){h();return}v===!0&&(a=setTimeout(g,t))};return d.cancel=c,d}const re=b((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:u,value:a}=w(i.toValue(t));if(!u)return;const o=i.toValue(n);s=ne(l=>l(),a,o),e.runAsync=(...l)=>new Promise((v,f)=>{s(()=>{r(...l).then(v).catch(f)})}),i.onWatcherCleanup(()=>{s?.cancel(),e.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),se=[I,X,Y,k,D,K,te,re,z];function ie(e,t,n){return U(e,t||{},[...n||[],...se])}exports.clearCache=y;exports.definePlugin=b;exports.useRequest=ie;
|
package/dist/vue3-request.es.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { reactive as
|
|
2
|
-
const
|
|
1
|
+
import { reactive as H, onMounted as W, onUnmounted as F, toRefs as _, ref as p, computed as $, toValue as d, watch as E, watchEffect as x, onWatcherCleanup as O } from "vue";
|
|
2
|
+
const b = (e) => e, S = (e) => e == null, L = typeof window > "u";
|
|
3
3
|
function q(e) {
|
|
4
|
-
return
|
|
4
|
+
return S(e) ? e : typeof e == "number" && !isNaN(e);
|
|
5
5
|
}
|
|
6
|
-
function
|
|
6
|
+
function w(e, t) {
|
|
7
7
|
return q(e) === void 0 ? {
|
|
8
8
|
is: !1,
|
|
9
9
|
value: 0
|
|
@@ -18,26 +18,26 @@ function b(e, t) {
|
|
|
18
18
|
value: 0
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
const
|
|
21
|
+
const z = (e, t) => {
|
|
22
22
|
let n = t;
|
|
23
23
|
for (let s = e.length; s-- > 0; ) {
|
|
24
24
|
const r = e[s];
|
|
25
25
|
n = r(n);
|
|
26
26
|
}
|
|
27
27
|
return n?.();
|
|
28
|
-
},
|
|
29
|
-
}),
|
|
28
|
+
}, T = () => new Promise(() => {
|
|
29
|
+
}), I = (e) => e instanceof Function, P = /* @__PURE__ */ new Map(), U = (e, t, { data: n, params: s, time: r }) => {
|
|
30
30
|
let i;
|
|
31
|
-
const u =
|
|
31
|
+
const u = j(e), { is: a, value: o } = w(t, !0);
|
|
32
32
|
a && (u?.timer && window.clearTimeout(u?.timer), o !== -1 && (i = window.setTimeout(() => {
|
|
33
|
-
|
|
33
|
+
N(e);
|
|
34
34
|
}, o)), P.set(e, { data: n, params: s, time: r, timer: i }));
|
|
35
|
-
},
|
|
35
|
+
}, j = (e) => P.get(e), N = (e) => {
|
|
36
36
|
e && typeof e == "string" ? P.delete(e) : P.clear();
|
|
37
37
|
};
|
|
38
|
-
class
|
|
38
|
+
class J {
|
|
39
39
|
constructor(t, n) {
|
|
40
|
-
this.service = t, this.options = n, this.state =
|
|
40
|
+
this.service = t, this.options = n, this.state = H({
|
|
41
41
|
data: void 0,
|
|
42
42
|
error: void 0,
|
|
43
43
|
params: n?.defaultParams || [],
|
|
@@ -58,7 +58,7 @@ class U {
|
|
|
58
58
|
executePlugin = (t, ...n) => {
|
|
59
59
|
if (t === "onRequest")
|
|
60
60
|
return {
|
|
61
|
-
servicePromise:
|
|
61
|
+
servicePromise: z(
|
|
62
62
|
this.pluginImpls.map((r) => r.onRequest).filter(Boolean),
|
|
63
63
|
n[0]
|
|
64
64
|
)
|
|
@@ -91,7 +91,7 @@ class U {
|
|
|
91
91
|
t
|
|
92
92
|
);
|
|
93
93
|
if (!r)
|
|
94
|
-
return
|
|
94
|
+
return T();
|
|
95
95
|
if (this.setState({ params: t }), this.loading(!0), s)
|
|
96
96
|
return this.loading(i.loading || !1), i.data;
|
|
97
97
|
this.options.onBefore?.(t);
|
|
@@ -100,10 +100,10 @@ class U {
|
|
|
100
100
|
let { servicePromise: a } = this.executePlugin("onRequest", u);
|
|
101
101
|
a || (a = u());
|
|
102
102
|
const o = await a;
|
|
103
|
-
return n !== this.currentRequestId ?
|
|
103
|
+
return n !== this.currentRequestId ? T() : (this.setState({ data: o, error: void 0 }), this.executePlugin("onSuccess", o, t), this.options.onSuccess?.(o, t), this.onFinished(), o);
|
|
104
104
|
} catch (u) {
|
|
105
105
|
if (n !== this.currentRequestId)
|
|
106
|
-
return
|
|
106
|
+
return T();
|
|
107
107
|
const a = u;
|
|
108
108
|
throw this.setState({ data: void 0, error: a }), this.executePlugin("onError", a, t), this.options.onError?.(a, t), this.onFinished(), a;
|
|
109
109
|
}
|
|
@@ -118,20 +118,20 @@ class U {
|
|
|
118
118
|
};
|
|
119
119
|
refreshAsync = async () => await this.runAsync(...this.state.params);
|
|
120
120
|
mutate = (t) => {
|
|
121
|
-
|
|
121
|
+
I(t) ? t(this.state.data) : this.setState({ data: t }), this.executePlugin("onMutate", this.state.data);
|
|
122
122
|
};
|
|
123
123
|
cancel = () => {
|
|
124
124
|
this.executePlugin("onCancel"), this.currentRequestId++, this.loading(!1);
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
|
-
function
|
|
127
|
+
function Q(e, t, n) {
|
|
128
128
|
const s = {
|
|
129
129
|
manual: !1,
|
|
130
130
|
...t
|
|
131
|
-
}, r = new
|
|
131
|
+
}, r = new J(e, s);
|
|
132
132
|
r.pluginImpls = n.map(
|
|
133
133
|
(g) => g(r, s)
|
|
134
|
-
),
|
|
134
|
+
), W(() => {
|
|
135
135
|
if (!s.manual) {
|
|
136
136
|
const g = r.state.params;
|
|
137
137
|
r.run(...g);
|
|
@@ -139,7 +139,7 @@ function J(e, t, n) {
|
|
|
139
139
|
}), F(r.cancel);
|
|
140
140
|
const { run: i, cancel: u, refresh: a, runAsync: o, refreshAsync: h, abort: c, mutate: v } = r;
|
|
141
141
|
return {
|
|
142
|
-
...
|
|
142
|
+
..._(r.state),
|
|
143
143
|
run: i,
|
|
144
144
|
cancel: u,
|
|
145
145
|
refresh: a,
|
|
@@ -147,10 +147,10 @@ function J(e, t, n) {
|
|
|
147
147
|
mutate: v,
|
|
148
148
|
abort: c,
|
|
149
149
|
refreshAsync: h,
|
|
150
|
-
clearCache:
|
|
150
|
+
clearCache: N
|
|
151
151
|
};
|
|
152
152
|
}
|
|
153
|
-
const
|
|
153
|
+
const X = b((e, { abortPrevious: t = !0 }) => {
|
|
154
154
|
let n = null;
|
|
155
155
|
const s = () => {
|
|
156
156
|
n = new AbortController(), e.setState({
|
|
@@ -171,20 +171,20 @@ const Q = p((e, { abortPrevious: t = !0 }) => {
|
|
|
171
171
|
e.abort(), n = null;
|
|
172
172
|
}
|
|
173
173
|
};
|
|
174
|
-
}), R = /* @__PURE__ */ new Map(),
|
|
174
|
+
}), R = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: s }) => {
|
|
175
175
|
R.has(e) && R.get(e)?.forEach((i) => i({ data: t, params: n, time: s }));
|
|
176
176
|
}, M = (e, t) => (R.has(e) ? R.get(e)?.add(t) : R.set(e, /* @__PURE__ */ new Set()), () => {
|
|
177
177
|
const n = R.get(e);
|
|
178
178
|
n?.delete(t), n?.size === 0 && R.delete(e);
|
|
179
|
-
}), C = /* @__PURE__ */ new Map(),
|
|
179
|
+
}), C = /* @__PURE__ */ new Map(), Z = (e, t) => {
|
|
180
180
|
C.set(e, t), t.then((n) => {
|
|
181
181
|
}).catch(() => {
|
|
182
182
|
}).finally(() => {
|
|
183
|
-
|
|
183
|
+
k(e);
|
|
184
184
|
});
|
|
185
|
-
},
|
|
185
|
+
}, G = (e) => C.get(e), k = (e) => {
|
|
186
186
|
e && typeof e == "string" ? C.delete(e) : C.clear();
|
|
187
|
-
},
|
|
187
|
+
}, K = b(
|
|
188
188
|
(e, {
|
|
189
189
|
cacheKey: t,
|
|
190
190
|
cacheTime: n = 3e5,
|
|
@@ -192,14 +192,14 @@ const Q = p((e, { abortPrevious: t = !0 }) => {
|
|
|
192
192
|
setCache: r,
|
|
193
193
|
getCache: i
|
|
194
194
|
}) => {
|
|
195
|
-
const u =
|
|
195
|
+
const u = p(null);
|
|
196
196
|
let a = null;
|
|
197
|
-
const { is: o, value: h } =
|
|
197
|
+
const { is: o, value: h } = w(s, !0);
|
|
198
198
|
if (!o)
|
|
199
199
|
return {};
|
|
200
200
|
const c = (l, f) => {
|
|
201
|
-
r ? r(l, f) :
|
|
202
|
-
}, v = (l) => i ? i(l) :
|
|
201
|
+
r ? r(l, f) : U(l, n, f), Y(l, f);
|
|
202
|
+
}, v = (l) => i ? i(l) : j(l);
|
|
203
203
|
function g() {
|
|
204
204
|
if (!t) return;
|
|
205
205
|
const l = v(t);
|
|
@@ -230,8 +230,8 @@ const Q = p((e, { abortPrevious: t = !0 }) => {
|
|
|
230
230
|
},
|
|
231
231
|
onRequest: (l) => {
|
|
232
232
|
if (!t) return l;
|
|
233
|
-
let f =
|
|
234
|
-
return f && f !== a ? () => f : (f = l(), a = f,
|
|
233
|
+
let f = G(t);
|
|
234
|
+
return f && f !== a ? () => f : (f = l(), a = f, Z(t, f), () => f);
|
|
235
235
|
},
|
|
236
236
|
onSuccess: (l, f) => {
|
|
237
237
|
if (!t) return;
|
|
@@ -240,23 +240,23 @@ const Q = p((e, { abortPrevious: t = !0 }) => {
|
|
|
240
240
|
params: f,
|
|
241
241
|
time: Date.now()
|
|
242
242
|
};
|
|
243
|
-
u.value?.(), c(t, m), u.value = M(t, (
|
|
243
|
+
u.value?.(), c(t, m), u.value = M(t, (D) => {
|
|
244
244
|
e.setState({
|
|
245
|
-
data:
|
|
245
|
+
data: D.data
|
|
246
246
|
});
|
|
247
247
|
});
|
|
248
248
|
}
|
|
249
249
|
};
|
|
250
250
|
}
|
|
251
|
-
),
|
|
252
|
-
const s =
|
|
251
|
+
), ee = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
|
|
252
|
+
const s = p(), r = p(0), i = p(!1), u = $(
|
|
253
253
|
() => 1e3 * Math.pow(2, r.value)
|
|
254
254
|
), a = () => {
|
|
255
255
|
let c = null;
|
|
256
256
|
r.value++;
|
|
257
|
-
const { value: v } =
|
|
257
|
+
const { value: v } = w(d(t), !0), g = v === -1, l = r.value <= v;
|
|
258
258
|
if (g || l) {
|
|
259
|
-
let { is: f, value: m } =
|
|
259
|
+
let { is: f, value: m } = w(d(n));
|
|
260
260
|
f || (m = d(Math.min(u.value, 3e4))), c = window.setTimeout(() => {
|
|
261
261
|
i.value = !0, e.refresh();
|
|
262
262
|
}, m);
|
|
@@ -266,7 +266,7 @@ const Q = p((e, { abortPrevious: t = !0 }) => {
|
|
|
266
266
|
};
|
|
267
267
|
}, o = () => {
|
|
268
268
|
s.value?.();
|
|
269
|
-
}, { is: h } =
|
|
269
|
+
}, { is: h } = w(d(t), !0);
|
|
270
270
|
return h ? {
|
|
271
271
|
onBefore: () => {
|
|
272
272
|
i.value || (r.value = 0), i.value = !1, o();
|
|
@@ -281,34 +281,34 @@ const Q = p((e, { abortPrevious: t = !0 }) => {
|
|
|
281
281
|
r.value = 0, o();
|
|
282
282
|
}
|
|
283
283
|
} : {};
|
|
284
|
-
}), B = () => document.visibilityState === "visible",
|
|
285
|
-
|
|
286
|
-
}),
|
|
287
|
-
B() &&
|
|
284
|
+
}), B = () => L || S(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", y = /* @__PURE__ */ new Set(), te = (e) => (y.add(e), () => {
|
|
285
|
+
y.delete(e);
|
|
286
|
+
}), ne = () => {
|
|
287
|
+
B() && y.forEach((e) => {
|
|
288
288
|
e();
|
|
289
289
|
});
|
|
290
290
|
};
|
|
291
|
-
window.addEventListener("visibilitychange",
|
|
292
|
-
const
|
|
293
|
-
const r =
|
|
291
|
+
window.addEventListener("visibilitychange", ne, !1);
|
|
292
|
+
const re = b((e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: s }) => {
|
|
293
|
+
const r = p(null), i = p(), u = () => {
|
|
294
294
|
let a;
|
|
295
|
-
const { value: o } =
|
|
295
|
+
const { value: o } = w(
|
|
296
296
|
d(s),
|
|
297
297
|
!0
|
|
298
298
|
);
|
|
299
299
|
if (e.state.error && o !== 0) return;
|
|
300
|
-
const { is: h, value: c } =
|
|
300
|
+
const { is: h, value: c } = w(
|
|
301
301
|
d(t)
|
|
302
302
|
);
|
|
303
303
|
if (!h) return;
|
|
304
304
|
const v = c;
|
|
305
305
|
return a = window.setTimeout(() => {
|
|
306
|
-
!d(n) && !B() ? r.value =
|
|
306
|
+
!d(n) && !B() ? r.value = te(e.refresh) : e.refresh();
|
|
307
307
|
}, v), () => {
|
|
308
308
|
a && window.clearTimeout(a), r.value?.();
|
|
309
309
|
};
|
|
310
310
|
};
|
|
311
|
-
return
|
|
311
|
+
return E(
|
|
312
312
|
[() => d(t), () => d(n)],
|
|
313
313
|
() => {
|
|
314
314
|
i.value?.(), i.value = u();
|
|
@@ -326,11 +326,11 @@ const ne = p((e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCoun
|
|
|
326
326
|
i.value = u();
|
|
327
327
|
}
|
|
328
328
|
};
|
|
329
|
-
}),
|
|
329
|
+
}), se = b(
|
|
330
330
|
(e, { manual: t, refreshDeps: n, refreshDepsAction: s }) => {
|
|
331
|
-
if (
|
|
331
|
+
if (S(n) || typeof n != "object" && typeof n != "function")
|
|
332
332
|
return {};
|
|
333
|
-
const r =
|
|
333
|
+
const r = E(
|
|
334
334
|
n,
|
|
335
335
|
() => {
|
|
336
336
|
t || (s ? s() : e.refresh());
|
|
@@ -345,19 +345,19 @@ const ne = p((e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCoun
|
|
|
345
345
|
}
|
|
346
346
|
};
|
|
347
347
|
}
|
|
348
|
-
),
|
|
349
|
-
|
|
350
|
-
}),
|
|
351
|
-
!B() || !
|
|
348
|
+
), ie = () => (!L && window.navigator?.onLine) ?? !0, A = /* @__PURE__ */ new Set(), ue = (e) => (A.add(e), () => {
|
|
349
|
+
A.delete(e);
|
|
350
|
+
}), V = () => {
|
|
351
|
+
!B() || !ie() || A.forEach((e) => {
|
|
352
352
|
e();
|
|
353
353
|
});
|
|
354
354
|
};
|
|
355
|
-
window.addEventListener("visibilitychange",
|
|
356
|
-
window.addEventListener("focus",
|
|
357
|
-
const
|
|
355
|
+
window.addEventListener("visibilitychange", V, !1);
|
|
356
|
+
window.addEventListener("focus", V, !1);
|
|
357
|
+
const ae = b(
|
|
358
358
|
(e, { refocusTimespan: t = 5e3, refreshOnWindowFocus: n = !1 }) => {
|
|
359
|
-
const s =
|
|
360
|
-
if (!r ||
|
|
359
|
+
const s = p(), { is: r } = w(d(t));
|
|
360
|
+
if (!r || S(d(n)) || typeof d(n) != "boolean")
|
|
361
361
|
return {};
|
|
362
362
|
const i = (u, a) => {
|
|
363
363
|
let o = !1;
|
|
@@ -367,17 +367,17 @@ const ue = p(
|
|
|
367
367
|
}, a));
|
|
368
368
|
};
|
|
369
369
|
};
|
|
370
|
-
return
|
|
371
|
-
s.value?.(), d(n) && (s.value =
|
|
370
|
+
return x(() => {
|
|
371
|
+
s.value?.(), d(n) && (s.value = ue(
|
|
372
372
|
i(e.refresh, d(t))
|
|
373
373
|
));
|
|
374
374
|
}), F(() => {
|
|
375
375
|
s.value?.();
|
|
376
376
|
}), {};
|
|
377
377
|
}
|
|
378
|
-
),
|
|
378
|
+
), oe = b(
|
|
379
379
|
(e, { manual: t, ready: n = !0, defaultParams: s = [] }) => {
|
|
380
|
-
const r =
|
|
380
|
+
const r = E(
|
|
381
381
|
() => d(n),
|
|
382
382
|
(i) => {
|
|
383
383
|
!t && i && e.run(...s);
|
|
@@ -394,7 +394,7 @@ const ue = p(
|
|
|
394
394
|
};
|
|
395
395
|
}
|
|
396
396
|
);
|
|
397
|
-
function
|
|
397
|
+
function le(e, t, n) {
|
|
398
398
|
var s = null, r = null, i = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), u = !1, a = null, o = i.leading, h = i.trailing, c = function() {
|
|
399
399
|
s = null, r = null;
|
|
400
400
|
}, v = function() {
|
|
@@ -417,16 +417,16 @@ function oe(e, t, n) {
|
|
|
417
417
|
};
|
|
418
418
|
return f.cancel = l, f;
|
|
419
419
|
}
|
|
420
|
-
const
|
|
420
|
+
const ce = b(
|
|
421
421
|
(e, { debounceWait: t, debounceOptions: n, manual: s }) => {
|
|
422
|
-
const r =
|
|
422
|
+
const r = p(!1);
|
|
423
423
|
let i = null;
|
|
424
424
|
const u = e.runAsync;
|
|
425
|
-
return s || (r.value = !0),
|
|
426
|
-
const { is: a, value: o } =
|
|
425
|
+
return s || (r.value = !0), x(() => {
|
|
426
|
+
const { is: a, value: o } = w(d(t));
|
|
427
427
|
if (!a) return;
|
|
428
428
|
const h = d(n);
|
|
429
|
-
i =
|
|
429
|
+
i = le(
|
|
430
430
|
(c) => c(),
|
|
431
431
|
o,
|
|
432
432
|
h
|
|
@@ -444,7 +444,7 @@ const le = p(
|
|
|
444
444
|
};
|
|
445
445
|
}
|
|
446
446
|
);
|
|
447
|
-
function
|
|
447
|
+
function fe(e, t, n) {
|
|
448
448
|
var s = null, r = null, i = !1, u = null, a = Object.assign({ leading: !0, trailing: !0 }, n), o = a.leading, h = a.trailing, c = function() {
|
|
449
449
|
s = null, r = null;
|
|
450
450
|
}, v = function() {
|
|
@@ -471,15 +471,15 @@ function ce(e, t, n) {
|
|
|
471
471
|
};
|
|
472
472
|
return f.cancel = l, f;
|
|
473
473
|
}
|
|
474
|
-
const
|
|
474
|
+
const de = b(
|
|
475
475
|
(e, { throttleWait: t, throttleOptions: n }) => {
|
|
476
476
|
let s = null;
|
|
477
477
|
const r = e.runAsync;
|
|
478
|
-
return
|
|
479
|
-
const { is: i, value: u } =
|
|
478
|
+
return x(() => {
|
|
479
|
+
const { is: i, value: u } = w(d(t));
|
|
480
480
|
if (!i) return;
|
|
481
481
|
const a = d(n);
|
|
482
|
-
s =
|
|
482
|
+
s = fe(
|
|
483
483
|
(o) => o(),
|
|
484
484
|
u,
|
|
485
485
|
a
|
|
@@ -496,25 +496,25 @@ const fe = p(
|
|
|
496
496
|
}
|
|
497
497
|
};
|
|
498
498
|
}
|
|
499
|
-
),
|
|
500
|
-
|
|
501
|
-
ne,
|
|
499
|
+
), ve = [
|
|
500
|
+
ee,
|
|
502
501
|
re,
|
|
503
|
-
|
|
504
|
-
Q,
|
|
502
|
+
se,
|
|
505
503
|
ae,
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
504
|
+
X,
|
|
505
|
+
oe,
|
|
506
|
+
ce,
|
|
507
|
+
de,
|
|
508
|
+
K
|
|
509
509
|
];
|
|
510
|
-
function
|
|
511
|
-
return
|
|
510
|
+
function ge(e, t, n) {
|
|
511
|
+
return Q(e, t || {}, [
|
|
512
512
|
...n || [],
|
|
513
|
-
...
|
|
513
|
+
...ve
|
|
514
514
|
]);
|
|
515
515
|
}
|
|
516
516
|
export {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
517
|
+
N as clearCache,
|
|
518
|
+
b as definePlugin,
|
|
519
|
+
ge as useRequest
|
|
520
520
|
};
|
package/dist/vue3-request.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(b,
|
|
1
|
+
(function(b,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(b=typeof globalThis<"u"?globalThis:b||self,i(b.Vue3Request={},b.Vue))})(this,function(b,i){"use strict";const v=t=>t,P=t=>t==null,E=typeof window>"u";function q(t){return P(t)?t:typeof t=="number"&&!isNaN(t)}function w(t,e){return q(t)===void 0?{is:!1,value:0}:q(t)?e&&t===-1?{is:!0,value:-1}:{is:!0,value:t===0||t&&t<0?0:t}:{is:!1,value:0}}const O=(t,e)=>{let n=e;for(let s=t.length;s-- >0;){const r=t[s];n=r(n)}return n?.()},V=()=>new Promise(()=>{}),j=t=>t instanceof Function,C=new Map,L=(t,e,{data:n,params:s,time:r})=>{let o;const a=x(t),{is:l,value:u}=w(e,!0);l&&(a?.timer&&window.clearTimeout(a?.timer),u!==-1&&(o=window.setTimeout(()=>{y(t)},u)),C.set(t,{data:n,params:s,time:r,timer:o}))},x=t=>C.get(t),y=t=>{t&&typeof t=="string"?C.delete(t):C.clear()};class N{constructor(e,n){this.service=e,this.options=n,this.state=i.reactive({data:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=e=>{Object.assign(this.state,e)};executePlugin=(e,...n)=>{if(e==="onRequest")return{servicePromise:O(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[e]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=e=>{this.setState({loading:e,isFinished:!e})};onFinished=()=>{this.executePlugin("onFinally",this.state.params,this.state.data,this.state.error),this.loading(!1),this.options.onFinally?.(this.state.params,this.state.data,this.state.error)};runAsync=async(...e)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...o}=this.executePlugin("onBefore",e);if(!r)return V();if(this.setState({params:e}),this.loading(!0),s)return this.loading(o.loading||!1),o.data;this.options.onBefore?.(e);try{const a=()=>this.service(...e);let{servicePromise:l}=this.executePlugin("onRequest",a);l||(l=a());const u=await l;return n!==this.currentRequestId?V():(this.setState({data:u,error:void 0}),this.executePlugin("onSuccess",u,e),this.options.onSuccess?.(u,e),this.onFinished(),u)}catch(a){if(n!==this.currentRequestId)return V();const l=a;throw this.setState({data:void 0,error:l}),this.executePlugin("onError",l,e),this.options.onError?.(l,e),this.onFinished(),l}};run=(...e)=>{this.runAsync(...e).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);mutate=e=>{j(e)?e(this.state.data):this.setState({data:e}),this.executePlugin("onMutate",this.state.data)};cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function U(t,e,n){const s={manual:!1,...e},r=new N(t,s);r.pluginImpls=n.map(m=>m(r,s)),i.onMounted(()=>{if(!s.manual){const m=r.state.params;r.run(...m)}}),i.onUnmounted(r.cancel);const{run:o,cancel:a,refresh:l,runAsync:u,refreshAsync:g,abort:f,mutate:h}=r;return{...i.toRefs(r.state),run:o,cancel:a,refresh:l,runAsync:u,mutate:h,abort:f,refreshAsync:g,clearCache:y}}const D=v((t,{abortPrevious:e=!0})=>{let n=null;const s=()=>{n=new AbortController,t.setState({signal:n.signal,isAborted:n.signal.aborted})};return t.abort=()=>{n&&!n.signal.aborted&&!t.state.isFinished&&(n.abort(),t.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof e=="boolean"&&e&&t.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{t.abort(),n=null}}}),R=new Map,W=(t,{data:e,params:n,time:s})=>{R.has(t)&&R.get(t)?.forEach(o=>o({data:e,params:n,time:s}))},B=(t,e)=>(R.has(t)?R.get(t)?.add(e):R.set(t,new Set),()=>{const n=R.get(t);n?.delete(e),n?.size===0&&R.delete(t)}),S=new Map,H=(t,e)=>{S.set(t,e),e.then(n=>{}).catch(()=>{}).finally(()=>{$(t)})},_=t=>S.get(t),$=t=>{t&&typeof t=="string"?S.delete(t):S.clear()},z=v((t,{cacheKey:e,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:o})=>{const a=i.ref(null);let l=null;const{is:u,value:g}=w(s,!0);if(!u)return{};const f=(c,d)=>{r?r(c,d):L(c,n,d),W(c,d)},h=c=>o?o(c):x(c);function m(){if(!e)return;const c=h(e);c&&Reflect.has(c,"data")&&t.setState({data:c.data,params:c.params}),a.value=B(e,d=>{t.setState({data:d.data})})}return m(),i.onUnmounted(()=>{a.value?.()}),{onBefore:()=>{if(!e)return null;const c=h(e);return!c||!Reflect.has(c,"data")?{}:g===-1||Date.now()-c.time<g?{loading:!1,data:c.data,error:void 0,isReturn:!0}:{data:c.data,error:void 0}},onRequest:c=>{if(!e)return c;let d=_(e);return d&&d!==l?()=>d:(d=c(),l=d,H(e,d),()=>d)},onSuccess:(c,d)=>{if(!e)return;const p={data:c,params:d,time:Date.now()};a.value?.(),f(e,p),a.value=B(e,ot=>{t.setState({data:ot.data})})}}}),I=v((t,{errorRetryCount:e=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),o=i.ref(!1),a=i.computed(()=>1e3*Math.pow(2,r.value)),l=()=>{let f=null;r.value++;const{value:h}=w(i.toValue(e),!0),m=h===-1,c=r.value<=h;if(m||c){let{is:d,value:p}=w(i.toValue(n));d||(p=i.toValue(Math.min(a.value,3e4))),f=window.setTimeout(()=>{o.value=!0,t.refresh()},p)}return()=>{f&&window.clearTimeout(f)}},u=()=>{s.value?.()},{is:g}=w(i.toValue(e),!0);return g?{onBefore:()=>{o.value||(r.value=0),o.value=!1,u()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,u()}}:{}}),T=()=>E||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",F=new Set,J=t=>(F.add(t),()=>{F.delete(t)}),Q=()=>{T()&&F.forEach(t=>{t()})};window.addEventListener("visibilitychange",Q,!1);const X=v((t,{pollingInterval:e,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),o=i.ref(),a=()=>{let l;const{value:u}=w(i.toValue(s),!0);if(t.state.error&&u!==0)return;const{is:g,value:f}=w(i.toValue(e));if(!g)return;const h=f;return l=window.setTimeout(()=>{!i.toValue(n)&&!T()?r.value=J(t.refresh):t.refresh()},h),()=>{l&&window.clearTimeout(l),r.value?.()}};return i.watch([()=>i.toValue(e),()=>i.toValue(n)],()=>{o.value?.(),o.value=a()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{o.value?.()},onCancel:()=>{o.value?.()},onFinally:()=>{o.value=a()}}}),Y=v((t,{manual:e,refreshDeps:n,refreshDepsAction:s})=>{if(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{e||(s?s():t.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),Z=()=>(!E&&window.navigator?.onLine)??!0,A=new Set,G=t=>(A.add(t),()=>{A.delete(t)}),M=()=>{!T()||!Z()||A.forEach(t=>{t()})};window.addEventListener("visibilitychange",M,!1),window.addEventListener("focus",M,!1);const k=v((t,{refocusTimespan:e=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=w(i.toValue(e));if(!r||P(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const o=(a,l)=>{let u=!1;return()=>{u||(u=!0,a(),window.setTimeout(()=>{u=!1},l))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=G(o(t.refresh,i.toValue(e))))}),i.onUnmounted(()=>{s.value?.()}),{}}),K=v((t,{manual:e,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),o=>{!e&&o&&t.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function tt(t,e,n){var s=null,r=null,o=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),a=!1,l=null,u=o.leading,g=o.trailing,f=function(){s=null,r=null},h=function(){a=!0,t.apply(r,s),f()},m=function(){if(u===!0&&(l=null),a){f();return}if(g===!0){h();return}f()},c=function(){var p=l!==null;return p&&clearTimeout(l),f(),l=null,a=!1,p},d=function(){a=!1,s=arguments,r=this,l===null?u===!0&&h():clearTimeout(l),l=setTimeout(m,e)};return d.cancel=c,d}const et=v((t,{debounceWait:e,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let o=null;const a=t.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:l,value:u}=w(i.toValue(e));if(!l)return;const g=i.toValue(n);o=tt(f=>f(),u,g),t.runAsync=(...f)=>new Promise((h,m)=>{r.value?(r.value=!1,a(...f).then(h).catch(m)):o(()=>{a(...f).then(h).catch(m)})}),i.onWatcherCleanup(()=>{o?.cancel(),t.runAsync=a})}),{onCancel:()=>{o?.cancel()}}});function nt(t,e,n){var s=null,r=null,o=!1,a=null,l=Object.assign({leading:!0,trailing:!0},n),u=l.leading,g=l.trailing,f=function(){s=null,r=null},h=function(){o=!0,t.apply(r,s),a=setTimeout(m,e),f()},m=function(){if(a=null,o){f();return}if(g===!0){h();return}f()},c=function(){var p=a!==null;return p&&clearTimeout(a),f(),a=null,o=!1,p},d=function(){if(s=arguments,r=this,o=!1,a===null&&u===!0){h();return}g===!0&&(a=setTimeout(m,e))};return d.cancel=c,d}const rt=v((t,{throttleWait:e,throttleOptions:n})=>{let s=null;const r=t.runAsync;return i.watchEffect(()=>{const{is:o,value:a}=w(i.toValue(e));if(!o)return;const l=i.toValue(n);s=nt(u=>u(),a,l),t.runAsync=(...u)=>new Promise((g,f)=>{s(()=>{r(...u).then(g).catch(f)})}),i.onWatcherCleanup(()=>{s?.cancel(),t.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),it=[I,X,Y,k,D,K,et,rt,z];function st(t,e,n){return U(t,e||{},[...n||[],...it])}b.clearCache=y,b.definePlugin=v,b.useRequest=st,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED