vue3-request 1.0.25 → 1.0.26
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),p=e=>e,y=e=>e==null,S=typeof window>"u";function x(e){return y(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?.()},V=()=>new Promise(()=>{}),P=new Map,O=(e,t,{data:n,params:i,time:r})=>{let a;const u=L(e),{is:o,value:f}=b(t,!0);o&&(u?.timer&&clearTimeout(u.timer),f!==-1&&(a=setTimeout(()=>{A(e)},f)),P.set(e,{data:n,params:i,time:r,timer:a}))},L=e=>P.get(e),A=e=>{e&&typeof e=="string"?P.delete(e):P.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"&&!y(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};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 V();if(this.setState({params:t,...a}),this.loading(!0),i)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const u=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",u);return o||(o=u()),this.setState({data:await o,error:void 0}),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),p=e=>e,y=e=>e==null,S=typeof window>"u";function x(e){return y(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?.()},V=()=>new Promise(()=>{}),P=new Map,O=(e,t,{data:n,params:i,time:r})=>{let a;const u=L(e),{is:o,value:f}=b(t,!0);o&&(u?.timer&&clearTimeout(u.timer),f!==-1&&(a=setTimeout(()=>{A(e)},f)),P.set(e,{data:n,params:i,time:r,timer:a}))},L=e=>P.get(e),A=e=>{e&&typeof e=="string"?P.delete(e):P.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"&&!y(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};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 V();if(this.setState({params:t,...a}),this.loading(!0),i)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const u=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",u);return o||(o=u()),n!==this.currentRequestId?V():(this.setState({data:await o,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(u){if(n!==this.currentRequestId)return V();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:f,refreshAsync:v,abort:l}=r;return{...s.toRefs(r.state),run:a,cancel:u,refresh:o,runAsync:f,abort:l,refreshAsync:v,clearCache:A}}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.get(e)?.add(t):R.set(e,new Set),()=>{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:f,value:v}=b(i,!0);if(!f)return{};const l=typeof t=="function"?t:()=>t,h=(d,c)=>{r?r(d,c):O(d,n,c),W(d,c)},m=d=>a?a(d):L(d);function w(){const d=l();if(!d)return;const c=m(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),u.value=B(d,g=>{e.setState({data:g.data})})}return w(),s.onUnmounted(()=>{u.value?.()}),{onBefore:d=>{const c=l(d);if(!c)return null;const g=m(c);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 c=l(e.state.params);if(!c)return d;let g=D(c);return g&&g!==o?()=>g:(g=d(),o=g,_(c,g),()=>g)},onSuccess:d=>{const c=l(d);c&&(u.value?.(),h(c,{data:e.state.data,params:d,time:Date.now()}),u.value=B(c,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:c}=b(s.toValue(n));d||(c=s.toValue(Math.min(u.value,3e4))),l=setTimeout(()=>{a.value=!0,e.refresh()},c)}return()=>{l&&clearTimeout(l)}},f=()=>{i.value?.()},{is:v}=b(s.toValue(t),!0);return v?{onBefore:()=>{a.value||(r.value=0),a.value=!1,f()},onSuccess:()=>{r.value=0},onError:()=>{i.value=o()},onCancel:()=>{r.value=0,f()}}:{}}),E=()=>S||y(window.document?.visibilityState)?!0:document.visibilityState==="visible",T=new Set,J=e=>(T.add(e),()=>{T.delete(e)}),Q=()=>{E()&&T.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:f}=b(s.toValue(i),!0);if(e.state.error&&f!==0)return;const{is:v,value:l}=b(s.toValue(t));return v?(o=setTimeout(()=>{!s.toValue(n)&&!E()?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(y(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=()=>{!E()||!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||y(s.toValue(n))||typeof s.toValue(n)!="boolean")return{};const a=(u,o)=>{let f=!1;return()=>{f||(f=!0,u(),setTimeout(()=>{f=!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,f=a.leading,v=a.trailing,l=function(){i=null,r=null},h=function(){u=!0,e.apply(r,i),l()},m=function(){if(f===!0&&(o=null),u){l();return}if(v===!0){h();return}l()},w=function(){var c=o!==null;return c&&clearTimeout(o),l(),o=null,u=!1,c},d=function(){u=!1,i=arguments,r=this,o===null?f===!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:f}=b(s.toValue(t));if(!o)return;const v=s.toValue(n);a=K(l=>l(),f,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),f=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 c=u!==null;return c&&clearTimeout(u),l(),u=null,a=!1,c},d=function(){if(i=arguments,r=this,a=!1,u===null&&f===!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(f=>f(),u,o),e.runAsync=(...f)=>new Promise((v,l)=>{i(()=>{r(...f).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=A;exports.definePlugin=p;exports.useRequest=ie;
|
package/dist/vue3-request.es.js
CHANGED
|
@@ -100,7 +100,7 @@ class J {
|
|
|
100
100
|
try {
|
|
101
101
|
const a = () => this.service(...t);
|
|
102
102
|
let { servicePromise: u } = this.executePlugin("onRequest", a);
|
|
103
|
-
return u || (u = a()), this.setState({ data: await u, error: void 0 }),
|
|
103
|
+
return u || (u = a()), n !== this.currentRequestId ? A() : (this.setState({ data: await u, error: void 0 }), this.executePlugin("onSuccess", t), this.options.onSuccess?.(t), this.onFinished(), this.state.data);
|
|
104
104
|
} catch (a) {
|
|
105
105
|
if (n !== this.currentRequestId)
|
|
106
106
|
return A();
|
|
@@ -116,14 +116,6 @@ class J {
|
|
|
116
116
|
this.run(...this.state.params);
|
|
117
117
|
};
|
|
118
118
|
refreshAsync = async () => await this.runAsync(...this.state.params);
|
|
119
|
-
// mutate = (data: D | ((data: D) => D)) => {
|
|
120
|
-
// if (isFunction(data)) {
|
|
121
|
-
// this.setState({ data: data(this.state.data!) });
|
|
122
|
-
// } else {
|
|
123
|
-
// this.setState({ data });
|
|
124
|
-
// }
|
|
125
|
-
// this.executePlugin("onMutate", this.state.data);
|
|
126
|
-
// };
|
|
127
119
|
cancel = () => {
|
|
128
120
|
this.executePlugin("onCancel"), this.currentRequestId++, this.loading(!1);
|
|
129
121
|
};
|
|
@@ -148,7 +140,6 @@ function Q(e, t, n) {
|
|
|
148
140
|
cancel: a,
|
|
149
141
|
refresh: u,
|
|
150
142
|
runAsync: c,
|
|
151
|
-
// mutate,
|
|
152
143
|
abort: o,
|
|
153
144
|
refreshAsync: v,
|
|
154
145
|
clearCache: _
|
|
@@ -251,21 +242,6 @@ const X = b((e, { abortPrevious: t = !0 }) => {
|
|
|
251
242
|
});
|
|
252
243
|
}));
|
|
253
244
|
}
|
|
254
|
-
// onMutate(data) {
|
|
255
|
-
// const cacheKey = _getCacheKey(requestInstance.state.params);
|
|
256
|
-
// if (!cacheKey) return;
|
|
257
|
-
// unSubscribe.value?.();
|
|
258
|
-
// _setCache(cacheKey, {
|
|
259
|
-
// data,
|
|
260
|
-
// params: requestInstance.state.params,
|
|
261
|
-
// time: Date.now(),
|
|
262
|
-
// });
|
|
263
|
-
// unSubscribe.value = on(cacheKey, (cacheData) => {
|
|
264
|
-
// requestInstance.setState({
|
|
265
|
-
// data: cacheData.data,
|
|
266
|
-
// });
|
|
267
|
-
// });
|
|
268
|
-
// },
|
|
269
245
|
};
|
|
270
246
|
}
|
|
271
247
|
), K = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
|
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,P=e=>e==null,C=typeof window>"u";function q(e){return P(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?.()},T=()=>new Promise(()=>{}),V=new Map,j=(e,t,{data:n,params:s,time:r})=>{let a;const o=B(e),{is:l,value:f}=b(t,!0);l&&(o?.timer&&clearTimeout(o.timer),f!==-1&&(a=setTimeout(()=>{F(e)},f)),V.set(e,{data:n,params:s,time:r,timer:a}))},B=e=>V.get(e),F=e=>{e&&typeof e=="string"?V.delete(e):V.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"&&!P(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};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 T();if(this.setState({params:t,...a}),this.loading(!0),s)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const o=()=>this.service(...t);let{servicePromise:l}=this.executePlugin("onRequest",o);return l||(l=o()),this.setState({data:await l,error:void 0}),
|
|
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,P=e=>e==null,C=typeof window>"u";function q(e){return P(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?.()},T=()=>new Promise(()=>{}),V=new Map,j=(e,t,{data:n,params:s,time:r})=>{let a;const o=B(e),{is:l,value:f}=b(t,!0);l&&(o?.timer&&clearTimeout(o.timer),f!==-1&&(a=setTimeout(()=>{F(e)},f)),V.set(e,{data:n,params:s,time:r,timer:a}))},B=e=>V.get(e),F=e=>{e&&typeof e=="string"?V.delete(e):V.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"&&!P(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};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 T();if(this.setState({params:t,...a}),this.loading(!0),s)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const o=()=>this.service(...t);let{servicePromise:l}=this.executePlugin("onRequest",o);return l||(l=o()),n!==this.currentRequestId?T():(this.setState({data:await l,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(o){if(n!==this.currentRequestId)return T();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:f,refreshAsync:g,abort:u}=r;return{...i.toRefs(r.state),run:a,cancel:o,refresh:l,runAsync:f,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.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),S=new Map,D=(e,t)=>{S.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{$(e)})},H=e=>S.get(e),$=e=>{e&&typeof e=="string"?S.delete(e):S.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:f,value:g}=b(s,!0);if(!f)return{};const u=typeof t=="function"?t:()=>t,h=(d,c)=>{r?r(d,c):j(d,n,c),_(d,c)},v=d=>a?a(d):B(d);function y(){const d=u();if(!d)return;const c=v(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),o.value=L(d,m=>{e.setState({data:m.data})})}return y(),i.onUnmounted(()=>{o.value?.()}),{onBefore:d=>{const c=u(d);if(!c)return null;const m=v(c);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 c=u(e.state.params);if(!c)return d;let m=H(c);return m&&m!==l?()=>m:(m=d(),l=m,D(c,m),()=>m)},onSuccess:d=>{const c=u(d);c&&(o.value?.(),h(c,{data:e.state.data,params:d,time:Date.now()}),o.value=L(c,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:c}=b(i.toValue(n));d||(c=i.toValue(Math.min(o.value,3e4))),u=setTimeout(()=>{a.value=!0,e.refresh()},c)}return()=>{u&&clearTimeout(u)}},f=()=>{s.value?.()},{is:g}=b(i.toValue(t),!0);return g?{onBefore:()=>{a.value||(r.value=0),a.value=!1,f()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,f()}}:{}}),A=()=>C||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",E=new Set,Q=e=>(E.add(e),()=>{E.delete(e)}),X=()=>{A()&&E.forEach(e=>{e()})};!C&&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:f}=b(i.toValue(s),!0);if(e.state.error&&f!==0)return;const{is:g,value:u}=b(i.toValue(t));return g?(l=setTimeout(()=>{!i.toValue(n)&&!A()?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(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{t||(s?s():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),G=()=>(!C&&window.navigator?.onLine)??!0,x=new Set,I=e=>(x.add(e),()=>{x.delete(e)}),M=()=>{!A()||!G()||x.forEach(e=>{e()})};!C&&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||P(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const a=(o,l)=>{let f=!1;return()=>{f||(f=!0,o(),setTimeout(()=>{f=!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,f=a.leading,g=a.trailing,u=function(){s=null,r=null},h=function(){o=!0,e.apply(r,s),u()},v=function(){if(f===!0&&(l=null),o){u();return}if(g===!0){h();return}u()},y=function(){var c=l!==null;return c&&clearTimeout(l),u(),l=null,o=!1,c},d=function(){o=!1,s=arguments,r=this,l===null?f===!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:f}=b(i.toValue(t));if(!l)return;const g=i.toValue(n);a=ee(u=>u(),f,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),f=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 c=o!==null;return c&&clearTimeout(o),u(),o=null,a=!1,c},d=function(){if(s=arguments,r=this,a=!1,o===null&&f===!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(f=>f(),o,l),e.runAsync=(...f)=>new Promise((g,u)=>{s(()=>{r(...f).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