vue3-request 1.0.27 → 1.0.30
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/README.md +1 -1
- package/dist/vue3-request.cjs.js +1 -1
- package/dist/vue3-request.es.js +34 -27
- package/dist/vue3-request.umd.js +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
package/dist/vue3-request.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),p=e=>e,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),p=e=>e,P=e=>e==null,S=typeof window>"u";function x(e){return P(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return x(e)===void 0?{is:!1,value:0}:x(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const M=(e,t)=>{let n=t;for(let i=e.length;i-- >0;){const r=e[i];n=r(n)}return n?.()},T=()=>new Promise(()=>{}),y=new Map,O=(e,t,{data:n,params:i,time:r})=>{let a;const u=L(e),{is:o,value:c}=b(t,!0);o&&(u?.timer&&clearTimeout(u.timer),c!==-1&&(a=setTimeout(()=>{E(e)},c)),y.set(e,{data:n,params:i,time:r,timer:a}))},L=e=>y.get(e),E=e=>{if(e&&typeof e=="string"){const t=y.get(e);t?.timer&&clearTimeout(t.timer),y.delete(e);return}y.forEach(t=>{t?.timer&&clearTimeout(t.timer)}),y.clear()};class j{constructor(t,n){this.service=t,this.options=n,this.state=s.reactive({data:void 0,res: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=>{if(Object.assign(this.state,t),t.data&&typeof t.data=="object"&&!P(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)?this.state.res=t.data[n]:this.state.res=void 0}else this.state.res=void 0};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:M(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const i=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...i)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params),this.loading(!1),this.options.onFinally?.(this.state.params)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:i,isReady:r,...a}=this.executePlugin("onBefore",t);if(!r)return T();if(this.setState({params:t,...a}),this.loading(!0),this.options.onBefore?.(t),i)return this.loading(!1),a.data;try{const u=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",u);o||(o=u());const c=await o;return n!==this.currentRequestId?T():(this.setState({data:c,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(u){if(n!==this.currentRequestId)return T();throw this.setState({data:void 0,res:void 0,error:u}),this.executePlugin("onError",t),this.options.onError?.(t),this.onFinished(),this.state.error}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function N(e,t,n){const i={manual:!1,...t},r=new j(e,i);r.pluginImpls=n.map(h=>h(r,i)),s.onMounted(()=>{if(!i.manual){const h=r.state.params;r.run(...h)}}),s.onUnmounted(r.cancel);const{run:a,cancel:u,refresh:o,runAsync:c,refreshAsync:v,abort:l}=r;return{...s.toRefs(r.state),run:a,cancel:u,refresh:o,runAsync:c,abort:l,refreshAsync:v,clearCache:E}}const U=p((e,{abortPrevious:t=!0})=>{let n=null;const i=()=>{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(),i()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,W=(e,{data:t,params:n,time:i})=>{R.has(e)&&R.get(e)?.forEach(a=>a({data:t,params:n,time:i}))},B=(e,t)=>(R.has(e)||R.set(e,new Set),R.get(e)?.add(t),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),C=new Map,_=(e,t)=>{C.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{H(e)})},D=e=>C.get(e),H=e=>{e&&typeof e=="string"?C.delete(e):C.clear()},$=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:i=0,setCache:r,getCache:a})=>{const u=s.ref(null);let o=null;const{is:c,value:v}=b(i,!0);if(!c)return{};const l=typeof t=="function"?t:()=>t,h=(d,f)=>{r?r(d,f):O(d,n,f),W(d,f)},m=d=>a?a(d):L(d);function w(){const d=l();if(!d)return;const f=m(d);f&&Reflect.has(f,"data")&&e.setState({data:f.data,params:f.params}),u.value=B(d,g=>{e.setState({data:g.data})})}return w(),s.onUnmounted(()=>{u.value?.()}),{onBefore:d=>{const f=l(d);if(!f)return null;const g=m(f);return!g||!Reflect.has(g,"data")?{}:v===-1||Date.now()-g.time<v?{data:g.data,error:void 0,isReturn:!0}:{data:g.data,error:void 0}},onRequest:d=>{const f=l(e.state.params);if(!f)return d;let g=D(f);return g&&g!==o?()=>g:(g=d(),o=g,_(f,g),()=>g)},onSuccess:d=>{const f=l(d);f&&(u.value?.(),h(f,{data:e.state.data,params:d,time:Date.now()}),u.value=B(f,g=>{e.setState({data:g.data})}))}}}),z=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const i=s.ref(),r=s.ref(0),a=s.ref(!1),u=s.computed(()=>1e3*Math.pow(2,r.value)),o=()=>{let l=null;r.value++;const{value:h}=b(s.toValue(t),!0),m=h===-1,w=r.value<=h;if(m||w){let{is:d,value:f}=b(s.toValue(n));d||(f=s.toValue(Math.min(u.value,3e4))),l=setTimeout(()=>{a.value=!0,e.refresh()},f)}return()=>{l&&clearTimeout(l)}},c=()=>{i.value?.()},{is:v}=b(s.toValue(t),!0);return v?{onBefore:()=>{a.value||(r.value=0),a.value=!1,c()},onSuccess:()=>{r.value=0},onError:()=>{i.value=o()},onCancel:()=>{r.value=0,c()}}:{}}),A=()=>S||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",V=new Set,J=e=>(V.add(e),()=>{V.delete(e)}),Q=()=>{A()&&V.forEach(e=>{e()})};!S&&window?.addEventListener&&window.addEventListener("visibilitychange",Q,!1);const X=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:i})=>{const r=s.ref(null),a=s.ref(),u=()=>{let o;const{value:c}=b(s.toValue(i),!0);if(e.state.error&&c!==0)return;const{is:v,value:l}=b(s.toValue(t));return v?(o=setTimeout(()=>{!s.toValue(n)&&!A()?r.value=J(e.refresh):e.refresh()},l),()=>{o&&clearTimeout(o),r.value?.()}):void 0};return s.watch([()=>s.toValue(t),()=>s.toValue(n)],()=>{a.value?.(),a.value=u()}),s.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=u()}}}),Y=p((e,{manual:t,refreshDeps:n,refreshDepsAction:i})=>{if(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=s.watch(n,()=>{t||(i?i():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),Z=()=>(!S&&window.navigator?.onLine)??!0,F=new Set,G=e=>(F.add(e),()=>{F.delete(e)}),q=()=>{!A()||!Z()||F.forEach(e=>{e()})};!S&&window?.addEventListener&&(window.addEventListener("visibilitychange",q,!1),window.addEventListener("focus",q,!1));const I=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const i=s.ref(),{is:r}=b(s.toValue(t));if(!r||P(s.toValue(n))||typeof s.toValue(n)!="boolean")return{};const a=(u,o)=>{let c=!1;return()=>{c||(c=!0,u(),setTimeout(()=>{c=!1},o))}};return s.watchEffect(()=>{i.value?.(),s.toValue(n)&&(i.value=G(a(e.refresh,s.toValue(t))))}),s.onUnmounted(()=>{i.value?.()}),{}}),k=p((e,{manual:t,ready:n=!0,defaultParams:i=[]})=>{const r=s.watch(()=>s.toValue(n),a=>{!t&&a&&e.run(...i)},{flush:"sync"});return{onBefore:()=>({isReady:s.toValue(n)}),onCancel:()=>{r()}}});function K(e,t,n){var i=null,r=null,a=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),u=!1,o=null,c=a.leading,v=a.trailing,l=function(){i=null,r=null},h=function(){u=!0,e.apply(r,i),l()},m=function(){if(c===!0&&(o=null),u){l();return}if(v===!0){h();return}l()},w=function(){var f=o!==null;return f&&clearTimeout(o),l(),o=null,u=!1,f},d=function(){u=!1,i=arguments,r=this,o===null?c===!0&&h():clearTimeout(o),o=setTimeout(m,t)};return d.cancel=w,d}const ee=p((e,{debounceWait:t,debounceOptions:n,manual:i})=>{const r=s.ref(!1);let a=null;const u=e.runAsync;return i||(r.value=!0),s.watchEffect(()=>{const{is:o,value:c}=b(s.toValue(t));if(!o)return;const v=s.toValue(n);a=K(l=>l(),c,v),e.runAsync=(...l)=>new Promise((h,m)=>{r.value?(r.value=!1,u(...l).then(h).catch(m)):a(()=>{u(...l).then(h).catch(m)})}),s.onWatcherCleanup(()=>{a?.cancel(),e.runAsync=u})}),{onCancel:()=>{a?.cancel()}}});function te(e,t,n){var i=null,r=null,a=!1,u=null,o=Object.assign({leading:!0,trailing:!0},n),c=o.leading,v=o.trailing,l=function(){i=null,r=null},h=function(){a=!0,e.apply(r,i),u=setTimeout(m,t),l()},m=function(){if(u=null,a){l();return}if(v===!0){h();return}l()},w=function(){var f=u!==null;return f&&clearTimeout(u),l(),u=null,a=!1,f},d=function(){if(i=arguments,r=this,a=!1,u===null&&c===!0){h();return}v===!0&&(u=setTimeout(m,t))};return d.cancel=w,d}const ne=p((e,{throttleWait:t,throttleOptions:n})=>{let i=null;const r=e.runAsync;return s.watchEffect(()=>{const{is:a,value:u}=b(s.toValue(t));if(!a)return;const o=s.toValue(n);i=te(c=>c(),u,o),e.runAsync=(...c)=>new Promise((v,l)=>{i(()=>{r(...c).then(v).catch(l)})}),s.onWatcherCleanup(()=>{i?.cancel(),e.runAsync=r})}),{onCancel:()=>{i?.cancel()}}}),re=[z,X,Y,I,U,k,ee,ne,$];function ie(e,t,n){return N(e,t||{},[...n||[],...re])}exports.clearCache=E;exports.definePlugin=p;exports.useRequest=ie;
|
package/dist/vue3-request.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { reactive as D, onMounted as H, onUnmounted as
|
|
2
|
-
const b = (e) => e,
|
|
1
|
+
import { reactive as D, onMounted as H, onUnmounted as S, toRefs as W, ref as R, computed as $, toValue as d, watch as B, watchEffect as L, onWatcherCleanup as N } from "vue";
|
|
2
|
+
const b = (e) => e, C = (e) => e == null, F = typeof window > "u";
|
|
3
3
|
function O(e) {
|
|
4
|
-
return
|
|
4
|
+
return C(e) ? e : typeof e == "number" && !isNaN(e);
|
|
5
5
|
}
|
|
6
6
|
function p(e, t) {
|
|
7
7
|
return O(e) === void 0 ? {
|
|
@@ -26,14 +26,21 @@ const z = (e, t) => {
|
|
|
26
26
|
}
|
|
27
27
|
return n?.();
|
|
28
28
|
}, A = () => new Promise(() => {
|
|
29
|
-
}),
|
|
29
|
+
}), P = /* @__PURE__ */ new Map(), U = (e, t, { data: n, params: i, time: r }) => {
|
|
30
30
|
let s;
|
|
31
31
|
const a = V(e), { is: u, value: l } = p(t, !0);
|
|
32
32
|
u && (a?.timer && clearTimeout(a.timer), l !== -1 && (s = setTimeout(() => {
|
|
33
33
|
_(e);
|
|
34
|
-
}, l)),
|
|
35
|
-
}, V = (e) =>
|
|
36
|
-
e && typeof e == "string"
|
|
34
|
+
}, l)), P.set(e, { data: n, params: i, time: r, timer: s }));
|
|
35
|
+
}, V = (e) => P.get(e), _ = (e) => {
|
|
36
|
+
if (e && typeof e == "string") {
|
|
37
|
+
const t = P.get(e);
|
|
38
|
+
t?.timer && clearTimeout(t.timer), P.delete(e);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
P.forEach((t) => {
|
|
42
|
+
t?.timer && clearTimeout(t.timer);
|
|
43
|
+
}), P.clear();
|
|
37
44
|
};
|
|
38
45
|
class J {
|
|
39
46
|
constructor(t, n) {
|
|
@@ -54,10 +61,11 @@ class J {
|
|
|
54
61
|
abort = () => {
|
|
55
62
|
};
|
|
56
63
|
setState = (t) => {
|
|
57
|
-
if (Object.assign(this.state, t), t.data && typeof t.data == "object" && !
|
|
64
|
+
if (Object.assign(this.state, t), t.data && typeof t.data == "object" && !C(t.data)) {
|
|
58
65
|
const n = this.options.resKey || "data";
|
|
59
|
-
Reflect.has(t.data, n)
|
|
60
|
-
}
|
|
66
|
+
Reflect.has(t.data, n) ? this.state.res = t.data[n] : this.state.res = void 0;
|
|
67
|
+
} else
|
|
68
|
+
this.state.res = void 0;
|
|
61
69
|
};
|
|
62
70
|
executePlugin = (t, ...n) => {
|
|
63
71
|
if (t === "onRequest")
|
|
@@ -94,9 +102,8 @@ class J {
|
|
|
94
102
|
);
|
|
95
103
|
if (!r)
|
|
96
104
|
return A();
|
|
97
|
-
if (this.setState({ params: t, ...s }), this.loading(!0), i)
|
|
105
|
+
if (this.setState({ params: t, ...s }), this.loading(!0), this.options.onBefore?.(t), i)
|
|
98
106
|
return this.loading(!1), s.data;
|
|
99
|
-
this.options.onBefore?.(t);
|
|
100
107
|
try {
|
|
101
108
|
const a = () => this.service(...t);
|
|
102
109
|
let { servicePromise: u } = this.executePlugin("onRequest", a);
|
|
@@ -134,7 +141,7 @@ function Q(e, t, n) {
|
|
|
134
141
|
const h = r.state.params;
|
|
135
142
|
r.run(...h);
|
|
136
143
|
}
|
|
137
|
-
}),
|
|
144
|
+
}), S(r.cancel);
|
|
138
145
|
const { run: s, cancel: a, refresh: u, runAsync: l, refreshAsync: v, abort: o } = r;
|
|
139
146
|
return {
|
|
140
147
|
...W(r.state),
|
|
@@ -170,17 +177,17 @@ const X = b((e, { abortPrevious: t = !0 }) => {
|
|
|
170
177
|
};
|
|
171
178
|
}), y = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: i }) => {
|
|
172
179
|
y.has(e) && y.get(e)?.forEach((s) => s({ data: t, params: n, time: i }));
|
|
173
|
-
}, q = (e, t) => (y.has(e)
|
|
180
|
+
}, q = (e, t) => (y.has(e) || y.set(e, /* @__PURE__ */ new Set()), y.get(e)?.add(t), () => {
|
|
174
181
|
const n = y.get(e);
|
|
175
182
|
n?.delete(t), n?.size === 0 && y.delete(e);
|
|
176
|
-
}),
|
|
177
|
-
|
|
183
|
+
}), T = /* @__PURE__ */ new Map(), Z = (e, t) => {
|
|
184
|
+
T.set(e, t), t.then(() => {
|
|
178
185
|
}).catch(() => {
|
|
179
186
|
}).finally(() => {
|
|
180
187
|
I(e);
|
|
181
188
|
});
|
|
182
|
-
}, G = (e) =>
|
|
183
|
-
e && typeof e == "string" ?
|
|
189
|
+
}, G = (e) => T.get(e), I = (e) => {
|
|
190
|
+
e && typeof e == "string" ? T.delete(e) : T.clear();
|
|
184
191
|
}, k = b(
|
|
185
192
|
(e, {
|
|
186
193
|
cacheKey: t,
|
|
@@ -210,7 +217,7 @@ const X = b((e, { abortPrevious: t = !0 }) => {
|
|
|
210
217
|
});
|
|
211
218
|
});
|
|
212
219
|
}
|
|
213
|
-
return w(),
|
|
220
|
+
return w(), S(() => {
|
|
214
221
|
a.value?.();
|
|
215
222
|
}), {
|
|
216
223
|
onBefore: (f) => {
|
|
@@ -279,14 +286,14 @@ const X = b((e, { abortPrevious: t = !0 }) => {
|
|
|
279
286
|
r.value = 0, l();
|
|
280
287
|
}
|
|
281
288
|
} : {};
|
|
282
|
-
}), M = () =>
|
|
289
|
+
}), M = () => F || C(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", E = /* @__PURE__ */ new Set(), ee = (e) => (E.add(e), () => {
|
|
283
290
|
E.delete(e);
|
|
284
291
|
}), te = () => {
|
|
285
292
|
M() && E.forEach((e) => {
|
|
286
293
|
e();
|
|
287
294
|
});
|
|
288
295
|
};
|
|
289
|
-
!
|
|
296
|
+
!F && window?.addEventListener && window.addEventListener("visibilitychange", te, !1);
|
|
290
297
|
const ne = b(
|
|
291
298
|
(e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: i }) => {
|
|
292
299
|
const r = R(null), s = R(), a = () => {
|
|
@@ -310,7 +317,7 @@ const ne = b(
|
|
|
310
317
|
() => {
|
|
311
318
|
s.value?.(), s.value = a();
|
|
312
319
|
}
|
|
313
|
-
),
|
|
320
|
+
), S(() => {
|
|
314
321
|
r.value?.();
|
|
315
322
|
}), {
|
|
316
323
|
onBefore: () => {
|
|
@@ -326,7 +333,7 @@ const ne = b(
|
|
|
326
333
|
}
|
|
327
334
|
), re = b(
|
|
328
335
|
(e, { manual: t, refreshDeps: n, refreshDepsAction: i }) => {
|
|
329
|
-
if (
|
|
336
|
+
if (C(n) || typeof n != "object" && typeof n != "function")
|
|
330
337
|
return {};
|
|
331
338
|
const r = B(
|
|
332
339
|
n,
|
|
@@ -343,18 +350,18 @@ const ne = b(
|
|
|
343
350
|
}
|
|
344
351
|
};
|
|
345
352
|
}
|
|
346
|
-
), ie = () => (!
|
|
353
|
+
), ie = () => (!F && window.navigator?.onLine) ?? !0, x = /* @__PURE__ */ new Set(), se = (e) => (x.add(e), () => {
|
|
347
354
|
x.delete(e);
|
|
348
355
|
}), j = () => {
|
|
349
356
|
!M() || !ie() || x.forEach((e) => {
|
|
350
357
|
e();
|
|
351
358
|
});
|
|
352
359
|
};
|
|
353
|
-
!
|
|
360
|
+
!F && window?.addEventListener && (window.addEventListener("visibilitychange", j, !1), window.addEventListener("focus", j, !1));
|
|
354
361
|
const ae = b(
|
|
355
362
|
(e, { refocusTimespan: t = 5e3, refreshOnWindowFocus: n = !1 }) => {
|
|
356
363
|
const i = R(), { is: r } = p(d(t));
|
|
357
|
-
if (!r ||
|
|
364
|
+
if (!r || C(d(n)) || typeof d(n) != "boolean")
|
|
358
365
|
return {};
|
|
359
366
|
const s = (a, u) => {
|
|
360
367
|
let l = !1;
|
|
@@ -368,7 +375,7 @@ const ae = b(
|
|
|
368
375
|
i.value?.(), d(n) && (i.value = se(
|
|
369
376
|
s(e.refresh, d(t))
|
|
370
377
|
));
|
|
371
|
-
}),
|
|
378
|
+
}), S(() => {
|
|
372
379
|
i.value?.();
|
|
373
380
|
}), {};
|
|
374
381
|
}
|
package/dist/vue3-request.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(w,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(w=typeof globalThis<"u"?globalThis:w||self,i(w.Vue3Request={},w.Vue))})(this,function(w,i){"use strict";const p=e=>e,
|
|
1
|
+
(function(w,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(w=typeof globalThis<"u"?globalThis:w||self,i(w.Vue3Request={},w.Vue))})(this,function(w,i){"use strict";const p=e=>e,C=e=>e==null,T=typeof window>"u";function q(e){return C(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return q(e)===void 0?{is:!1,value:0}:q(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(()=>{}),P=new Map,j=(e,t,{data:n,params:s,time:r})=>{let a;const o=B(e),{is:l,value:c}=b(t,!0);l&&(o?.timer&&clearTimeout(o.timer),c!==-1&&(a=setTimeout(()=>{F(e)},c)),P.set(e,{data:n,params:s,time:r,timer:a}))},B=e=>P.get(e),F=e=>{if(e&&typeof e=="string"){const t=P.get(e);t?.timer&&clearTimeout(t.timer),P.delete(e);return}P.forEach(t=>{t?.timer&&clearTimeout(t.timer)}),P.clear()};class N{constructor(t,n){this.service=t,this.options=n,this.state=i.reactive({data:void 0,res: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=>{if(Object.assign(this.state,t),t.data&&typeof t.data=="object"&&!C(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)?this.state.res=t.data[n]:this.state.res=void 0}else this.state.res=void 0};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.loading(!1),this.options.onFinally?.(this.state.params)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...a}=this.executePlugin("onBefore",t);if(!r)return S();if(this.setState({params:t,...a}),this.loading(!0),this.options.onBefore?.(t),s)return this.loading(!1),a.data;try{const o=()=>this.service(...t);let{servicePromise:l}=this.executePlugin("onRequest",o);l||(l=o());const c=await l;return n!==this.currentRequestId?S():(this.setState({data:c,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(o){if(n!==this.currentRequestId)return S();throw this.setState({data:void 0,res:void 0,error:o}),this.executePlugin("onError",t),this.options.onError?.(t),this.onFinished(),this.state.error}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);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(h=>h(r,s)),i.onMounted(()=>{if(!s.manual){const h=r.state.params;r.run(...h)}}),i.onUnmounted(r.cancel);const{run:a,cancel:o,refresh:l,runAsync:c,refreshAsync:g,abort:u}=r;return{...i.toRefs(r.state),run:a,cancel:o,refresh:l,runAsync:c,abort:u,refreshAsync:g,clearCache:F}}const W=p((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}}}),R=new Map,_=(e,{data:t,params:n,time:s})=>{R.has(e)&&R.get(e)?.forEach(a=>a({data:t,params:n,time:s}))},L=(e,t)=>(R.has(e)||R.set(e,new Set),R.get(e)?.add(t),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),V=new Map,D=(e,t)=>{V.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{$(e)})},H=e=>V.get(e),$=e=>{e&&typeof e=="string"?V.delete(e):V.clear()},z=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:a})=>{const o=i.ref(null);let l=null;const{is:c,value:g}=b(s,!0);if(!c)return{};const u=typeof t=="function"?t:()=>t,h=(d,f)=>{r?r(d,f):j(d,n,f),_(d,f)},v=d=>a?a(d):B(d);function y(){const d=u();if(!d)return;const f=v(d);f&&Reflect.has(f,"data")&&e.setState({data:f.data,params:f.params}),o.value=L(d,m=>{e.setState({data:m.data})})}return y(),i.onUnmounted(()=>{o.value?.()}),{onBefore:d=>{const f=u(d);if(!f)return null;const m=v(f);return!m||!Reflect.has(m,"data")?{}:g===-1||Date.now()-m.time<g?{data:m.data,error:void 0,isReturn:!0}:{data:m.data,error:void 0}},onRequest:d=>{const f=u(e.state.params);if(!f)return d;let m=H(f);return m&&m!==l?()=>m:(m=d(),l=m,D(f,m),()=>m)},onSuccess:d=>{const f=u(d);f&&(o.value?.(),h(f,{data:e.state.data,params:d,time:Date.now()}),o.value=L(f,m=>{e.setState({data:m.data})}))}}}),J=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),a=i.ref(!1),o=i.computed(()=>1e3*Math.pow(2,r.value)),l=()=>{let u=null;r.value++;const{value:h}=b(i.toValue(t),!0),v=h===-1,y=r.value<=h;if(v||y){let{is:d,value:f}=b(i.toValue(n));d||(f=i.toValue(Math.min(o.value,3e4))),u=setTimeout(()=>{a.value=!0,e.refresh()},f)}return()=>{u&&clearTimeout(u)}},c=()=>{s.value?.()},{is:g}=b(i.toValue(t),!0);return g?{onBefore:()=>{a.value||(r.value=0),a.value=!1,c()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,c()}}:{}}),E=()=>T||C(window.document?.visibilityState)?!0:document.visibilityState==="visible",A=new Set,Q=e=>(A.add(e),()=>{A.delete(e)}),X=()=>{E()&&A.forEach(e=>{e()})};!T&&window?.addEventListener&&window.addEventListener("visibilitychange",X,!1);const Y=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),a=i.ref(),o=()=>{let l;const{value:c}=b(i.toValue(s),!0);if(e.state.error&&c!==0)return;const{is:g,value:u}=b(i.toValue(t));return g?(l=setTimeout(()=>{!i.toValue(n)&&!E()?r.value=Q(e.refresh):e.refresh()},u),()=>{l&&clearTimeout(l),r.value?.()}):void 0};return i.watch([()=>i.toValue(t),()=>i.toValue(n)],()=>{a.value?.(),a.value=o()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=o()}}}),Z=p((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()}}}),G=()=>(!T&&window.navigator?.onLine)??!0,x=new Set,I=e=>(x.add(e),()=>{x.delete(e)}),M=()=>{!E()||!G()||x.forEach(e=>{e()})};!T&&window?.addEventListener&&(window.addEventListener("visibilitychange",M,!1),window.addEventListener("focus",M,!1));const k=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=b(i.toValue(t));if(!r||C(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const a=(o,l)=>{let c=!1;return()=>{c||(c=!0,o(),setTimeout(()=>{c=!1},l))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=I(a(e.refresh,i.toValue(t))))}),i.onUnmounted(()=>{s.value?.()}),{}}),K=p((e,{manual:t,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),a=>{!t&&a&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function ee(e,t,n){var s=null,r=null,a=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),o=!1,l=null,c=a.leading,g=a.trailing,u=function(){s=null,r=null},h=function(){o=!0,e.apply(r,s),u()},v=function(){if(c===!0&&(l=null),o){u();return}if(g===!0){h();return}u()},y=function(){var f=l!==null;return f&&clearTimeout(l),u(),l=null,o=!1,f},d=function(){o=!1,s=arguments,r=this,l===null?c===!0&&h():clearTimeout(l),l=setTimeout(v,t)};return d.cancel=y,d}const te=p((e,{debounceWait:t,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let a=null;const o=e.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:l,value:c}=b(i.toValue(t));if(!l)return;const g=i.toValue(n);a=ee(u=>u(),c,g),e.runAsync=(...u)=>new Promise((h,v)=>{r.value?(r.value=!1,o(...u).then(h).catch(v)):a(()=>{o(...u).then(h).catch(v)})}),i.onWatcherCleanup(()=>{a?.cancel(),e.runAsync=o})}),{onCancel:()=>{a?.cancel()}}});function ne(e,t,n){var s=null,r=null,a=!1,o=null,l=Object.assign({leading:!0,trailing:!0},n),c=l.leading,g=l.trailing,u=function(){s=null,r=null},h=function(){a=!0,e.apply(r,s),o=setTimeout(v,t),u()},v=function(){if(o=null,a){u();return}if(g===!0){h();return}u()},y=function(){var f=o!==null;return f&&clearTimeout(o),u(),o=null,a=!1,f},d=function(){if(s=arguments,r=this,a=!1,o===null&&c===!0){h();return}g===!0&&(o=setTimeout(v,t))};return d.cancel=y,d}const re=p((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:a,value:o}=b(i.toValue(t));if(!a)return;const l=i.toValue(n);s=ne(c=>c(),o,l),e.runAsync=(...c)=>new Promise((g,u)=>{s(()=>{r(...c).then(g).catch(u)})}),i.onWatcherCleanup(()=>{s?.cancel(),e.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),ie=[J,Y,Z,k,W,K,te,re,z];function se(e,t,n){return U(e,t||{},[...n||[],...ie])}w.clearCache=F,w.definePlugin=p,w.useRequest=se,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue3-request",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Vue3 asynchronous request processing library, designed to simplify your asynchronous operations and API calls.",
|
|
6
6
|
"main": "dist/vue3-request.es.js",
|
|
@@ -50,6 +50,10 @@
|
|
|
50
50
|
"build:vue3": "pnpm --filter @async-handler/vue3 build-only",
|
|
51
51
|
"build:docs": "pnpm --filter @async-handler/docs build",
|
|
52
52
|
"serve:docs": "pnpm --filter @async-handler/docs serve",
|
|
53
|
-
"rf": "rimraf node_modules && rimraf dist && pnpm --filter @async-handler/vue3 rf && pnpm --filter @async-handler/docs rf"
|
|
53
|
+
"rf": "rimraf node_modules && rimraf dist && pnpm --filter @async-handler/vue3 rf && pnpm --filter @async-handler/docs rf",
|
|
54
|
+
"release": "node ./scripts/release.mjs patch",
|
|
55
|
+
"release:patch": "node ./scripts/release.mjs patch",
|
|
56
|
+
"release:minor": "node ./scripts/release.mjs minor",
|
|
57
|
+
"release:major": "node ./scripts/release.mjs major"
|
|
54
58
|
}
|
|
55
59
|
}
|