solid-request
Version:
solid-js use-request hooks library
2 lines (1 loc) • 14.7 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const Ee=(e,t)=>e===t,M={equals:Ee};let ge=me;const A=1,H=2,je={owned:null,cleanups:null,context:null,owner:null};var v=null;let R=null,b=null,p=null,w=null,I=0;function j(e,t){t=t?Object.assign({},M,t):M;const n={value:e,observers:null,observerSlots:null,comparator:t.equals||void 0},r=s=>(typeof s=="function"&&(s=s(n.value)),be(n,s));return[he.bind(n),r]}function T(e,t,n){ge=_e;const r=pe(e,t,!1,A),s=oe&&we(v,oe.id);s&&(r.suspense=s),r.user=!0,w?w.push(r):W(r)}function O(e,t,n){n=n?Object.assign({},M,n):M;const r=pe(e,t,!0,0);return r.observers=null,r.observerSlots=null,r.comparator=n.equals||void 0,W(r),he.bind(r)}function K(e){if(b===null)return e();const t=b;b=null;try{return e()}finally{b=t}}function ie(e,t,n){const r=Array.isArray(e);let s,i=n&&n.defer;return o=>{let u;if(r){u=Array(e.length);for(let c=0;c<e.length;c++)u[c]=e[c]()}else u=e();if(i){i=!1;return}const l=K(()=>t(u,s,o));return s=u,l}}function ee(e){T(()=>K(e))}function N(e){return v===null||(v.cleanups===null?v.cleanups=[e]:v.cleanups.push(e)),e}let oe;function he(){const e=R;if(this.sources&&(this.state||e))if(this.state===A||e)W(this);else{const t=p;p=null,V(()=>G(this),!1),p=t}if(b){const t=this.observers?this.observers.length:0;b.sources?(b.sources.push(this),b.sourceSlots.push(t)):(b.sources=[this],b.sourceSlots=[t]),this.observers?(this.observers.push(b),this.observerSlots.push(b.sources.length-1)):(this.observers=[b],this.observerSlots=[b.sources.length-1])}return this.value}function be(e,t,n){let r=e.value;return(!e.comparator||!e.comparator(r,t))&&(e.value=t,e.observers&&e.observers.length&&V(()=>{for(let s=0;s<e.observers.length;s+=1){const i=e.observers[s],o=R&&R.running;o&&R.disposed.has(i),(o&&!i.tState||!o&&!i.state)&&(i.pure?p.push(i):w.push(i),i.observers&&ve(i)),o||(i.state=A)}if(p.length>1e6)throw p=[],new Error},!1)),t}function W(e){if(!e.fn)return;te(e);const t=v,n=b,r=I;b=v=e,xe(e,e.value,r),b=n,v=t}function xe(e,t,n){let r;try{r=e.fn(t)}catch(s){return e.pure&&(e.state=A,e.owned&&e.owned.forEach(te),e.owned=null),e.updatedAt=n+1,Se(s)}(!e.updatedAt||e.updatedAt<=n)&&(e.updatedAt!=null&&"observers"in e?be(e,r):e.value=r,e.updatedAt=n)}function pe(e,t,n,r=A,s){const i={fn:e,state:r,updatedAt:null,owned:null,sources:null,sourceSlots:null,cleanups:null,value:t,owner:v,context:null,pure:n};return v===null||v!==je&&(v.owned?v.owned.push(i):v.owned=[i]),i}function q(e){const t=R;if(e.state===0||t)return;if(e.state===H||t)return G(e);if(e.suspense&&K(e.suspense.inFallback))return e.suspense.effects.push(e);const n=[e];for(;(e=e.owner)&&(!e.updatedAt||e.updatedAt<I);)(e.state||t)&&n.push(e);for(let r=n.length-1;r>=0;r--)if(e=n[r],e.state===A||t)W(e);else if(e.state===H||t){const s=p;p=null,V(()=>G(e,n[0]),!1),p=s}}function V(e,t){if(p)return e();let n=!1;t||(p=[]),w?n=!0:w=[],I++;try{const r=e();return $e(n),r}catch(r){n||(w=null),p=null,Se(r)}}function $e(e){if(p&&(me(p),p=null),e)return;const t=w;w=null,t.length&&V(()=>ge(t),!1)}function me(e){for(let t=0;t<e.length;t++)q(e[t])}function _e(e){let t,n=0;for(t=0;t<e.length;t++){const r=e[t];r.user?e[n++]=r:q(r)}for(t=0;t<n;t++)q(e[t])}function G(e,t){const n=R;e.state=0;for(let r=0;r<e.sources.length;r+=1){const s=e.sources[r];s.sources&&(s.state===A||n?s!==t&&(!s.updatedAt||s.updatedAt<I)&&q(s):(s.state===H||n)&&G(s,t))}}function ve(e){const t=R;for(let n=0;n<e.observers.length;n+=1){const r=e.observers[n];(!r.state||t)&&(r.state=H,r.pure?p.push(r):w.push(r),r.observers&&ve(r))}}function te(e){let t;if(e.sources)for(;e.sources.length;){const n=e.sources.pop(),r=e.sourceSlots.pop(),s=n.observers;if(s&&s.length){const i=s.pop(),o=n.observerSlots.pop();r<s.length&&(i.sourceSlots[o]=r,s[r]=i,n.observerSlots[r]=o)}}if(e.owned){for(t=0;t<e.owned.length;t++)te(e.owned[t]);e.owned=null}if(e.cleanups){for(t=0;t<e.cleanups.length;t++)e.cleanups[t]();e.cleanups=null}e.state=0,e.context=null}function Ce(e){return e instanceof Error||typeof e=="string"?e:new Error("Unknown error")}function Se(e){throw e=Ce(e),e}function we(e,t){return e?e.context&&e.context[t]!==void 0?e.context[t]:we(e.owner,t):void 0}function Ue(e){return typeof e=="function"}function d(e){return Ue(e)?e():e}const Te=(e,{manual:t,ready:n=!0,refreshDeps:r=[],refreshDepsAction:s})=>{const[i,o]=j(!1);return T(()=>{t||o(d(n))}),r instanceof Array?T(ie([i,...r],([u])=>{u&&!t&&u&&(s?s():e.refresh())})):T(ie(i,u=>{!t&&u&&(s?s():e.refresh())})),{onBefore:()=>{if(!d(n))return{stopNow:!0}}}};Te.onInit=({ready:e=!0,manual:t})=>({loading:!t&&d(e)});const E=new Map,Ne=(e,t,n)=>{const r=E.get(e);r?.timer&&clearTimeout(r.timer);let s;t>-1&&(s=setTimeout(()=>{E.delete(e)},t)),E.set(e,{...n,timer:s})},Fe=e=>E.get(e),Le=e=>{e?(Array.isArray(e)?e:[e]).forEach(n=>E.delete(n)):E.clear()},B=new Map,De=e=>B.get(e),Be=(e,t)=>{B.set(e,t),t.then(n=>(B.delete(e),n)).catch(n=>{throw B.delete(e),n})},P={},Me=(e,t)=>{P[e]&&P[e].forEach(n=>n(t))},Z=(e,t)=>(P[e]||(P[e]=[]),P[e].push(t),function(){const r=P[e].indexOf(t);P[e].splice(r,1)}),He=(e,{cacheKey:t,cacheTime:n=5*60*1e3,staleTime:r=0,setCache:s,getCache:i})=>{let o,u;const l=(a,f)=>{s?s(f):Ne(a,n,f),Me(a,f.data)},c=(a,f=[])=>i?i(f):Fe(a);return T(()=>{if(!t)return;const a=c(t);a&&Object.hasOwnProperty.call(a,"data")&&(e.state.data=a.data,e.state.params=a.params,(r===-1||new Date().getTime()-a.time<=r)&&(e.state.loading=!1)),o=Z(t,f=>{e.setState({data:f})})}),ee(()=>{N(()=>{o?.()})}),t?{onBefore:a=>{const f=c(t,a);return!f||!Object.hasOwnProperty.call(f,"data")?{}:r===-1||new Date().getTime()-f.time<=r?{loading:!1,data:f?.data,returnNow:!0}:{data:f?.data}},onRequest:(a,f)=>{let m=De(t);return m&&m!==u?{servicePromise:m}:(m=a(...f),u=m,Be(t,m),{servicePromise:m})},onSuccess:(a,f)=>{t&&(o?.(),l(t,{data:a,params:f,time:new Date().getTime()}),o=Z(t,m=>{e.setState({data:m})}))},onMutate:a=>{t&&(o?.(),l(t,{data:a,params:e.state.params,time:new Date().getTime()}),o=Z(t,f=>{e.setState({data:f})}))}}:{}};var D=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function qe(e){var t=typeof e;return e!=null&&(t=="object"||t=="function")}var ne=qe,Ge=typeof D=="object"&&D&&D.Object===Object&&D,Ie=Ge,We=Ie,Ve=typeof self=="object"&&self&&self.Object===Object&&self,Xe=We||Ve||Function("return this")(),ye=Xe,Qe=ye,ze=function(){return Qe.Date.now()},Je=ze,Ye=/\s/;function Ze(e){for(var t=e.length;t--&&Ye.test(e.charAt(t)););return t}var ke=Ze,Ke=ke,et=/^\s+/;function tt(e){return e&&e.slice(0,Ke(e)+1).replace(et,"")}var nt=tt,rt=ye,st=rt.Symbol,Pe=st,ue=Pe,Oe=Object.prototype,it=Oe.hasOwnProperty,ot=Oe.toString,_=ue?ue.toStringTag:void 0;function ut(e){var t=it.call(e,_),n=e[_];try{e[_]=void 0;var r=!0}catch{}var s=ot.call(e);return r&&(t?e[_]=n:delete e[_]),s}var at=ut,lt=Object.prototype,ct=lt.toString;function ft(e){return ct.call(e)}var dt=ft,ae=Pe,gt=at,ht=dt,bt="[object Null]",pt="[object Undefined]",le=ae?ae.toStringTag:void 0;function mt(e){return e==null?e===void 0?pt:bt:le&&le in Object(e)?gt(e):ht(e)}var vt=mt;function St(e){return e!=null&&typeof e=="object"}var wt=St,Tt=vt,yt=wt,Pt="[object Symbol]";function Ot(e){return typeof e=="symbol"||yt(e)&&Tt(e)==Pt}var Rt=Ot,At=nt,ce=ne,Et=Rt,fe=0/0,jt=/^[-+]0x[0-9a-f]+$/i,xt=/^0b[01]+$/i,$t=/^0o[0-7]+$/i,_t=parseInt;function Ct(e){if(typeof e=="number")return e;if(Et(e))return fe;if(ce(e)){var t=typeof e.valueOf=="function"?e.valueOf():e;e=ce(t)?t+"":t}if(typeof e!="string")return e===0?e:+e;e=At(e);var n=xt.test(e);return n||$t.test(e)?_t(e.slice(2),n?2:8):jt.test(e)?fe:+e}var Ut=Ct,Nt=ne,k=Je,de=Ut,Ft="Expected a function",Lt=Math.max,Dt=Math.min;function Bt(e,t,n){var r,s,i,o,u,l,c=0,a=!1,f=!1,m=!0;if(typeof e!="function")throw new TypeError(Ft);t=de(t)||0,Nt(n)&&(a=!!n.leading,f="maxWait"in n,i=f?Lt(de(n.maxWait)||0,t):i,m="trailing"in n?!!n.trailing:m);function x(h){var y=r,$=s;return r=s=void 0,c=h,o=e.apply($,y),o}function Q(h){return c=h,u=setTimeout(g,t),a?x(h):o}function z(h){var y=h-l,$=h-c,se=t-y;return f?Dt(se,i-$):se}function F(h){var y=h-l,$=h-c;return l===void 0||y>=t||y<0||f&&$>=i}function g(){var h=k();if(F(h))return S(h);u=setTimeout(g,z(h))}function S(h){return u=void 0,m&&r?x(h):(r=s=void 0,o)}function L(){u!==void 0&&clearTimeout(u),c=0,r=l=s=u=void 0}function J(){return u===void 0?o:S(k())}function Y(){var h=k(),y=F(h);if(r=arguments,s=this,l=h,y){if(u===void 0)return Q(l);if(f)return clearTimeout(u),u=setTimeout(g,t),x(l)}return u===void 0&&(u=setTimeout(g,t)),o}return Y.cancel=L,Y.flush=J,Y}var Re=Bt;const Mt=(e,{debounceWait:t,debounceLeading:n,debounceTrailing:r,debounceMaxWait:s})=>{const[i,o]=j(),u=O(()=>{const l={},c=d(n),a=d(r),f=d(s);return c!==void 0&&(l.leading=c),a!==void 0&&(l.trailing=a),f!==void 0&&(l.maxWait=f),l});return T(()=>{if(d(t)){const l=e.runAsync.bind(e);o(()=>Re(c=>{c()},d(t),u())),e.runAsync=(...c)=>new Promise((a,f)=>{i()?.(()=>{l(...c).then(a).catch(f)})}),N(()=>{i()?.cancel(),e.runAsync=l})}}),d(t)?{onCancel:()=>{i()?.cancel()}}:{}},Ht=(e,{loadingDelay:t})=>{let n;if(!d(t))return{};const r=()=>{n&&clearTimeout(n)};return{onBefore:()=>(r(),n=setTimeout(()=>{e.setState({loading:!0})},d(t)),{loading:!1}),onFinally:()=>{r()},onCancel:()=>{r()}}};function X(){return!!(typeof window<"u"&&window.document&&window.document.createElement)}function re(){return X()?document.visibilityState!=="hidden":!0}const C=[];function qt(e){return C.push(e),function(){const n=C.indexOf(e);C.splice(n,1)}}if(X()){const e=()=>{if(re())for(let t=0;t<C.length;t++){const n=C[t];n()}};window.addEventListener("visibilitychange",e,!1)}const Gt=(e,{pollingInterval:t,pollingWhenHidden:n=!0,pollingErrorRetryCount:r=-1})=>{let s,i;const[o,u]=j(0),l=()=>{s&&clearInterval(s),i?.()};return T(()=>{d(t)||l()}),d(t)?{onBefore:()=>{l()},onError:()=>{u(c=>c+1)},onSuccess:()=>{u(()=>0)},onFinally:()=>{r===-1||r!==-1&&o()<=r?s=setTimeout(()=>{!n&&!re()?i=qt(()=>{e.refresh()}):e.refresh()},d(t)):u(()=>0)},onCancel:()=>{l()}}:{}};function It(e,t){let n=!1;return(...r)=>{n||(n=!0,e(...r),setTimeout(()=>{n=!1},t))}}function Wt(){return X()&&typeof navigator.onLine<"u"?navigator.onLine:!0}const U=[];function Vt(e){return U.push(e),function(){const n=U.indexOf(e);U.splice(n,1)}}if(X()){const e=()=>{if(!(!re()||!Wt()))for(let t=0;t<U.length;t++){const n=U[t];n()}};window.addEventListener("visibilitychange",e,!1),window.addEventListener("focus",e,!1)}const Xt=(e,{refreshOnWindowFocus:t,focusTimespan:n=5e3})=>{let r;const s=()=>{r?.()};return T(()=>{if(d(t)){const i=It(e.refresh.bind(e),d(n));r=Vt(()=>{i()})}N(()=>{s()})}),ee(()=>{N(()=>{s()})}),{}},Qt=(e,{retryInterval:t,retryCount:n})=>{let r;const[s,i]=j(0),[o,u]=j(!1);return n?{onBefore:()=>{o()||i(()=>0),u(()=>!1),r&&clearTimeout(r)},onSuccess:()=>{i(()=>0)},onError:()=>{if(i(l=>l+1),n===-1||s()<=n){const l=t??Math.min(1e3*2**s(),3e4);r=setTimeout(()=>{u(()=>!0),e.refresh()},l)}else i(()=>0)},onCancel:()=>{i(()=>0),r&&clearTimeout(r)}}:{}};var zt=Re,Jt=ne,Yt="Expected a function";function Zt(e,t,n){var r=!0,s=!0;if(typeof e!="function")throw new TypeError(Yt);return Jt(n)&&(r="leading"in n?!!n.leading:r,s="trailing"in n?!!n.trailing:s),zt(e,t,{leading:r,maxWait:t,trailing:s})}var kt=Zt;const Kt=(e,{throttleWait:t,throttleLeading:n,throttleTrailing:r})=>{const s=O(()=>{const o={};return d(n)!==void 0&&(o.leading=d(n)),d(r)!==void 0&&(o.trailing=d(r)),o}),i=O(()=>kt(o=>{o()},d(t),s()));return T(()=>{if(d(t)){const o=e.runAsync.bind(e);e.runAsync=(...u)=>new Promise((l,c)=>{i()?.(()=>{o(...u).then(l).catch(c)})}),N(()=>{e.runAsync=o,i()?.cancel()})}}),d(t)?{onCancel:()=>{i()?.cancel()}}:{}};class en{constructor(t,n,r,s={}){this.serviceRef=t,this.options=n,this.setUpdateData=r,this.initState=s,this.state={...this.state,loading:!n.manual,...s}}pluginImpls;count=0;servicePreset;state={loading:!1,params:void 0,data:void 0,error:void 0};setState(t={}){this.state={...this.state,...t},this.setUpdateData(this.state)}setData(t,n){n instanceof Array?n.forEach(r=>{this.state[r]=t,this.setUpdateData(t,r)}):(this.state[n]=t,this.setUpdateData(t,n))}runPluginHandler(t,...n){const r=(this.pluginImpls?.map(s=>s[t]?.(...n))??[])?.filter(Boolean);return Object.assign({},...r)}setServicePreset(t){t?this.servicePreset=t:this.servicePreset=null}async runAsync(...t){this.count+=1;const n=this.count,{stopNow:r=!1,returnNow:s=!1,...i}=this.runPluginHandler("onBefore",t);if(r)return new Promise(()=>{});if(this.setState({loading:!0,params:t,...i}),s)return Promise.resolve(i.data);this.options.onBefore?.(t);try{let{servicePromise:o}=this.runPluginHandler("onRequest",this.serviceRef,t);const u=c=>{if(n!==this.count)return new Promise(()=>{});const a=this.options.formatResult?this.options.formatResult(c):c;return this.setState({data:a,error:void 0,loading:!1}),this.options.onSuccess?.(a,t),this.runPluginHandler("onSuccess",a,t),this.options.onFinally?.(t,a,void 0),n===this.count&&this.runPluginHandler("onFinally",t,a,void 0),this.setServicePreset(null),a};o||(o=this.servicePreset??this.serviceRef(...t));const l=await o;return u(l)}catch(o){if(n!==this.count)return new Promise(()=>{});throw this.setState({error:o,loading:!1}),this.options.onError?.(o,t),this.runPluginHandler("onError",o,t),this.options.onFinally?.(t,void 0,o),n===this.count&&this.runPluginHandler("onFinally",t,void 0,o),o}}run(...t){this.runAsync(...t).catch(n=>{this.options.onError||console.error(n)})}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(t){let n;typeof t=="function"?n=t?.(this.state.data):n=t,this.runPluginHandler("onMutate",n),this.setState({data:n})}}function tn(e,t={},n=[]){const{initialData:r=void 0,manual:s=!1,ready:i=!0,...o}={...t??{}},u={manual:s,ready:i,...o},l=e,[c,a]=j({data:r,loading:!1,params:void 0,error:void 0}),f=(S,L)=>{a(L?J=>({...J,[L]:S}):S)},m=O(()=>c().data),x=O(()=>c().loading),Q=O(()=>c().params),z=O(()=>c().error),F=n.map(S=>S?.onInit?.(u)).filter(Boolean),g=new en(l,u,f,Object.assign({},...F,c));return g.options=u,g.pluginImpls=n.map(S=>S(g,u)),ee(()=>{if(!s){const S=g.state.params||t.defaultParams||[];d(i)&&g.run(...S)}}),{data:m,error:z,loading:x,params:Q,cancel:g.cancel.bind(g),refresh:g.refresh.bind(g),refreshAsync:g.refreshAsync.bind(g),run:g.run.bind(g),runAsync:g.runAsync.bind(g),mutate:g.mutate.bind(g)}}function Ae(e,t,n){return tn(e,t,[...n||[],Mt,Ht,Gt,Xt,Kt,Te,He,Qt])}function nn(e,t,n){return Ae(e,t,n)}exports.clearUseRequestCache=Le;exports.createUseRequest=nn;exports.default=Ae;