UNPKG

@caikengren/uni-use-request

Version:
1 lines 9.38 kB
(function(e,t,n,r){var i=Object.create,a=Object.defineProperty,o=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,c=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,u=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=s(t),c=0,u=i.length,d;c<u;c++)d=i[c],!l.call(e,d)&&d!==n&&a(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=o(t,d))||r.enumerable});return e},d=(e,t,n)=>(n=e==null?{}:i(c(e)),u(t||!e||!e.__esModule?a(n,`default`,{value:e,enumerable:!0}):n,e));t=d(t),n=d(n),r=d(r);let f=(e,{manual:n,ready:r=!0,refreshDeps:i=[],refreshDepsAction:a})=>{let o=(0,t.ref)(!1);return(0,t.watchEffect)(()=>{!n&&e.options.refreshDeps!==!0&&(o.value=(0,t.unref)(r))}),Array.isArray(i)?(0,t.watch)([o,...i],([t])=>{t&&!n&&t&&(a?a():e.refresh())},{deep:!0,immediate:!1}):(0,t.watch)(o,t=>{!n&&t&&(a?a():e.refresh())}),{name:`autoRunPlugin`,onBefore:()=>{if(!(0,t.unref)(r))return{stopNow:!0}}}};f.onInit=({ready:e=!0,manual:n})=>({loading:!n&&(0,t.unref)(e)});var p=f;let m=new Map;function h(e,t,n){let r=m.get(e);r!=null&&r.timer&&clearTimeout(r.timer);let i;t>-1&&(i=setTimeout(()=>{m.delete(e)},t)),m.set(e,{...n,timer:i})}let g=e=>m.get(e),_=new Map,v=e=>_.get(e);function y(e,t){_.set(e,t),t.then(t=>(_.delete(e),t)).catch(t=>{throw _.delete(e),t})}let b={},x=[];function S(e,t){b[e]&&(b[e].forEach(e=>e(t)),x.forEach(n=>n({type:e,data:t})))}function C(e,t){return b[e]||(b[e]=[]),b[e].push(t),function(){let n=b[e].indexOf(t);b[e].splice(n,1)}}var w=(e,{cacheKey:n,cacheTime:r=300*1e3,staleTime:i=0,setCache:a,getCache:o})=>{let s=(0,t.ref)(),c=(0,t.ref)(),l=(e,t)=>{a?a(t):h(e,r,t),S(e,t.data)},u=(e,t=[])=>o?o(t):g(e);return(0,t.watchEffect)(()=>{if(!n)return;let t=u(n);t&&Object.hasOwnProperty.call(t,`data`)&&(e.state.data=t.data,e.state.params=t.params,(i===-1||new Date().getTime()-t.time<=i)&&(e.state.loading=!1)),s.value=C(n,t=>{e.setState({data:t})})}),(0,t.onScopeDispose)(()=>{var e;(e=s.value)==null||e.call(s)}),n?{name:`cachePlugin`,onBefore:e=>{let t=u(n,e);return!t||!Object.hasOwnProperty.call(t,`data`)?{}:i===-1||new Date().getTime()-t.time<=i?{loading:!1,data:t==null?void 0:t.data,returnNow:!0}:{data:t==null?void 0:t.data}},onRequest:(e,t)=>{let r=v(n);return r&&r!==c.value?{servicePromise:r}:(r=e(...t),c.value=r,y(n,r),{servicePromise:r})},onSuccess:(t,r)=>{if(n){var i;(i=s.value)==null||i.call(s),l(n,{data:t,params:r,time:new Date().getTime()}),s.value=C(n,t=>{e.setState({data:t})})}},onMutate:t=>{if(n){var r;(r=s.value)==null||r.call(s),l(n,{data:t,params:e.state.params,time:new Date().getTime()}),s.value=C(n,t=>{e.setState({data:t})})}}}:{}},T=(e,{debounceWait:r,debounceMaxWait:i})=>{let a=(0,t.ref)();return(0,t.watchEffect)(o=>{if((0,t.unref)(r)){let s=e.runAsync.bind(e);a.value=(0,n.createFilterWrapper)((0,n.debounceFilter)(r||200,{maxWait:(0,t.unref)(i)}),e=>{e()}),e.runAsync=(...e)=>new Promise((t,n)=>{var r;(r=a.value)==null||r.call(a,()=>{s(...e).then(t).catch(n)})}),o(()=>{var t;(t=a.value)==null||t.cancel(),e.runAsync=s})}}),(0,t.unref)(r)?{name:`debouncePlugin`,onCancel:()=>{var e;(e=a.value)==null||e.cancel()}}:{}},E=(e,{loadingDelay:n})=>{let r=(0,t.ref)(),i=()=>{r.value&&(clearTimeout((0,t.unref)(r.value)),r.value=void 0)};return{name:`loadingDelayPlugin`,onFinally:()=>{i();let a=(0,t.unref)(n);a&&(e.setState({loading:!0}),r.value=setTimeout(()=>{e.setState({loading:!1})},a))},onError:()=>{i()}}},D=(e,{pollingInterval:n,pollingWhenHidden:i=!0,pollingErrorRetryCount:a=-1})=>{let o,s=(0,t.ref)(null),c=(0,t.ref)(0),l=(0,t.ref)(!1);(0,r.onPageShow)(()=>{l.value=!0,s.value&&(s.value(),s.value=null)}),(0,r.onPageHide)(()=>{l.value=!1});let u=()=>{o&&clearTimeout(o),s.value=null};return(0,t.watchEffect)(()=>{(0,t.unref)(n)||u()}),(0,t.unref)(n)?{name:`pollingPlugin`,onBefore:()=>{u()},onError:()=>{c.value+=1},onSuccess:()=>{c.value=0},onFinally:()=>{a===-1||a!==-1&&c.value<=a?o=setTimeout(()=>{!i&&!l.value?s.value=()=>{e.refresh()}:e.refresh()},(0,t.unref)(n)):c.value=0},onCancel:()=>{u()}}:{}},O=(e,{retryInterval:n,retryCount:r})=>{let i=(0,t.ref)(),a=(0,t.ref)(0),o=(0,t.ref)(!1);return r?{name:`retryPlugin`,onBefore:()=>{o.value||(a.value=0),o.value=!1,i.value&&clearTimeout(i.value)},onSuccess:()=>{a.value=0},onError:()=>{if(a.value+=1,r===-1||a.value<=r){let t=n==null?Math.min(1e3*2**a.value,3e4):n;i.value=setTimeout(()=>{o.value=!0,e.refresh()},t)}else a.value=0},onCancel:()=>{a.value=0,i.value&&clearTimeout(i.value)}}:{}},k=(e,{throttleWait:r,throttleLeading:i,throttleTrailing:a})=>{let o=(0,t.computed)(()=>{let e={delay:200};return(0,t.unref)(r)!==void 0&&(e.delay=(0,t.unref)(r)||200),(0,t.unref)(i)!==void 0&&(e.leading=(0,t.unref)(i)),(0,t.unref)(a)!==void 0&&(e.trailing=(0,t.unref)(a)),e}),s=(0,t.computed)(()=>(0,n.createFilterWrapper)((0,n.throttleFilter)(o.value),e=>{e()}));return(0,t.watchEffect)(n=>{if((0,t.unref)(r)){let t=e.runAsync.bind(e);e.runAsync=(...e)=>new Promise((n,r)=>{var i;(i=s.value)==null||i.call(s,()=>{t(...e).then(n).catch(r)})}),n(()=>{var n;e.runAsync=t,(n=s.value)==null||n.cancel()})}}),(0,t.unref)(r)?{name:`throttlePlugin`,onCancel:()=>{var e;(e=s.value)==null||e.cancel()}}:{}};let A=Symbol(`USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY`),j=e=>typeof e==`function`,M=e=>typeof e==`boolean`;var N=class{constructor(e,t,n,r={}){this.serviceRef=e,this.options=t,this.setUpdateData=n,this.initState=r,this.count=0,this.state={loading:!1,params:void 0,data:void 0,error:void 0},this.previousValidData=void 0,this.state={...this.state,loading:!t.manual,...r}}setState(e={}){this.state={...this.state,...e},this.setUpdateData(this.state)}setFetchState(e,t){Array.isArray(t)?t.forEach(t=>{this.state[t]=e,this.setUpdateData(e,t)}):(this.state[t]=e,this.setUpdateData(e,t))}runPluginHandler(e,...t){var n,r,i;let a=(n=(r=(i=this.pluginImpls)==null?void 0:i.map(n=>{var r;return(r=n[e])==null?void 0:r.call(n,...t)}))==null?[]:r)==null?void 0:n.filter(Boolean);return Object.assign({},...a)}async runAsync(...e){this.count+=1;let t=this.count,{stopNow:n=!1,returnNow:r=!1,...i}=this.runPluginHandler(`onBefore`,e);if(n)return new Promise(()=>{});if(this.setState({loading:!0,params:e,...i}),r)return Promise.resolve(i.data);try{var a,o;(a=(o=this.options).onBefore)==null||a.call(o,e)}catch(t){var s,c;return this.setState({error:t,loading:!1}),(s=(c=this.options).onError)==null||s.call(c,t,e),this.runPluginHandler(`onError`,t,e),new Promise(()=>{})}try{let{servicePromise:n}=this.runPluginHandler(`onRequest`,this.serviceRef.value,e),r=n=>{var r,i,a,o;if(t!==this.count)return new Promise(()=>{});let s=this.options.formatResult?this.options.formatResult(n):n;return this.setState({data:s,error:void 0,loading:!1}),(r=(i=this.options).onSuccess)==null||r.call(i,s,e),this.runPluginHandler(`onSuccess`,s,e),this.previousValidData=s,(a=(o=this.options).onFinally)==null||a.call(o,e,s,void 0),t===this.count&&this.runPluginHandler(`onFinally`,e,s,void 0),s};return n||(n=this.serviceRef.value(...e)),r(await n)}catch(n){var l,u,d,f,p,m,h;if(t!==this.count)return new Promise(()=>{});throw this.setState({error:n,loading:!1}),(l=(u=this.options).onError)==null||l.call(u,n,e),this.runPluginHandler(`onError`,n,e),(j((d=this.options)==null?void 0:d.rollbackOnError)&&(f=this.options)!=null&&f.rollbackOnError(e)||M((p=this.options)==null?void 0:p.rollbackOnError)&&this.options.rollbackOnError)&&this.setState({data:this.previousValidData}),(m=(h=this.options).onFinally)==null||m.call(h,e,void 0,n),t===this.count&&this.runPluginHandler(`onFinally`,e,void 0,n),n}}run(...e){this.runAsync(...e).catch(e=>{this.options.onError||console.error(e)})}cancel(){this.count+=1,this.setState({loading:!1}),this.runPluginHandler(`onCancel`)}refresh(){this.run(...this.state.params||[])}refreshAsync(){return this.runAsync(...this.state.params||[])}mutate(e){let t=j(e)?e(this.state.data):e;this.runPluginHandler(`onMutate`,t),this.setState({data:t})}};function P(e){return Object.keys(e).filter(e=>[`data`,`loading`,`params`,`error`].includes(e)).length===4}function F(e,n={},r=[]){let i=(0,t.inject)(A,{}),{initialData:a=void 0,manual:o=!1,ready:s=!0,...c}={...i==null?{}:i,...n==null?{}:n},l={manual:o,ready:s,initialData:a,...c},u=(0,t.ref)(e),d=(0,t.reactive)({data:a,loading:!1,params:void 0,error:void 0}),f=(e,t)=>{t?d[t]=e:P(e)&&(d.data=e.data,d.loading=e.loading,d.error=e.error,d.params=e.params)},p=r.map(e=>{var t;return e==null||(t=e.onInit)==null?void 0:t.call(e,l)}).filter(Boolean),m=new N(u,l,f,Object.assign({},...p,d));m.options=l,m.pluginImpls=r.map(e=>e(m,l));let h=(0,t.computed)(()=>(0,t.isRef)(s)?s.value:s);if((0,t.watchEffect)(()=>{if(!o){let e=m.state.params||n.defaultParams||[];h.value&&m.options.refreshDeps===!0&&u.value&&m.run(...e)}}),!o&&m.options.refreshDeps!==!0){let e=m.state.params||n.defaultParams||[];(0,t.unref)(s)&&m.run(...e)}return(0,t.onScopeDispose)(()=>{m.cancel()}),{...(0,t.toRefs)(d),cancel:m.cancel.bind(m),refresh:m.refresh.bind(m),refreshAsync:m.refreshAsync.bind(m),run:m.run.bind(m),runAsync:m.runAsync.bind(m),mutate:m.mutate.bind(m)}}var I=F;function L(e,t){return function(n,r={},i=[]){let a=e,o=t||[];for(let e=o.length;e--;)a=o[e](a);return a(n,r,i)}}function R(e,t,n){var r;let i=(r=[null,T,E,D,k,p,w,O])==null?void 0:r.filter(Boolean);return L(I,t==null?void 0:t.use)(e,t,[...n||[],...i])}e.useRequest=R})(this.UniHooks=this.UniHooks||{},Vue,__caikengren_uni_hooks_shared,__dcloudio_uni_app);