vue3-request 1.0.26 → 1.0.27

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 +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()),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;
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:c}=b(t,!0);o&&(u?.timer&&clearTimeout(u.timer),c!==-1&&(a=setTimeout(()=>{A(e)},c)),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);o||(o=u());const c=await o;return n!==this.currentRequestId?V():(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 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:c,refreshAsync:v,abort:l}=r;return{...s.toRefs(r.state),run:a,cancel:u,refresh:o,runAsync:c,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: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()}}:{}}),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: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)&&!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 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=A;exports.definePlugin=p;exports.useRequest=ie;
@@ -28,10 +28,10 @@ const z = (e, t) => {
28
28
  }, A = () => new Promise(() => {
29
29
  }), C = /* @__PURE__ */ new Map(), U = (e, t, { data: n, params: i, time: r }) => {
30
30
  let s;
31
- const a = V(e), { is: u, value: c } = p(t, !0);
32
- u && (a?.timer && clearTimeout(a.timer), c !== -1 && (s = setTimeout(() => {
31
+ const a = V(e), { is: u, value: l } = p(t, !0);
32
+ u && (a?.timer && clearTimeout(a.timer), l !== -1 && (s = setTimeout(() => {
33
33
  _(e);
34
- }, c)), C.set(e, { data: n, params: i, time: r, timer: s }));
34
+ }, l)), C.set(e, { data: n, params: i, time: r, timer: s }));
35
35
  }, V = (e) => C.get(e), _ = (e) => {
36
36
  e && typeof e == "string" ? C.delete(e) : C.clear();
37
37
  };
@@ -100,7 +100,9 @@ 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()), 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);
103
+ u || (u = a());
104
+ const l = await u;
105
+ return n !== this.currentRequestId ? A() : (this.setState({ data: l, error: void 0 }), this.executePlugin("onSuccess", t), this.options.onSuccess?.(t), this.onFinished(), this.state.data);
104
106
  } catch (a) {
105
107
  if (n !== this.currentRequestId)
106
108
  return A();
@@ -133,13 +135,13 @@ function Q(e, t, n) {
133
135
  r.run(...h);
134
136
  }
135
137
  }), F(r.cancel);
136
- const { run: s, cancel: a, refresh: u, runAsync: c, refreshAsync: v, abort: o } = r;
138
+ const { run: s, cancel: a, refresh: u, runAsync: l, refreshAsync: v, abort: o } = r;
137
139
  return {
138
140
  ...W(r.state),
139
141
  run: s,
140
142
  cancel: a,
141
143
  refresh: u,
142
- runAsync: c,
144
+ runAsync: l,
143
145
  abort: o,
144
146
  refreshAsync: v,
145
147
  clearCache: _
@@ -189,19 +191,19 @@ const X = b((e, { abortPrevious: t = !0 }) => {
189
191
  }) => {
190
192
  const a = R(null);
191
193
  let u = null;
192
- const { is: c, value: v } = p(i, !0);
193
- if (!c)
194
+ const { is: l, value: v } = p(i, !0);
195
+ if (!l)
194
196
  return {};
195
- const o = typeof t == "function" ? t : () => t, h = (f, l) => {
196
- r ? r(f, l) : U(f, n, l), Y(f, l);
197
+ const o = typeof t == "function" ? t : () => t, h = (f, c) => {
198
+ r ? r(f, c) : U(f, n, c), Y(f, c);
197
199
  }, m = (f) => s ? s(f) : V(f);
198
200
  function w() {
199
201
  const f = o();
200
202
  if (!f) return;
201
- const l = m(f);
202
- l && Reflect.has(l, "data") && e.setState({
203
- data: l.data,
204
- params: l.params
203
+ const c = m(f);
204
+ c && Reflect.has(c, "data") && e.setState({
205
+ data: c.data,
206
+ params: c.params
205
207
  }), a.value = q(f, (g) => {
206
208
  e.setState({
207
209
  data: g.data
@@ -212,9 +214,9 @@ const X = b((e, { abortPrevious: t = !0 }) => {
212
214
  a.value?.();
213
215
  }), {
214
216
  onBefore: (f) => {
215
- const l = o(f);
216
- if (!l) return null;
217
- const g = m(l);
217
+ const c = o(f);
218
+ if (!c) return null;
219
+ const g = m(c);
218
220
  return !g || !Reflect.has(g, "data") ? {} : v === -1 || Date.now() - g.time < v ? {
219
221
  data: g.data,
220
222
  error: void 0,
@@ -225,18 +227,18 @@ const X = b((e, { abortPrevious: t = !0 }) => {
225
227
  };
226
228
  },
227
229
  onRequest: (f) => {
228
- const l = o(e.state.params);
229
- if (!l) return f;
230
- let g = G(l);
231
- return g && g !== u ? () => g : (g = f(), u = g, Z(l, g), () => g);
230
+ const c = o(e.state.params);
231
+ if (!c) return f;
232
+ let g = G(c);
233
+ return g && g !== u ? () => g : (g = f(), u = g, Z(c, g), () => g);
232
234
  },
233
235
  onSuccess: (f) => {
234
- const l = o(f);
235
- l && (a.value?.(), h(l, {
236
+ const c = o(f);
237
+ c && (a.value?.(), h(c, {
236
238
  data: e.state.data,
237
239
  params: f,
238
240
  time: Date.now()
239
- }), a.value = q(l, (g) => {
241
+ }), a.value = q(c, (g) => {
240
242
  e.setState({
241
243
  data: g.data
242
244
  });
@@ -252,20 +254,20 @@ const X = b((e, { abortPrevious: t = !0 }) => {
252
254
  r.value++;
253
255
  const { value: h } = p(d(t), !0), m = h === -1, w = r.value <= h;
254
256
  if (m || w) {
255
- let { is: f, value: l } = p(d(n));
256
- f || (l = d(Math.min(a.value, 3e4))), o = setTimeout(() => {
257
+ let { is: f, value: c } = p(d(n));
258
+ f || (c = d(Math.min(a.value, 3e4))), o = setTimeout(() => {
257
259
  s.value = !0, e.refresh();
258
- }, l);
260
+ }, c);
259
261
  }
260
262
  return () => {
261
263
  o && clearTimeout(o);
262
264
  };
263
- }, c = () => {
265
+ }, l = () => {
264
266
  i.value?.();
265
267
  }, { is: v } = p(d(t), !0);
266
268
  return v ? {
267
269
  onBefore: () => {
268
- s.value || (r.value = 0), s.value = !1, c();
270
+ s.value || (r.value = 0), s.value = !1, l();
269
271
  },
270
272
  onSuccess: () => {
271
273
  r.value = 0;
@@ -274,7 +276,7 @@ const X = b((e, { abortPrevious: t = !0 }) => {
274
276
  i.value = u();
275
277
  },
276
278
  onCancel: () => {
277
- r.value = 0, c();
279
+ r.value = 0, l();
278
280
  }
279
281
  } : {};
280
282
  }), M = () => T || P(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", E = /* @__PURE__ */ new Set(), ee = (e) => (E.add(e), () => {
@@ -289,11 +291,11 @@ const ne = b(
289
291
  (e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: i }) => {
290
292
  const r = R(null), s = R(), a = () => {
291
293
  let u;
292
- const { value: c } = p(
294
+ const { value: l } = p(
293
295
  d(i),
294
296
  !0
295
297
  );
296
- if (e.state.error && c !== 0) return;
298
+ if (e.state.error && l !== 0) return;
297
299
  const { is: v, value: o } = p(
298
300
  d(t)
299
301
  );
@@ -355,10 +357,10 @@ const ae = b(
355
357
  if (!r || P(d(n)) || typeof d(n) != "boolean")
356
358
  return {};
357
359
  const s = (a, u) => {
358
- let c = !1;
360
+ let l = !1;
359
361
  return () => {
360
- c || (c = !0, a(), setTimeout(() => {
361
- c = !1;
362
+ l || (l = !0, a(), setTimeout(() => {
363
+ l = !1;
362
364
  }, u));
363
365
  };
364
366
  };
@@ -390,12 +392,12 @@ const ae = b(
390
392
  }
391
393
  );
392
394
  function oe(e, t, n) {
393
- var i = null, r = null, s = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), a = !1, u = null, c = s.leading, v = s.trailing, o = function() {
395
+ var i = null, r = null, s = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), a = !1, u = null, l = s.leading, v = s.trailing, o = function() {
394
396
  i = null, r = null;
395
397
  }, h = function() {
396
398
  a = !0, e.apply(r, i), o();
397
399
  }, m = function() {
398
- if (c === !0 && (u = null), a) {
400
+ if (l === !0 && (u = null), a) {
399
401
  o();
400
402
  return;
401
403
  }
@@ -405,10 +407,10 @@ function oe(e, t, n) {
405
407
  }
406
408
  o();
407
409
  }, w = function() {
408
- var l = u !== null;
409
- return l && clearTimeout(u), o(), u = null, a = !1, l;
410
+ var c = u !== null;
411
+ return c && clearTimeout(u), o(), u = null, a = !1, c;
410
412
  }, f = function() {
411
- a = !1, i = arguments, r = this, u === null ? c === !0 && h() : clearTimeout(u), u = setTimeout(m, t);
413
+ a = !1, i = arguments, r = this, u === null ? l === !0 && h() : clearTimeout(u), u = setTimeout(m, t);
412
414
  };
413
415
  return f.cancel = w, f;
414
416
  }
@@ -418,12 +420,12 @@ const le = b(
418
420
  let s = null;
419
421
  const a = e.runAsync;
420
422
  return i || (r.value = !0), L(() => {
421
- const { is: u, value: c } = p(d(t));
423
+ const { is: u, value: l } = p(d(t));
422
424
  if (!u) return;
423
425
  const v = d(n);
424
426
  s = oe(
425
427
  (o) => o(),
426
- c,
428
+ l,
427
429
  v
428
430
  ), e.runAsync = (...o) => new Promise((h, m) => {
429
431
  r.value ? (r.value = !1, a(...o).then(h).catch(m)) : s(() => {
@@ -440,7 +442,7 @@ const le = b(
440
442
  }
441
443
  );
442
444
  function ce(e, t, n) {
443
- var i = null, r = null, s = !1, a = null, u = Object.assign({ leading: !0, trailing: !0 }, n), c = u.leading, v = u.trailing, o = function() {
445
+ var i = null, r = null, s = !1, a = null, u = Object.assign({ leading: !0, trailing: !0 }, n), l = u.leading, v = u.trailing, o = function() {
444
446
  i = null, r = null;
445
447
  }, h = function() {
446
448
  s = !0, e.apply(r, i), a = setTimeout(m, t), o();
@@ -455,10 +457,10 @@ function ce(e, t, n) {
455
457
  }
456
458
  o();
457
459
  }, w = function() {
458
- var l = a !== null;
459
- return l && clearTimeout(a), o(), a = null, s = !1, l;
460
+ var c = a !== null;
461
+ return c && clearTimeout(a), o(), a = null, s = !1, c;
460
462
  }, f = function() {
461
- if (i = arguments, r = this, s = !1, a === null && c === !0) {
463
+ if (i = arguments, r = this, s = !1, a === null && l === !0) {
462
464
  h();
463
465
  return;
464
466
  }
@@ -475,12 +477,12 @@ const fe = b(
475
477
  if (!s) return;
476
478
  const u = d(n);
477
479
  i = ce(
478
- (c) => c(),
480
+ (l) => l(),
479
481
  a,
480
482
  u
481
- ), e.runAsync = (...c) => new Promise((v, o) => {
483
+ ), e.runAsync = (...l) => new Promise((v, o) => {
482
484
  i(() => {
483
- r(...c).then(v).catch(o);
485
+ r(...l).then(v).catch(o);
484
486
  });
485
487
  }), N(() => {
486
488
  i?.cancel(), e.runAsync = r;
@@ -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()),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"})});
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:c}=b(t,!0);l&&(o?.timer&&clearTimeout(o.timer),c!==-1&&(a=setTimeout(()=>{F(e)},c)),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);l||(l=o());const c=await l;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(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: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.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: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()}}:{}}),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: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)&&!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 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.26",
3
+ "version": "1.0.27",
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",