@hyper-fetch/react 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -9
- package/dist/index.cjs.js +1451 -1
- package/dist/index.cjs.js.map +3 -3
- package/dist/index.d.ts +19 -16
- package/dist/index.esm.js +1430 -1
- package/dist/index.esm.js.map +3 -3
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +7 -7
- package/src/hooks/use-cache/use-cache.hooks.ts +5 -5
- package/src/hooks/use-cache/use-cache.types.ts +2 -2
- package/src/hooks/use-emitter/use-emitter.hooks.ts +2 -2
- package/src/hooks/use-event-messages/use-event-messages.hooks.ts +4 -4
- package/src/hooks/use-event-messages/use-event-messages.types.ts +1 -1
- package/src/hooks/use-fetch/use-fetch.constants.ts +5 -5
- package/src/hooks/use-fetch/use-fetch.hooks.ts +20 -20
- package/src/hooks/use-fetch/use-fetch.types.ts +7 -7
- package/src/hooks/use-listener/use-listener.hooks.ts +17 -10
- package/src/hooks/use-submit/use-submit.hooks.ts +8 -8
- package/src/hooks/use-submit/use-submit.types.ts +2 -2
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,1431 @@
|
|
|
1
|
-
var Le=Object.defineProperty,Me=Object.defineProperties;var we=Object.getOwnPropertyDescriptors;var xe=Object.getOwnPropertySymbols;var Ke=Object.prototype.hasOwnProperty,Qe=Object.prototype.propertyIsEnumerable;var Ee=(e,n,o)=>n in e?Le(e,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[n]=o,r=(e,n)=>{for(var o in n||(n={}))Ke.call(n,o)&&Ee(e,o,n[o]);if(xe)for(var o of xe(n))Qe.call(n,o)&&Ee(e,o,n[o]);return e},C=(e,n)=>Me(e,we(n));var _=(e,n,o)=>new Promise((R,m)=>{var l=s=>{try{y(o.next(s))}catch(d){m(d)}},T=s=>{try{y(o.throw(s))}catch(d){m(d)}},y=s=>s.done?R(s.value):Promise.resolve(s.value).then(l,T);y((o=o.apply(e,n)).next())});import{useRef as Ye}from"react";import{useDidUpdate as de,useDidMount as Ze}from"@better-hooks/lifecycle";import{useDebounce as Oe,useThrottle as _e}from"@better-hooks/performance";import{Request as Ce,getRequestKey as et}from"@hyper-fetch/core";import{useRef as J}from"react";import{useWillUnmount as Be}from"@better-hooks/lifecycle";var ee=({request:e,dispatcher:n,logger:o,actions:R,setCacheData:m})=>{let{cache:l,requestManager:T}=e.client,y=J(null),s=J(null),d=J(null),h=J(null),g=J(null),E=J(null),b=J(null),a=J(null),i=J(null),v=J(new Map),q=J(null),k=c=>{let t=v.current.get(c);t==null||t.unmount(),v.current.delete(c)},U=()=>{let c=v.current;Array.from(c.values()).forEach(f=>{f.unmount()}),c.clear()},P=(c,t,f)=>{var Q,N,W,V,Z;let{success:A}=t,{isOffline:B,isCanceled:$}=f;e.offline&&B&&!A?(o.debug("Performing offline error callback",{data:t,details:f}),(Q=h.current)==null||Q.call(h,{response:t[1],request:c,details:f})):$?(o.debug("Performing abort callback",{data:t,details:f}),(N=d.current)==null||N.call(d,{response:t[1],request:c,details:f})):A?(o.debug("Performing success callback",{data:t,details:f}),(W=y.current)==null||W.call(y,{response:t[0],request:c,details:f})):(o.debug("Performing error callback",{data:t,details:f}),(V=s.current)==null||V.call(s,{response:t[1],request:c,details:f})),(Z=g.current)==null||Z.call(g,{response:t,request:c,details:f})},S=c=>({loading:t})=>{let f=!t&&!n.hasRunningRequests(c);(t||f)&&R.setLoading(t,!1)},O=(c,t)=>{var f;(f=a.current)==null||f.call(a,c,t)},F=(c,t)=>{var f;(f=i.current)==null||f.call(i,c,t)},u=c=>t=>{var f;(f=E.current)==null||f.call(E,{request:c,details:t})},p=c=>t=>{var f;(f=b.current)==null||f.call(b,{request:c,details:t})},x=c=>(t,f)=>{P(c,t,f)},L=({requestId:c})=>{k(c)},M=()=>{var c;(c=q.current)==null||c.unmount(),q.current=null},X=c=>{let t=T.events.onLoading(c.queueKey,S(c.queueKey)),f=l.events.onData(c.cacheKey,m),A=()=>{t(),f()};return M(),q.current={unmount:A},A},K=(c,t)=>{if(!t){U();let{queueKey:V,cacheKey:Z}=c,j=T.events.onRequestStart(V,u(c)),z=T.events.onResponseStart(V,p(c)),ce=T.events.onUploadProgress(V,F),ie=T.events.onDownloadProgress(V,O),le=T.events.onResponse(Z,x(c)),ue=()=>{ie(),ce(),j(),z(),le()};return v.current.set(V,{unmount:ue}),ue}let f=T.events.onRemoveById(t,L),A=T.events.onRequestStartById(t,u(c)),B=T.events.onResponseStartById(t,p(c)),$=T.events.onResponseById(t,x(c)),Q=T.events.onUploadProgressById(t,F),N=T.events.onDownloadProgressById(t,O),W=()=>{f(),N(),Q(),A(),B(),$()};return v.current.set(t,{unmount:W}),W},re=()=>{let{abortKey:c}=e;n.getAllRunningRequest().forEach(f=>{f.request.abortKey===c&&n.delete(f.request.queueKey,f.requestId,c)})};return Be(()=>{U(),M()}),[{abort:re,onSuccess:c=>{y.current=c},onError:c=>{s.current=c},onAbort:c=>{d.current=c},onOfflineError:c=>{h.current=c},onFinished:c=>{g.current=c},onRequestStart:c=>{E.current=c},onResponseStart:c=>{b.current=c},onDownloadProgress:c=>{a.current=c},onUploadProgress:c=>{i.current=c}},{addDataListener:X,clearDataListener:M,addLifecycleListeners:K,removeLifecycleListener:k,clearLifecycleListeners:U}]};var Y={data:null,error:null,status:null,extra:null,success:null,loading:!1,retries:0,timestamp:null};import{useRef as he}from"react";import{useDidUpdate as ve,useForceUpdate as je}from"@better-hooks/lifecycle";var be=e=>{let n=Object.prototype.toString.call(e);return Array.isArray(e)?!e.length:typeof e=="object"&&e!==null&&n==="[object Object]"?!Object.keys(e).length:!1},oe=(e,n)=>{let o=Object.prototype.toString.call(e),R=Object.prototype.toString.call(n),m=typeof e,l=typeof n,T=s=>m===s&&l===s,y=s=>o===s&&R===s;return o!==R?!1:e===null&&n===null||T("number")&&Number.isNaN(e)&&Number.isNaN(n)||be(e)&&be(n)?!0:Array.isArray(e)&&Array.isArray(n)?e.length!==n.length?!1:!e.some((s,d)=>!oe(s,n[d])):T("object")&&y("[object Object]")?Object.keys(e).length!==Object.keys(n).length?!1:!Object.entries(e).some(([s,d])=>!oe(d,n[s])):e instanceof Date&&n instanceof Date?+e==+n:e===n};var pe=e=>C(r({},e),{throttle:void 0,debounce:void 0});var G=(e,n)=>r({retries:(e==null?void 0:e.retries)||0,timestamp:+new Date,isCanceled:!1,isOffline:!1},n),Te=(e,n)=>n?+new Date>+n+e:!0,Ne=(e,n,o)=>!Te(e.cacheTime,o==null?void 0:o.timestamp)&&o?o:n?C(r(r({},n),G()),{cacheTime:1e3,clearKey:e.client.cache.clearKey,garbageCollection:e.garbageCollection}):null,Se=e=>e?new Date(e):null,ye=(e,n,o)=>{let{client:R,cacheKey:m}=o,{cache:l}=R,T=l.get(m),y=Ne(o,e,T),s=n.hasRunningRequests(o.queueKey);return y?{data:y.data,error:y.error,status:y.status,success:y.success,extra:y.extra,retries:y.retries,timestamp:Se(y.timestamp),loading:s}:{data:Y.data,error:Y.error,status:Y.status,success:Y.success,extra:o.client.defaultExtra,retries:Y.retries,timestamp:Se(Y.timestamp),loading:s}};var te=({request:e,dispatcher:n,initialData:o,deepCompare:R,dependencyTracking:m,defaultCacheEmitting:l=!0})=>{let{client:T,cacheKey:y,queueKey:s,cacheTime:d,responseMapper:h}=e,{cache:g,requestManager:E}=T,b=je(),a=he(ye(o,n,e)),i=he([]),v=()=>{let u=g.get(y);return Te(d,(u==null?void 0:u.timestamp)||a.current.timestamp)},q=u=>{i.current.some(x=>u.includes(x))&&b()},k=u=>{i.current.includes(u)||i.current.push(u)};ve(()=>{a.current.loading=n.hasRunningRequests(s);let u=ye(o,n,e),p=oe(o==null?void 0:o.data,a.current.data),x=!!(a.current.data||a.current.error)&&!p,L=!x&&!!a.current.data,M=x&&!a.current.data;(L||M)&&(a.current=u)},[y,s],!0),ve(()=>{(()=>{m||Object.keys(a.current).forEach(p=>k(p))})()},[m],!0);let U=(u,p)=>typeof R=="function"?R(u,p):R?oe(u,p):!1,P=u=>{if(h){let p=h(u);return p instanceof Promise?new Promise(x=>{_(void 0,null,function*(){let L=yield p;x(r(r({},u),L))})}):r(r({},u),p)}return u},S=u=>{let p={data:u.data,error:u.error,status:u.status,success:u.success,extra:u.extra,retries:u.retries,timestamp:new Date(u.timestamp),loading:a.current.loading},x=Object.keys(p).filter(L=>{let M=L,X=a.current[M],K=p[M];return!U(X,K)});a.current=r(r({},a.current),p),q(x)},O=u=>{let p=P(u);return p instanceof Promise?(()=>_(void 0,null,function*(){let x=yield p;S(x)}))():S(p)},F={setData:(u,p=l)=>{if(p){let x=a.current;g.set(e,C(r(r({},x),G(a.current)),{success:!0,data:u}))}else a.current.data=u,q(["data"])},setError:(u,p=l)=>{if(p){let x=a.current;g.set(e,C(r(r({},x),G(a.current)),{success:!1,error:u}))}else a.current.error=u,q(["error"])},setLoading:(u,p=!0)=>{p?E.events.emitLoading(s,"",{queueKey:s,requestId:"",loading:u,isRetry:!1,isOffline:!1}):(a.current.loading=u,q(["loading"]))},setStatus:(u,p=l)=>{if(p){let x=a.current;g.set(e,C(r(r({},x),G(a.current)),{status:u}))}else a.current.status=u,q(["status"])},setSuccess:(u,p=l)=>{if(p){let x=a.current;g.set(e,C(r(r({},x),G(a.current)),{success:u}))}else a.current.success=u,q(["success"])},setExtra:(u,p=l)=>{if(p){let x=a.current;g.set(e,C(r(r({},x),G(a.current)),{extra:u}))}else a.current.extra=u,q(["extra"])},setRetries:(u,p=l)=>{if(p){let x=a.current;g.set(e,r(r({},x),G(a.current,{retries:u})))}else a.current.retries=u,q(["retries"])},setTimestamp:(u,p=l)=>{if(p){let x=a.current;g.set(e,r(r({},x),G(a.current,{timestamp:+u})))}else a.current.timestamp=u,q(["timestamp"])}};return[a.current,F,{setRenderKey:k,setCacheData:O,getStaleStatus:v}]};import{useRef as H}from"react";import{useDidMount as Je,useDidUpdate as Ve,useForceUpdate as We}from"@better-hooks/lifecycle";var ne=(e,{dependencyTracking:n})=>{let o=We(),R=H(null),m=H(null),l=H(null),T=H(null),y=H(null),s=H(null),d=H(qe),h=H([]),g=i=>{h.current.some(q=>i.includes(q))&&o()},E=i=>{h.current.includes(i)||h.current.push(i)};Ve(()=>{(()=>{n||Object.keys(d.current).forEach(v=>E(v))})()},[n],!0);let b={setData:i=>{d.current.data=i,g(["data"])},setConnected:i=>{d.current.connected=i,g(["data"])},setConnecting:i=>{d.current.connecting=i,g(["data"])},setTimestamp:i=>{d.current.timestamp=i,g(["timestamp"])}},a={onOpen:i=>{R.current=i},onClose:i=>{m.current=i},onError:i=>{l.current=i},onConnecting:i=>{T.current=i},onReconnecting:i=>{y.current=i},onReconnectingStop:i=>{s.current=i}};return Je(()=>{let i=e.events.onError(S=>{var O;(O=l.current)==null||O.call(l,S)}),v=e.events.onConnecting(()=>{var S;b.setConnecting(!0),(S=T.current)==null||S.call(T)}),q=e.events.onOpen(()=>{var S;b.setConnected(!0),(S=R.current)==null||S.call(R)}),k=e.events.onClose(()=>{var S;b.setConnected(!1),(S=m.current)==null||S.call(m)}),U=e.events.onReconnecting(S=>{var O;(O=y.current)==null||O.call(y,S)}),P=e.events.onReconnectingStop(S=>{var O;(O=s.current)==null||O.call(s,S)});return()=>{i(),v(),q(),k(),U(),P()}}),[d.current,b,a,{setRenderKey:E}]};var qe={data:null,connected:!1,connecting:!1,timestamp:null};import Ge,{useContext as $e,useMemo as ze,useState as He}from"react";import{jsx as Xe}from"react/jsx-runtime";var Ue=Ge.createContext([{},()=>null]),fn=({children:e,config:n})=>{let[o,R]=He(n||{}),m=ze(()=>[o,R],[o]);return Xe(Ue.Provider,{value:m,children:e})},w=()=>$e(Ue);var In=(e,n=I)=>{let[o]=w(),{dependencies:R=I.dependencies,disabled:m=I.disabled,dependencyTracking:l=I.dependencyTracking,revalidateOnMount:T=I.revalidateOnMount,initialData:y=I.initialData,refresh:s=I.refresh,refreshTime:d=I.refreshTime,refreshBlurred:h=I.refreshBlurred,refreshOnBlur:g=I.refreshOnBlur,refreshOnFocus:E=I.refreshOnFocus,refreshOnReconnect:b=I.refreshOnReconnect,bounce:a=I.bounce,bounceType:i=I.bounceType,bounceTime:v=I.bounceTime,bounceTimeout:q=I.bounceTime,deepCompare:k=I.deepCompare}=r(r(r({},I),o.useFetchConfig),n),U=JSON.stringify(e.toJSON()),P=Oe({delay:v}),S=_e({interval:v,timeout:q}),O=Oe({delay:d}),{cacheKey:F,queueKey:u,client:p}=e,{cache:x,fetchDispatcher:L,appManager:M,loggerManager:X}=p,K=Ye(X.init("useFetch")).current,re=i==="throttle"?S:P,c=i==="throttle"?S.throttle:P.debounce,[t,f,{setRenderKey:A,setCacheData:B,getStaleStatus:$}]=te({logger:K,request:e,dispatcher:L,initialData:y,deepCompare:k,dependencyTracking:l}),[Q,N]=ee({logger:K,actions:f,request:e,dispatcher:L,setCacheData:B}),{addDataListener:W,addLifecycleListeners:V,clearDataListener:Z}=N,j=()=>{m?K.debug("Cannot add to fetch queue",{disabled:m}):(K.debug("Fetching data"),L.add(e))};function z(){!s||O.debounce(()=>{let D=!M.isFocused,se=L.hasRunningRequests(e.queueKey),me=L.getIsActiveQueue(e.queueKey),ae=se||me;(D&&h&&!ae||!D&&!ae)&&(j(),K.debug("Performing refresh request")),z()})}let ce=D=>{D&&D instanceof Ce?x.revalidate(et(D)):D&&!(D instanceof Ce)&&x.revalidate(D)},ie=D=>{D&&Array.isArray(D)?D.forEach(ce):D&&!Array.isArray(D)?ce(D):(j(),z())},le=()=>{let D=$(),se=L.getIsActiveQueue(u);(T||D&&!se)&&j()},ue=()=>{a?(K.debug(`Bounce request with ${i}`,{queueKey:u,request:e}),c(()=>j())):j()};return de(()=>{W(e),V(e);let D=M.events.onFocus(()=>{E&&(j(),z())}),se=M.events.onBlur(()=>{g&&(j(),z())}),me=M.events.onOnline(()=>{b&&(j(),z())}),ae=x.events.onRevalidate(F,j);return()=>{Z(),D(),se(),me(),ae()}},[U],!0),Ze(le),de(ue,[U,m,...R]),de(z,[U,...R,m,s,d],!0),C(r(r({get data(){return A("data"),t.data},get error(){return A("error"),t.error},get loading(){return A("loading"),t.loading},get status(){return A("status"),t.status},get success(){return A("success"),t.success},get extra(){return A("extra"),t.extra},get retries(){return A("retries"),t.retries},get timestamp(){return A("timestamp"),t.timestamp},bounce:pe(re)},f),Q),{revalidate:ie})};var Mn=(e,n)=>{if(n){let o=Date.now()-+n;return o<e?e-o:e}return e};import{DateInterval as tt}from"@hyper-fetch/core";var I={dependencies:[],disabled:!1,dependencyTracking:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:tt.hour,refreshBlurred:!0,refreshOnBlur:!1,refreshOnFocus:!1,refreshOnReconnect:!1,bounce:!1,bounceType:"debounce",bounceTime:400,deepCompare:!0};import{useMemo as nt,useRef as ke}from"react";import{Request as De,getRequestKey as rt,sendRequest as st}from"@hyper-fetch/core";import{useDidMount as ot}from"@better-hooks/lifecycle";import{useDebounce as ct,useThrottle as ut}from"@better-hooks/performance";var lr=(e,n=fe)=>{let[o]=w(),R=nt(()=>r(r(r({},fe),o.useSubmitConfig),n),[JSON.stringify(o.useSubmitConfig),JSON.stringify(n)]),{disabled:m,dependencyTracking:l,initialData:T,bounce:y,bounceType:s,bounceTime:d,deepCompare:h}=R,{client:g}=e,{cache:E,submitDispatcher:b,loggerManager:a}=g,i=ke(a.init("useSubmit")).current,v=ct({delay:d}),q=ut({interval:d,timeout:"bounceTimeout"in R?R.bounceTimeout:d}),k=ke(()=>null),U=s==="throttle"?q:v,P=s==="throttle"?q.throttle:v.debounce,[S,O,{setRenderKey:F,setCacheData:u}]=te({logger:i,request:e,dispatcher:b,initialData:T,deepCompare:h,dependencyTracking:l}),[p,x]=ee({logger:i,actions:O,request:e,dispatcher:b,setCacheData:u}),{addDataListener:L,addLifecycleListeners:M}=x,X=t=>{let f=e.clone(t);if(m)return i.warning("Cannot submit request",{disabled:m,submitOptions:t}),Promise.resolve({data:null,error:new Error("Cannot submit request. Option 'disabled' is enabled"),status:null,extra:e.client.defaultExtra});let A=()=>(L(f),st(f,C(r({dispatcherType:"submit"},t),{onSettle:(B,$)=>{var Q;M(f,B),(Q=t==null?void 0:t.onSettle)==null||Q.call(t,B,$)}})));return new Promise(B=>{(()=>_(void 0,null,function*(){if(i.debug("Submitting request",{disabled:m,submitOptions:t}),y){let Q=k.current;k.current=N=>{Q(N),B(N)},P(()=>_(void 0,null,function*(){let N=k.current;k.current=()=>null;let W=yield A();N(W)}))}else{let Q=yield A();B(Q)}}))()})},K=t=>{t&&t instanceof De?E.revalidate(rt(t)):t&&!(t instanceof De)&&E.revalidate(t)},re=t=>{!t||(t&&Array.isArray(t)?t.forEach(K):t&&!Array.isArray(t)&&K(t))},c={onSubmitSuccess:p.onSuccess,onSubmitError:p.onError,onSubmitFinished:p.onFinished,onSubmitRequestStart:p.onRequestStart,onSubmitResponseStart:p.onResponseStart,onSubmitDownloadProgress:p.onDownloadProgress,onSubmitUploadProgress:p.onUploadProgress,onSubmitOfflineError:p.onOfflineError,onSubmitAbort:p.onAbort};return ot(()=>{L(e)}),C(r(r({submit:X,get data(){return F("data"),S.data},get error(){return F("error"),S.error},get submitting(){return F("loading"),S.loading},get status(){return F("status"),S.status},get success(){return F("success"),S.success},get extra(){return F("extra"),S.extra},get retries(){return F("retries"),S.retries},get timestamp(){return F("timestamp"),S.timestamp},abort:p.abort},O),c),{bounce:pe(U),revalidate:re})};var fe={disabled:!1,dependencyTracking:!0,initialData:null,bounce:!1,bounceType:"debounce",bounceTime:400,deepCompare:!0};import{useState as Pe,useRef as at}from"react";import{getRequestDispatcher as pt}from"@hyper-fetch/core";import{useDidMount as it,useDidUpdate as lt}from"@better-hooks/lifecycle";var kr=(e,n=ge)=>{let[o]=w(),{queueType:R="auto"}=r(r(r({},ge),o.useQueueConfig),n),{abortKey:m,queueKey:l,client:T}=e,{requestManager:y}=T,[s]=pt(e,R),d=at(null),[h,g]=Pe(!1),[E,b]=Pe([]),a=U=>U.map(P=>C(r({},P),{stopRequest:()=>s.stopRequest(l,P.requestId),startRequest:()=>s.startRequest(l,P.requestId),deleteRequest:()=>s.delete(l,P.requestId,m)})),i=(U,P)=>{b(S=>S.map(O=>O.requestId===U?r(r({},O),P):O))},v=()=>{let U=s.getQueue(l);g(U.stopped),b(a(U.requests))},q=U=>{g(U.stopped),b(a(U.requests))},k=()=>{var u;let U=s.events.onQueueChange(l,q),P=s.events.onQueueStatus(l,q),S=y.events.onDownloadProgress(l,(p,{requestId:x})=>{i(x,{downloading:p})}),O=y.events.onUploadProgress(l,(p,{requestId:x})=>{i(x,{uploading:p})}),F=()=>{P(),U(),S(),O()};return(u=d.current)==null||u.call(d),d.current=F,F};return it(v),lt(k,[h,E,b,g],!0),{stopped:h,requests:E,stop:()=>s.stop(l),pause:()=>s.pause(l),start:()=>s.start(l)}};var ge={queueType:"auto"};import{useRef as mt}from"react";import{getRequestDispatcher as Tt,Request as yt,getRequestKey as dt}from"@hyper-fetch/core";var Wr=(e,n=Re)=>{let{cacheKey:o,client:R}=e,{cache:m,loggerManager:l}=R,T=mt(l.init("useCache")).current,[y]=Tt(e),[s]=w(),{dependencyTracking:d,initialData:h,deepCompare:g}=r(r(r({},Re),s.useCacheConfig),n),[E,b,{setRenderKey:a,setCacheData:i}]=te({logger:T,request:e,dispatcher:y,initialData:h,deepCompare:g,dependencyTracking:d}),[v]=ee({logger:T,actions:b,request:e,dispatcher:y,setCacheData:i}),q=k=>{k instanceof yt?m.revalidate(dt(k,!0)):k?m.revalidate(k):m.revalidate(o)};return C(r({get data(){return a("data"),E.data},get error(){return a("error"),E.error},get loading(){return a("loading"),E.loading},get status(){return a("status"),E.status},get success(){return a("success"),E.success},get extra(){return a("extra"),E.extra},get retries(){return a("retries"),E.retries},get timestamp(){return a("timestamp"),E.timestamp},onCacheError:v.onError,onCacheSuccess:v.onSuccess,onCacheChange:v.onFinished},b),{revalidate:q})};var Re={dependencyTracking:!0,initialData:null,deepCompare:!0};import{useRef as Ae}from"react";import{useDidUpdate as ft,useWillUnmount as gt}from"@better-hooks/lifecycle";var rs=(e,n)=>{let[o]=w(),{dependencyTracking:R}=r(r({},o.useListener),n),m=Ae(null),l=Ae(null),[T,y,s,{setRenderKey:d}]=ne(e.socket,{dependencyTracking:R}),h=()=>{var b;(b=l.current)==null||b[0]()},g=()=>{h(),l.current=e.listen((b,a)=>{var i;(i=m.current)==null||i.call(m,b,a),y.setData(b),y.setTimestamp(+new Date)})};ft(()=>{g()},[e],!0),gt(()=>{h()});let E={onEvent:b=>{m.current=b}};return C(r(r(r({get data(){return d("data"),T.data},get connected(){return d("connected"),T.connected},get connecting(){return d("connecting"),T.connecting},get timestamp(){return d("timestamp"),T.timestamp}},y),s),E),{listen:g})};import{useRef as Rt}from"react";var ms=(e,n)=>{let[o]=w(),{dependencyTracking:R}=r(r({},o.useEmitter),n),m=Rt(null),[l,T,y,{setRenderKey:s}]=ne(e.socket,{dependencyTracking:R}),d={onEvent:g=>{m.current=g}},h=(...g)=>{var E;return T.setTimestamp(+new Date),(E=m.current)==null||E.call(m,e),e.emit(...g)};return C(r(r(r({get connected(){return s("connected"),l.connected},get connecting(){return s("connecting"),l.connecting},get timestamp(){return s("timestamp"),l.timestamp}},T),y),d),{emit:h,reconnect:e.socket.reconnect})};import{useRef as xt}from"react";import{useDidUpdate as Et}from"@better-hooks/lifecycle";var Ss=(e,n)=>{let[o]=w(),{dependencyTracking:R=!1,filter:m}=r(r({},o.useEventMessages),n),l=xt(null),[T,y,s,{setRenderKey:d}]=ne(e,{dependencyTracking:R});Et(()=>e.events.onListenerEvent((E,b)=>{var v;(m?(typeof m=="function"?()=>m(E,b):()=>m.includes(E.name))():!1)||((v=l.current)==null||v.call(l,E,b),y.setData(E),y.setTimestamp(+new Date))}),[e,m],!0);let h={onEvent:g=>{l.current=g}};return r(r(r({get data(){return d("data"),T.data},get connected(){return d("connected"),T.connected},get connecting(){return d("connecting"),T.connecting},get timestamp(){return d("timestamp"),T.timestamp}},y),s),h)};import{useState as Ie}from"react";import{useDidMount as bt}from"@better-hooks/lifecycle";var Ds=e=>{let[n,o]=Ie(e.appManager.isOnline),[R,m]=Ie(e.appManager.isFocused),l=()=>{let s=e.appManager.events.onOnline(()=>o(!0)),d=e.appManager.events.onOffline(()=>o(!1)),h=e.appManager.events.onFocus(()=>m(!0)),g=e.appManager.events.onBlur(()=>m(!1));return()=>{s(),d(),h(),g()}},T=s=>{e.appManager.setOnline(s)},y=s=>{e.appManager.setFocused(s)};return bt(l),{isOnline:n,isFocused:R,setOnline:T,setFocused:y}};export{fn as ConfigProvider,pe as getBounceData,G as getDetailsState,ye as getInitialState,Mn as getRefreshTime,Se as getTimestamp,Ne as getValidCacheData,qe as initialSocketState,Y as initialState,be as isEmpty,oe as isEqual,Te as isStaleCacheData,Ds as useAppManager,Wr as useCache,Re as useCacheDefaultOptions,w as useConfigProvider,ms as useEmitter,Ss as useEventMessages,In as useFetch,I as useFetchDefaultOptions,rs as useListener,kr as useQueue,ge as useQueueDefaultOptions,ee as useRequestEvents,ne as useSocketState,lr as useSubmit,fe as useSubmitDefaultOptions,te as useTrackedState};
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/hooks/use-fetch/use-fetch.hooks.ts
|
|
42
|
+
import { useRef as useRef4 } from "react";
|
|
43
|
+
import { useDidUpdate as useDidUpdate3, useDidMount as useDidMount2 } from "@better-hooks/lifecycle";
|
|
44
|
+
import { useDebounce, useThrottle } from "@better-hooks/performance";
|
|
45
|
+
import { Request, getRequestKey } from "@hyper-fetch/core";
|
|
46
|
+
|
|
47
|
+
// src/helpers/use-request-events/use-request-events.hooks.ts
|
|
48
|
+
import { useRef } from "react";
|
|
49
|
+
import { useWillUnmount } from "@better-hooks/lifecycle";
|
|
50
|
+
var useRequestEvents = ({
|
|
51
|
+
request,
|
|
52
|
+
dispatcher,
|
|
53
|
+
logger,
|
|
54
|
+
actions,
|
|
55
|
+
setCacheData
|
|
56
|
+
}) => {
|
|
57
|
+
const { cache, requestManager } = request.client;
|
|
58
|
+
const onSuccessCallback = useRef(null);
|
|
59
|
+
const onErrorCallback = useRef(null);
|
|
60
|
+
const onAbortCallback = useRef(null);
|
|
61
|
+
const onOfflineErrorCallback = useRef(null);
|
|
62
|
+
const onFinishedCallback = useRef(null);
|
|
63
|
+
const onRequestStartCallback = useRef(null);
|
|
64
|
+
const onResponseStartCallback = useRef(null);
|
|
65
|
+
const onDownloadProgressCallback = useRef(null);
|
|
66
|
+
const onUploadProgressCallback = useRef(null);
|
|
67
|
+
const lifecycleEvents = useRef(/* @__PURE__ */ new Map());
|
|
68
|
+
const dataEvents = useRef(null);
|
|
69
|
+
const removeLifecycleListener = (requestId) => {
|
|
70
|
+
const event = lifecycleEvents.current.get(requestId);
|
|
71
|
+
event == null ? void 0 : event.unmount();
|
|
72
|
+
lifecycleEvents.current.delete(requestId);
|
|
73
|
+
};
|
|
74
|
+
const clearLifecycleListeners = () => {
|
|
75
|
+
const events = lifecycleEvents.current;
|
|
76
|
+
const listeners = Array.from(events.values());
|
|
77
|
+
listeners.forEach((value) => {
|
|
78
|
+
value.unmount();
|
|
79
|
+
});
|
|
80
|
+
events.clear();
|
|
81
|
+
};
|
|
82
|
+
const handleResponseCallbacks = (cmd, response, details) => {
|
|
83
|
+
var _a, _b, _c, _d, _e;
|
|
84
|
+
const { data, error, success } = response;
|
|
85
|
+
const { isOffline, isCanceled } = details;
|
|
86
|
+
if (request.offline && isOffline && !success) {
|
|
87
|
+
logger.debug("Performing offline error callback", { data, details });
|
|
88
|
+
(_a = onOfflineErrorCallback.current) == null ? void 0 : _a.call(onOfflineErrorCallback, { response: error, request: cmd, details });
|
|
89
|
+
} else if (isCanceled) {
|
|
90
|
+
logger.debug("Performing abort callback", { data, details });
|
|
91
|
+
(_b = onAbortCallback.current) == null ? void 0 : _b.call(onAbortCallback, { response: error, request: cmd, details });
|
|
92
|
+
} else if (success) {
|
|
93
|
+
logger.debug("Performing success callback", { data, details });
|
|
94
|
+
(_c = onSuccessCallback.current) == null ? void 0 : _c.call(onSuccessCallback, { response: data, request: cmd, details });
|
|
95
|
+
} else {
|
|
96
|
+
logger.debug("Performing error callback", { data, details });
|
|
97
|
+
(_d = onErrorCallback.current) == null ? void 0 : _d.call(onErrorCallback, { response: error, request: cmd, details });
|
|
98
|
+
}
|
|
99
|
+
(_e = onFinishedCallback.current) == null ? void 0 : _e.call(onFinishedCallback, { response, request: cmd, details });
|
|
100
|
+
};
|
|
101
|
+
const handleGetLoadingEvent = (queueKey) => {
|
|
102
|
+
return ({ loading }) => {
|
|
103
|
+
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(queueKey);
|
|
104
|
+
if (loading || canDisableLoading) {
|
|
105
|
+
actions.setLoading(loading, false);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
const handleDownloadProgress = (progress, details) => {
|
|
110
|
+
var _a;
|
|
111
|
+
(_a = onDownloadProgressCallback.current) == null ? void 0 : _a.call(onDownloadProgressCallback, progress, details);
|
|
112
|
+
};
|
|
113
|
+
const handleUploadProgress = (progress, details) => {
|
|
114
|
+
var _a;
|
|
115
|
+
(_a = onUploadProgressCallback.current) == null ? void 0 : _a.call(onUploadProgressCallback, progress, details);
|
|
116
|
+
};
|
|
117
|
+
const handleRequestStart = (cmd) => {
|
|
118
|
+
return (details) => {
|
|
119
|
+
var _a;
|
|
120
|
+
(_a = onRequestStartCallback.current) == null ? void 0 : _a.call(onRequestStartCallback, { request: cmd, details });
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
const handleResponseStart = (cmd) => {
|
|
124
|
+
return (details) => {
|
|
125
|
+
var _a;
|
|
126
|
+
(_a = onResponseStartCallback.current) == null ? void 0 : _a.call(onResponseStartCallback, { request: cmd, details });
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
const handleResponse = (req) => {
|
|
130
|
+
return (data, details) => {
|
|
131
|
+
handleResponseCallbacks(req, data, details);
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
const handleRemove = ({ requestId }) => {
|
|
135
|
+
removeLifecycleListener(requestId);
|
|
136
|
+
};
|
|
137
|
+
const clearDataListener = () => {
|
|
138
|
+
var _a;
|
|
139
|
+
(_a = dataEvents.current) == null ? void 0 : _a.unmount();
|
|
140
|
+
dataEvents.current = null;
|
|
141
|
+
};
|
|
142
|
+
const addDataListener = (req) => {
|
|
143
|
+
const loadingUnmount = requestManager.events.onLoading(req.queueKey, handleGetLoadingEvent(req.queueKey));
|
|
144
|
+
const getResponseUnmount = cache.events.onData(
|
|
145
|
+
req.cacheKey,
|
|
146
|
+
setCacheData
|
|
147
|
+
);
|
|
148
|
+
const unmount = () => {
|
|
149
|
+
loadingUnmount();
|
|
150
|
+
getResponseUnmount();
|
|
151
|
+
};
|
|
152
|
+
clearDataListener();
|
|
153
|
+
dataEvents.current = { unmount };
|
|
154
|
+
return unmount;
|
|
155
|
+
};
|
|
156
|
+
const addLifecycleListeners = (req, requestId) => {
|
|
157
|
+
if (!requestId) {
|
|
158
|
+
clearLifecycleListeners();
|
|
159
|
+
const { queueKey, cacheKey } = req;
|
|
160
|
+
const requestStartUnmount2 = requestManager.events.onRequestStart(queueKey, handleRequestStart(req));
|
|
161
|
+
const responseStartUnmount2 = requestManager.events.onResponseStart(queueKey, handleResponseStart(req));
|
|
162
|
+
const uploadUnmount2 = requestManager.events.onUploadProgress(queueKey, handleUploadProgress);
|
|
163
|
+
const downloadUnmount2 = requestManager.events.onDownloadProgress(queueKey, handleDownloadProgress);
|
|
164
|
+
const responseUnmount2 = requestManager.events.onResponse(cacheKey, handleResponse(req));
|
|
165
|
+
const unmount2 = () => {
|
|
166
|
+
downloadUnmount2();
|
|
167
|
+
uploadUnmount2();
|
|
168
|
+
requestStartUnmount2();
|
|
169
|
+
responseStartUnmount2();
|
|
170
|
+
responseUnmount2();
|
|
171
|
+
};
|
|
172
|
+
lifecycleEvents.current.set(queueKey, { unmount: unmount2 });
|
|
173
|
+
return unmount2;
|
|
174
|
+
}
|
|
175
|
+
const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
|
|
176
|
+
const requestStartUnmount = requestManager.events.onRequestStartById(requestId, handleRequestStart(req));
|
|
177
|
+
const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart(req));
|
|
178
|
+
const responseUnmount = requestManager.events.onResponseById(requestId, handleResponse(req));
|
|
179
|
+
const uploadUnmount = requestManager.events.onUploadProgressById(requestId, handleUploadProgress);
|
|
180
|
+
const downloadUnmount = requestManager.events.onDownloadProgressById(requestId, handleDownloadProgress);
|
|
181
|
+
const unmount = () => {
|
|
182
|
+
requestRemove();
|
|
183
|
+
downloadUnmount();
|
|
184
|
+
uploadUnmount();
|
|
185
|
+
requestStartUnmount();
|
|
186
|
+
responseStartUnmount();
|
|
187
|
+
responseUnmount();
|
|
188
|
+
};
|
|
189
|
+
lifecycleEvents.current.set(requestId, { unmount });
|
|
190
|
+
return unmount;
|
|
191
|
+
};
|
|
192
|
+
const abort = () => {
|
|
193
|
+
const { abortKey } = request;
|
|
194
|
+
const requests = dispatcher.getAllRunningRequest();
|
|
195
|
+
requests.forEach((requestData) => {
|
|
196
|
+
if (requestData.request.abortKey === abortKey) {
|
|
197
|
+
dispatcher.delete(requestData.request.queueKey, requestData.requestId, abortKey);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
useWillUnmount(() => {
|
|
202
|
+
clearLifecycleListeners();
|
|
203
|
+
clearDataListener();
|
|
204
|
+
});
|
|
205
|
+
return [
|
|
206
|
+
{
|
|
207
|
+
abort,
|
|
208
|
+
onSuccess: (callback) => {
|
|
209
|
+
onSuccessCallback.current = callback;
|
|
210
|
+
},
|
|
211
|
+
onError: (callback) => {
|
|
212
|
+
onErrorCallback.current = callback;
|
|
213
|
+
},
|
|
214
|
+
onAbort: (callback) => {
|
|
215
|
+
onAbortCallback.current = callback;
|
|
216
|
+
},
|
|
217
|
+
onOfflineError: (callback) => {
|
|
218
|
+
onOfflineErrorCallback.current = callback;
|
|
219
|
+
},
|
|
220
|
+
onFinished: (callback) => {
|
|
221
|
+
onFinishedCallback.current = callback;
|
|
222
|
+
},
|
|
223
|
+
onRequestStart: (callback) => {
|
|
224
|
+
onRequestStartCallback.current = callback;
|
|
225
|
+
},
|
|
226
|
+
onResponseStart: (callback) => {
|
|
227
|
+
onResponseStartCallback.current = callback;
|
|
228
|
+
},
|
|
229
|
+
onDownloadProgress: (callback) => {
|
|
230
|
+
onDownloadProgressCallback.current = callback;
|
|
231
|
+
},
|
|
232
|
+
onUploadProgress: (callback) => {
|
|
233
|
+
onUploadProgressCallback.current = callback;
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
addDataListener,
|
|
238
|
+
clearDataListener,
|
|
239
|
+
addLifecycleListeners,
|
|
240
|
+
removeLifecycleListener,
|
|
241
|
+
clearLifecycleListeners
|
|
242
|
+
}
|
|
243
|
+
];
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/helpers/use-tracked-state/use-tracked-state.constants.ts
|
|
247
|
+
var initialState = {
|
|
248
|
+
data: null,
|
|
249
|
+
error: null,
|
|
250
|
+
status: null,
|
|
251
|
+
extra: null,
|
|
252
|
+
success: null,
|
|
253
|
+
loading: false,
|
|
254
|
+
retries: 0,
|
|
255
|
+
timestamp: null
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// src/helpers/use-tracked-state/use-tracked-state.hooks.ts
|
|
259
|
+
import { useRef as useRef2 } from "react";
|
|
260
|
+
import { useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
|
|
261
|
+
|
|
262
|
+
// src/utils/deep-equal.utils.ts
|
|
263
|
+
var isEmpty = (value) => {
|
|
264
|
+
const valueType = Object.prototype.toString.call(value);
|
|
265
|
+
if (Array.isArray(value))
|
|
266
|
+
return !value.length;
|
|
267
|
+
if (typeof value === "object" && value !== null && valueType === "[object Object]")
|
|
268
|
+
return !Object.keys(value).length;
|
|
269
|
+
return false;
|
|
270
|
+
};
|
|
271
|
+
var isEqual = (firstValue, secondValue) => {
|
|
272
|
+
const firstValueType = Object.prototype.toString.call(firstValue);
|
|
273
|
+
const secondValueType = Object.prototype.toString.call(secondValue);
|
|
274
|
+
const firstType = typeof firstValue;
|
|
275
|
+
const secondType = typeof secondValue;
|
|
276
|
+
const isType = (type) => firstType === type && secondType === type;
|
|
277
|
+
const isTypeValue = (type) => firstValueType === type && secondValueType === type;
|
|
278
|
+
if (firstValueType !== secondValueType)
|
|
279
|
+
return false;
|
|
280
|
+
if (firstValue === null && secondValue === null)
|
|
281
|
+
return true;
|
|
282
|
+
if (isType("number") && Number.isNaN(firstValue) && Number.isNaN(secondValue))
|
|
283
|
+
return true;
|
|
284
|
+
if (isEmpty(firstValue) && isEmpty(secondValue))
|
|
285
|
+
return true;
|
|
286
|
+
if (Array.isArray(firstValue) && Array.isArray(secondValue)) {
|
|
287
|
+
if (firstValue.length !== secondValue.length)
|
|
288
|
+
return false;
|
|
289
|
+
return !firstValue.some((element, i) => !isEqual(element, secondValue[i]));
|
|
290
|
+
}
|
|
291
|
+
if (isType("object") && isTypeValue("[object Object]")) {
|
|
292
|
+
if (Object.keys(firstValue).length !== Object.keys(secondValue).length)
|
|
293
|
+
return false;
|
|
294
|
+
return !Object.entries(firstValue).some(
|
|
295
|
+
([key, value]) => !isEqual(value, secondValue[key])
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
if (firstValue instanceof Date && secondValue instanceof Date) {
|
|
299
|
+
return +firstValue === +secondValue;
|
|
300
|
+
}
|
|
301
|
+
return firstValue === secondValue;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// src/utils/bounce.utils.ts
|
|
305
|
+
var getBounceData = (bounceData) => {
|
|
306
|
+
return __spreadValues(__spreadValues({}, bounceData), { throttle: void 0, debounce: void 0 });
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
// src/helpers/use-tracked-state/use-tracked-state.utils.ts
|
|
310
|
+
var getDetailsState = (state, details) => {
|
|
311
|
+
return __spreadValues({
|
|
312
|
+
retries: (state == null ? void 0 : state.retries) || 0,
|
|
313
|
+
timestamp: +new Date(),
|
|
314
|
+
isCanceled: false,
|
|
315
|
+
isOffline: false
|
|
316
|
+
}, details);
|
|
317
|
+
};
|
|
318
|
+
var isStaleCacheData = (cacheTime, cacheTimestamp) => {
|
|
319
|
+
if (!cacheTimestamp)
|
|
320
|
+
return true;
|
|
321
|
+
return +new Date() > +cacheTimestamp + cacheTime;
|
|
322
|
+
};
|
|
323
|
+
var getValidCacheData = (request, initialData, cacheData) => {
|
|
324
|
+
const isStale = isStaleCacheData(request.cacheTime, cacheData == null ? void 0 : cacheData.timestamp);
|
|
325
|
+
if (!isStale && cacheData) {
|
|
326
|
+
return cacheData;
|
|
327
|
+
}
|
|
328
|
+
if (initialData) {
|
|
329
|
+
return __spreadProps(__spreadValues(__spreadValues({}, initialData), getDetailsState()), {
|
|
330
|
+
cacheTime: 1e3,
|
|
331
|
+
clearKey: request.client.cache.clearKey,
|
|
332
|
+
garbageCollection: request.garbageCollection
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return null;
|
|
336
|
+
};
|
|
337
|
+
var getTimestamp = (timestamp) => {
|
|
338
|
+
return timestamp ? new Date(timestamp) : null;
|
|
339
|
+
};
|
|
340
|
+
var getInitialState = (initialData, dispatcher, request) => {
|
|
341
|
+
const { client, cacheKey } = request;
|
|
342
|
+
const { cache } = client;
|
|
343
|
+
const cacheData = cache.get(cacheKey);
|
|
344
|
+
const cacheState = getValidCacheData(request, initialData, cacheData);
|
|
345
|
+
const initialLoading = dispatcher.hasRunningRequests(request.queueKey);
|
|
346
|
+
if (cacheState) {
|
|
347
|
+
return {
|
|
348
|
+
data: cacheState.data,
|
|
349
|
+
error: cacheState.error,
|
|
350
|
+
status: cacheState.status,
|
|
351
|
+
success: cacheState.success,
|
|
352
|
+
extra: cacheState.extra,
|
|
353
|
+
retries: cacheState.retries,
|
|
354
|
+
timestamp: getTimestamp(cacheState.timestamp),
|
|
355
|
+
loading: initialLoading
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
data: initialState.data,
|
|
360
|
+
error: initialState.error,
|
|
361
|
+
status: initialState.status,
|
|
362
|
+
success: initialState.success,
|
|
363
|
+
extra: request.client.defaultExtra,
|
|
364
|
+
retries: initialState.retries,
|
|
365
|
+
timestamp: getTimestamp(initialState.timestamp),
|
|
366
|
+
loading: initialLoading
|
|
367
|
+
};
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
// src/helpers/use-tracked-state/use-tracked-state.hooks.ts
|
|
371
|
+
var useTrackedState = ({
|
|
372
|
+
request,
|
|
373
|
+
dispatcher,
|
|
374
|
+
initialData,
|
|
375
|
+
deepCompare,
|
|
376
|
+
dependencyTracking,
|
|
377
|
+
defaultCacheEmitting = true
|
|
378
|
+
}) => {
|
|
379
|
+
const { client, cacheKey, queueKey, cacheTime, responseMapper } = request;
|
|
380
|
+
const { cache, requestManager } = client;
|
|
381
|
+
const forceUpdate = useForceUpdate();
|
|
382
|
+
const state = useRef2(getInitialState(initialData, dispatcher, request));
|
|
383
|
+
const renderKeys = useRef2([]);
|
|
384
|
+
const getStaleStatus = () => {
|
|
385
|
+
const cacheData = cache.get(cacheKey);
|
|
386
|
+
return isStaleCacheData(cacheTime, (cacheData == null ? void 0 : cacheData.timestamp) || state.current.timestamp);
|
|
387
|
+
};
|
|
388
|
+
const renderKeyTrigger = (keys) => {
|
|
389
|
+
const shouldRerender = renderKeys.current.some((renderKey) => keys.includes(renderKey));
|
|
390
|
+
if (shouldRerender)
|
|
391
|
+
forceUpdate();
|
|
392
|
+
};
|
|
393
|
+
const setRenderKey = (renderKey) => {
|
|
394
|
+
if (!renderKeys.current.includes(renderKey)) {
|
|
395
|
+
renderKeys.current.push(renderKey);
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
useDidUpdate(
|
|
399
|
+
() => {
|
|
400
|
+
state.current.loading = dispatcher.hasRunningRequests(queueKey);
|
|
401
|
+
const newState = getInitialState(initialData, dispatcher, request);
|
|
402
|
+
const hasInitialState = isEqual(initialData == null ? void 0 : initialData.data, state.current.data);
|
|
403
|
+
const hasState = !!(state.current.data || state.current.error) && !hasInitialState;
|
|
404
|
+
const shouldLoadInitialCache = !hasState && !!state.current.data;
|
|
405
|
+
const shouldRemovePreviousData = hasState && !state.current.data;
|
|
406
|
+
if (shouldLoadInitialCache || shouldRemovePreviousData) {
|
|
407
|
+
state.current = newState;
|
|
408
|
+
}
|
|
409
|
+
},
|
|
410
|
+
[cacheKey, queueKey],
|
|
411
|
+
true
|
|
412
|
+
);
|
|
413
|
+
useDidUpdate(
|
|
414
|
+
() => {
|
|
415
|
+
const handleDependencyTracking = () => {
|
|
416
|
+
if (!dependencyTracking) {
|
|
417
|
+
Object.keys(state.current).forEach((key) => setRenderKey(key));
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
handleDependencyTracking();
|
|
421
|
+
},
|
|
422
|
+
[dependencyTracking],
|
|
423
|
+
true
|
|
424
|
+
);
|
|
425
|
+
const handleCompare = (firstValue, secondValue) => {
|
|
426
|
+
if (typeof deepCompare === "function") {
|
|
427
|
+
return deepCompare(firstValue, secondValue);
|
|
428
|
+
}
|
|
429
|
+
if (deepCompare) {
|
|
430
|
+
return isEqual(firstValue, secondValue);
|
|
431
|
+
}
|
|
432
|
+
return false;
|
|
433
|
+
};
|
|
434
|
+
const mapResponseData = (data) => {
|
|
435
|
+
if (responseMapper) {
|
|
436
|
+
const response = responseMapper(data);
|
|
437
|
+
if (response instanceof Promise) {
|
|
438
|
+
return new Promise((resolve) => {
|
|
439
|
+
(() => __async(void 0, null, function* () {
|
|
440
|
+
const newResponse = yield response;
|
|
441
|
+
resolve(__spreadValues(__spreadValues({}, data), newResponse));
|
|
442
|
+
}))();
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
return __spreadValues(__spreadValues({}, data), response);
|
|
446
|
+
}
|
|
447
|
+
return data;
|
|
448
|
+
};
|
|
449
|
+
const handleCacheData = (cacheData) => {
|
|
450
|
+
const newStateValues = {
|
|
451
|
+
data: cacheData.data,
|
|
452
|
+
error: cacheData.error,
|
|
453
|
+
status: cacheData.status,
|
|
454
|
+
success: cacheData.success,
|
|
455
|
+
extra: cacheData.extra,
|
|
456
|
+
retries: cacheData.retries,
|
|
457
|
+
timestamp: new Date(cacheData.timestamp),
|
|
458
|
+
loading: state.current.loading
|
|
459
|
+
};
|
|
460
|
+
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
461
|
+
const keyValue = key;
|
|
462
|
+
const firstValue = state.current[keyValue];
|
|
463
|
+
const secondValue = newStateValues[keyValue];
|
|
464
|
+
return !handleCompare(firstValue, secondValue);
|
|
465
|
+
});
|
|
466
|
+
state.current = __spreadValues(__spreadValues({}, state.current), newStateValues);
|
|
467
|
+
renderKeyTrigger(changedKeys);
|
|
468
|
+
};
|
|
469
|
+
const setCacheData = (cacheData) => {
|
|
470
|
+
const data = mapResponseData(cacheData);
|
|
471
|
+
if (data instanceof Promise) {
|
|
472
|
+
return (() => __async(void 0, null, function* () {
|
|
473
|
+
const promiseData = yield data;
|
|
474
|
+
handleCacheData(promiseData);
|
|
475
|
+
}))();
|
|
476
|
+
}
|
|
477
|
+
return handleCacheData(data);
|
|
478
|
+
};
|
|
479
|
+
const actions = {
|
|
480
|
+
setData: (data, emitToCache = defaultCacheEmitting) => {
|
|
481
|
+
if (emitToCache) {
|
|
482
|
+
const currentState = state.current;
|
|
483
|
+
cache.set(request, __spreadProps(__spreadValues(__spreadValues({}, currentState), getDetailsState(state.current)), { success: true, data }));
|
|
484
|
+
} else {
|
|
485
|
+
state.current.data = data;
|
|
486
|
+
renderKeyTrigger(["data"]);
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
setError: (error, emitToCache = defaultCacheEmitting) => {
|
|
490
|
+
if (emitToCache) {
|
|
491
|
+
const currentState = state.current;
|
|
492
|
+
cache.set(request, __spreadProps(__spreadValues(__spreadValues({}, currentState), getDetailsState(state.current)), { success: false, error }));
|
|
493
|
+
} else {
|
|
494
|
+
state.current.error = error;
|
|
495
|
+
renderKeyTrigger(["error"]);
|
|
496
|
+
}
|
|
497
|
+
},
|
|
498
|
+
setLoading: (loading, emitToHooks = true) => {
|
|
499
|
+
if (emitToHooks) {
|
|
500
|
+
requestManager.events.emitLoading(queueKey, "", {
|
|
501
|
+
queueKey,
|
|
502
|
+
requestId: "",
|
|
503
|
+
loading,
|
|
504
|
+
isRetry: false,
|
|
505
|
+
isOffline: false
|
|
506
|
+
});
|
|
507
|
+
} else {
|
|
508
|
+
state.current.loading = loading;
|
|
509
|
+
renderKeyTrigger(["loading"]);
|
|
510
|
+
}
|
|
511
|
+
},
|
|
512
|
+
setStatus: (status, emitToCache = defaultCacheEmitting) => {
|
|
513
|
+
if (emitToCache) {
|
|
514
|
+
const currentState = state.current;
|
|
515
|
+
cache.set(request, __spreadProps(__spreadValues(__spreadValues({}, currentState), getDetailsState(state.current)), { status }));
|
|
516
|
+
} else {
|
|
517
|
+
state.current.status = status;
|
|
518
|
+
renderKeyTrigger(["status"]);
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
setSuccess: (success, emitToCache = defaultCacheEmitting) => {
|
|
522
|
+
if (emitToCache) {
|
|
523
|
+
const currentState = state.current;
|
|
524
|
+
cache.set(request, __spreadProps(__spreadValues(__spreadValues({}, currentState), getDetailsState(state.current)), { success }));
|
|
525
|
+
} else {
|
|
526
|
+
state.current.success = success;
|
|
527
|
+
renderKeyTrigger(["success"]);
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
setExtra: (extra, emitToCache = defaultCacheEmitting) => {
|
|
531
|
+
if (emitToCache) {
|
|
532
|
+
const currentState = state.current;
|
|
533
|
+
cache.set(request, __spreadProps(__spreadValues(__spreadValues({}, currentState), getDetailsState(state.current)), { extra }));
|
|
534
|
+
} else {
|
|
535
|
+
state.current.extra = extra;
|
|
536
|
+
renderKeyTrigger(["extra"]);
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
setRetries: (retries, emitToCache = defaultCacheEmitting) => {
|
|
540
|
+
if (emitToCache) {
|
|
541
|
+
const currentState = state.current;
|
|
542
|
+
cache.set(request, __spreadValues(__spreadValues({}, currentState), getDetailsState(state.current, { retries })));
|
|
543
|
+
} else {
|
|
544
|
+
state.current.retries = retries;
|
|
545
|
+
renderKeyTrigger(["retries"]);
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
setTimestamp: (timestamp, emitToCache = defaultCacheEmitting) => {
|
|
549
|
+
if (emitToCache) {
|
|
550
|
+
const currentState = state.current;
|
|
551
|
+
cache.set(request, __spreadValues(__spreadValues({}, currentState), getDetailsState(state.current, { timestamp: +timestamp })));
|
|
552
|
+
} else {
|
|
553
|
+
state.current.timestamp = timestamp;
|
|
554
|
+
renderKeyTrigger(["timestamp"]);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
return [state.current, actions, { setRenderKey, setCacheData, getStaleStatus }];
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
// src/helpers/use-socket-state/use-socket-state.hooks.ts
|
|
562
|
+
import { useRef as useRef3 } from "react";
|
|
563
|
+
import { useDidMount, useDidUpdate as useDidUpdate2, useForceUpdate as useForceUpdate2 } from "@better-hooks/lifecycle";
|
|
564
|
+
var useSocketState = (socket, { dependencyTracking }) => {
|
|
565
|
+
const forceUpdate = useForceUpdate2();
|
|
566
|
+
const onOpenCallback = useRef3(null);
|
|
567
|
+
const onCloseCallback = useRef3(null);
|
|
568
|
+
const onErrorCallback = useRef3(null);
|
|
569
|
+
const onConnectingCallback = useRef3(null);
|
|
570
|
+
const onReconnectingCallback = useRef3(null);
|
|
571
|
+
const onReconnectingStopCallback = useRef3(null);
|
|
572
|
+
const state = useRef3(initialSocketState);
|
|
573
|
+
const renderKeys = useRef3([]);
|
|
574
|
+
const renderKeyTrigger = (keys) => {
|
|
575
|
+
const shouldRerender = renderKeys.current.some((renderKey) => keys.includes(renderKey));
|
|
576
|
+
if (shouldRerender)
|
|
577
|
+
forceUpdate();
|
|
578
|
+
};
|
|
579
|
+
const setRenderKey = (renderKey) => {
|
|
580
|
+
if (!renderKeys.current.includes(renderKey)) {
|
|
581
|
+
renderKeys.current.push(renderKey);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
useDidUpdate2(
|
|
585
|
+
() => {
|
|
586
|
+
const handleDependencyTracking = () => {
|
|
587
|
+
if (!dependencyTracking) {
|
|
588
|
+
Object.keys(state.current).forEach((key) => setRenderKey(key));
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
handleDependencyTracking();
|
|
592
|
+
},
|
|
593
|
+
[dependencyTracking],
|
|
594
|
+
true
|
|
595
|
+
);
|
|
596
|
+
const actions = {
|
|
597
|
+
setData: (data) => {
|
|
598
|
+
state.current.data = data;
|
|
599
|
+
renderKeyTrigger(["data"]);
|
|
600
|
+
},
|
|
601
|
+
setConnected: (connected) => {
|
|
602
|
+
state.current.connected = connected;
|
|
603
|
+
renderKeyTrigger(["data"]);
|
|
604
|
+
},
|
|
605
|
+
setConnecting: (connecting) => {
|
|
606
|
+
state.current.connecting = connecting;
|
|
607
|
+
renderKeyTrigger(["data"]);
|
|
608
|
+
},
|
|
609
|
+
setTimestamp: (timestamp) => {
|
|
610
|
+
state.current.timestamp = timestamp;
|
|
611
|
+
renderKeyTrigger(["timestamp"]);
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
const callbacks = {
|
|
615
|
+
onOpen: (callback) => {
|
|
616
|
+
onOpenCallback.current = callback;
|
|
617
|
+
},
|
|
618
|
+
onClose: (callback) => {
|
|
619
|
+
onCloseCallback.current = callback;
|
|
620
|
+
},
|
|
621
|
+
onError: (callback) => {
|
|
622
|
+
onErrorCallback.current = callback;
|
|
623
|
+
},
|
|
624
|
+
onConnecting: (callback) => {
|
|
625
|
+
onConnectingCallback.current = callback;
|
|
626
|
+
},
|
|
627
|
+
onReconnecting: (callback) => {
|
|
628
|
+
onReconnectingCallback.current = callback;
|
|
629
|
+
},
|
|
630
|
+
onReconnectingStop: (callback) => {
|
|
631
|
+
onReconnectingStopCallback.current = callback;
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
useDidMount(() => {
|
|
635
|
+
const umountOnError = socket.events.onError((event) => {
|
|
636
|
+
var _a;
|
|
637
|
+
(_a = onErrorCallback.current) == null ? void 0 : _a.call(onErrorCallback, event);
|
|
638
|
+
});
|
|
639
|
+
const umountOnConnecting = socket.events.onConnecting(() => {
|
|
640
|
+
var _a;
|
|
641
|
+
actions.setConnecting(true);
|
|
642
|
+
(_a = onConnectingCallback.current) == null ? void 0 : _a.call(onConnectingCallback);
|
|
643
|
+
});
|
|
644
|
+
const umountOnOpen = socket.events.onOpen(() => {
|
|
645
|
+
var _a;
|
|
646
|
+
actions.setConnected(true);
|
|
647
|
+
(_a = onOpenCallback.current) == null ? void 0 : _a.call(onOpenCallback);
|
|
648
|
+
});
|
|
649
|
+
const umountOnClose = socket.events.onClose(() => {
|
|
650
|
+
var _a;
|
|
651
|
+
actions.setConnected(false);
|
|
652
|
+
(_a = onCloseCallback.current) == null ? void 0 : _a.call(onCloseCallback);
|
|
653
|
+
});
|
|
654
|
+
const umountOnReconnecting = socket.events.onReconnecting((attempts) => {
|
|
655
|
+
var _a;
|
|
656
|
+
(_a = onReconnectingCallback.current) == null ? void 0 : _a.call(onReconnectingCallback, attempts);
|
|
657
|
+
});
|
|
658
|
+
const umountOnReconnectingStop = socket.events.onReconnectingStop((attempts) => {
|
|
659
|
+
var _a;
|
|
660
|
+
(_a = onReconnectingStopCallback.current) == null ? void 0 : _a.call(onReconnectingStopCallback, attempts);
|
|
661
|
+
});
|
|
662
|
+
return () => {
|
|
663
|
+
umountOnError();
|
|
664
|
+
umountOnConnecting();
|
|
665
|
+
umountOnOpen();
|
|
666
|
+
umountOnClose();
|
|
667
|
+
umountOnReconnecting();
|
|
668
|
+
umountOnReconnectingStop();
|
|
669
|
+
};
|
|
670
|
+
});
|
|
671
|
+
return [state.current, actions, callbacks, { setRenderKey }];
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
// src/helpers/use-socket-state/use-socket-state.constants.ts
|
|
675
|
+
var initialSocketState = {
|
|
676
|
+
data: null,
|
|
677
|
+
connected: false,
|
|
678
|
+
connecting: false,
|
|
679
|
+
timestamp: null
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// src/config-provider/config-provider.tsx
|
|
683
|
+
import React, { useContext, useMemo, useState } from "react";
|
|
684
|
+
import { jsx } from "react/jsx-runtime";
|
|
685
|
+
var ConfigContext = React.createContext([{}, () => null]);
|
|
686
|
+
var ConfigProvider = ({ children, config }) => {
|
|
687
|
+
const [currentConfig, setConfig] = useState(config || {});
|
|
688
|
+
const value = useMemo(() => {
|
|
689
|
+
const contextValue = [currentConfig, setConfig];
|
|
690
|
+
return contextValue;
|
|
691
|
+
}, [currentConfig]);
|
|
692
|
+
return /* @__PURE__ */ jsx(ConfigContext.Provider, { value, children });
|
|
693
|
+
};
|
|
694
|
+
var useConfigProvider = () => {
|
|
695
|
+
const config = useContext(ConfigContext);
|
|
696
|
+
return config;
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
// src/hooks/use-fetch/use-fetch.hooks.ts
|
|
700
|
+
var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
701
|
+
const [globalConfig] = useConfigProvider();
|
|
702
|
+
const {
|
|
703
|
+
dependencies = useFetchDefaultOptions.dependencies,
|
|
704
|
+
disabled = useFetchDefaultOptions.disabled,
|
|
705
|
+
dependencyTracking = useFetchDefaultOptions.dependencyTracking,
|
|
706
|
+
fetchOnMount = useFetchDefaultOptions.fetchOnMount,
|
|
707
|
+
initialData = useFetchDefaultOptions.initialData,
|
|
708
|
+
refresh = useFetchDefaultOptions.refresh,
|
|
709
|
+
refreshTime = useFetchDefaultOptions.refreshTime,
|
|
710
|
+
refetchBlurred = useFetchDefaultOptions.refetchBlurred,
|
|
711
|
+
refetchOnBlur = useFetchDefaultOptions.refetchOnBlur,
|
|
712
|
+
refetchOnFocus = useFetchDefaultOptions.refetchOnFocus,
|
|
713
|
+
refetchOnReconnect = useFetchDefaultOptions.refetchOnReconnect,
|
|
714
|
+
bounce = useFetchDefaultOptions.bounce,
|
|
715
|
+
bounceType = useFetchDefaultOptions.bounceType,
|
|
716
|
+
bounceTime = useFetchDefaultOptions.bounceTime,
|
|
717
|
+
bounceTimeout = useFetchDefaultOptions.bounceTime,
|
|
718
|
+
deepCompare = useFetchDefaultOptions.deepCompare
|
|
719
|
+
} = __spreadValues(__spreadValues(__spreadValues({}, useFetchDefaultOptions), globalConfig.useFetchConfig), options);
|
|
720
|
+
const updateKey = JSON.stringify(request.toJSON());
|
|
721
|
+
const requestDebounce = useDebounce({ delay: bounceTime });
|
|
722
|
+
const requestThrottle = useThrottle({ interval: bounceTime, timeout: bounceTimeout });
|
|
723
|
+
const refreshDebounce = useDebounce({ delay: refreshTime });
|
|
724
|
+
const { cacheKey, queueKey, client } = request;
|
|
725
|
+
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
726
|
+
const logger = useRef4(loggerManager.init("useFetch")).current;
|
|
727
|
+
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
728
|
+
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
729
|
+
const [state, actions, { setRenderKey, setCacheData, getStaleStatus }] = useTrackedState({
|
|
730
|
+
logger,
|
|
731
|
+
request,
|
|
732
|
+
dispatcher,
|
|
733
|
+
initialData,
|
|
734
|
+
deepCompare,
|
|
735
|
+
dependencyTracking
|
|
736
|
+
});
|
|
737
|
+
const [callbacks, listeners] = useRequestEvents({
|
|
738
|
+
logger,
|
|
739
|
+
actions,
|
|
740
|
+
request,
|
|
741
|
+
dispatcher,
|
|
742
|
+
setCacheData
|
|
743
|
+
});
|
|
744
|
+
const { addDataListener, addLifecycleListeners, clearDataListener } = listeners;
|
|
745
|
+
const handleFetch = () => {
|
|
746
|
+
if (!disabled) {
|
|
747
|
+
logger.debug(`Fetching data`);
|
|
748
|
+
dispatcher.add(request);
|
|
749
|
+
} else {
|
|
750
|
+
logger.debug(`Cannot add to fetch queue`, { disabled });
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
function handleRefresh() {
|
|
754
|
+
if (!refresh)
|
|
755
|
+
return;
|
|
756
|
+
refreshDebounce.debounce(() => {
|
|
757
|
+
const isBlurred = !appManager.isFocused;
|
|
758
|
+
const isFetching = dispatcher.hasRunningRequests(request.queueKey);
|
|
759
|
+
const isQueued = dispatcher.getIsActiveQueue(request.queueKey);
|
|
760
|
+
const isActive = isFetching || isQueued;
|
|
761
|
+
const canRefreshBlurred = isBlurred && refetchBlurred && !isActive;
|
|
762
|
+
const canRefreshFocused = !isBlurred && !isActive;
|
|
763
|
+
if (canRefreshBlurred || canRefreshFocused) {
|
|
764
|
+
handleFetch();
|
|
765
|
+
logger.debug(`Performing refresh request`);
|
|
766
|
+
}
|
|
767
|
+
handleRefresh();
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
const handleInvalidation = (invalidateKey) => {
|
|
771
|
+
if (invalidateKey && invalidateKey instanceof Request) {
|
|
772
|
+
cache.invalidate(getRequestKey(invalidateKey));
|
|
773
|
+
} else if (invalidateKey && !(invalidateKey instanceof Request)) {
|
|
774
|
+
cache.invalidate(invalidateKey);
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
const refetch = (invalidateKey) => {
|
|
778
|
+
if (invalidateKey && Array.isArray(invalidateKey)) {
|
|
779
|
+
invalidateKey.forEach(handleInvalidation);
|
|
780
|
+
} else if (invalidateKey && !Array.isArray(invalidateKey)) {
|
|
781
|
+
handleInvalidation(invalidateKey);
|
|
782
|
+
} else {
|
|
783
|
+
handleFetch();
|
|
784
|
+
handleRefresh();
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
const initialFetchData = () => {
|
|
788
|
+
const hasStaleData = getStaleStatus();
|
|
789
|
+
const isFetching = dispatcher.getIsActiveQueue(queueKey);
|
|
790
|
+
if (fetchOnMount || hasStaleData && !isFetching) {
|
|
791
|
+
handleFetch();
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
const updateFetchData = () => {
|
|
795
|
+
if (bounce) {
|
|
796
|
+
logger.debug(`Bounce request with ${bounceType}`, { queueKey, request });
|
|
797
|
+
bounceFunction(() => handleFetch());
|
|
798
|
+
} else {
|
|
799
|
+
handleFetch();
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
const handleMountEvents = () => {
|
|
803
|
+
addDataListener(request);
|
|
804
|
+
addLifecycleListeners(request);
|
|
805
|
+
const focusUnmount = appManager.events.onFocus(() => {
|
|
806
|
+
if (refetchOnFocus) {
|
|
807
|
+
handleFetch();
|
|
808
|
+
handleRefresh();
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
const blurUnmount = appManager.events.onBlur(() => {
|
|
812
|
+
if (refetchOnBlur) {
|
|
813
|
+
handleFetch();
|
|
814
|
+
handleRefresh();
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
const onlineUnmount = appManager.events.onOnline(() => {
|
|
818
|
+
if (refetchOnReconnect) {
|
|
819
|
+
handleFetch();
|
|
820
|
+
handleRefresh();
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
const invalidateUnmount = cache.events.onInvalidate(cacheKey, handleFetch);
|
|
824
|
+
const unmount = () => {
|
|
825
|
+
clearDataListener();
|
|
826
|
+
focusUnmount();
|
|
827
|
+
blurUnmount();
|
|
828
|
+
onlineUnmount();
|
|
829
|
+
invalidateUnmount();
|
|
830
|
+
};
|
|
831
|
+
return unmount;
|
|
832
|
+
};
|
|
833
|
+
useDidUpdate3(handleMountEvents, [updateKey], true);
|
|
834
|
+
useDidMount2(initialFetchData);
|
|
835
|
+
useDidUpdate3(updateFetchData, [updateKey, disabled, ...dependencies]);
|
|
836
|
+
useDidUpdate3(handleRefresh, [updateKey, ...dependencies, disabled, refresh, refreshTime], true);
|
|
837
|
+
return __spreadProps(__spreadValues(__spreadValues({
|
|
838
|
+
get data() {
|
|
839
|
+
setRenderKey("data");
|
|
840
|
+
return state.data;
|
|
841
|
+
},
|
|
842
|
+
get error() {
|
|
843
|
+
setRenderKey("error");
|
|
844
|
+
return state.error;
|
|
845
|
+
},
|
|
846
|
+
get loading() {
|
|
847
|
+
setRenderKey("loading");
|
|
848
|
+
return state.loading;
|
|
849
|
+
},
|
|
850
|
+
get status() {
|
|
851
|
+
setRenderKey("status");
|
|
852
|
+
return state.status;
|
|
853
|
+
},
|
|
854
|
+
get success() {
|
|
855
|
+
setRenderKey("success");
|
|
856
|
+
return state.success;
|
|
857
|
+
},
|
|
858
|
+
get extra() {
|
|
859
|
+
setRenderKey("extra");
|
|
860
|
+
return state.extra;
|
|
861
|
+
},
|
|
862
|
+
get retries() {
|
|
863
|
+
setRenderKey("retries");
|
|
864
|
+
return state.retries;
|
|
865
|
+
},
|
|
866
|
+
get timestamp() {
|
|
867
|
+
setRenderKey("timestamp");
|
|
868
|
+
return state.timestamp;
|
|
869
|
+
},
|
|
870
|
+
bounce: getBounceData(bounceData)
|
|
871
|
+
}, actions), callbacks), {
|
|
872
|
+
refetch
|
|
873
|
+
});
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
// src/hooks/use-fetch/use-fetch.utils.ts
|
|
877
|
+
var getRefreshTime = (refreshTime, dataTimestamp) => {
|
|
878
|
+
if (dataTimestamp) {
|
|
879
|
+
const timeDiff = Date.now() - +dataTimestamp;
|
|
880
|
+
return timeDiff < refreshTime ? refreshTime - timeDiff : refreshTime;
|
|
881
|
+
}
|
|
882
|
+
return refreshTime;
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
// src/hooks/use-fetch/use-fetch.constants.ts
|
|
886
|
+
import { DateInterval } from "@hyper-fetch/core";
|
|
887
|
+
var useFetchDefaultOptions = {
|
|
888
|
+
dependencies: [],
|
|
889
|
+
disabled: false,
|
|
890
|
+
dependencyTracking: true,
|
|
891
|
+
fetchOnMount: true,
|
|
892
|
+
initialData: null,
|
|
893
|
+
refresh: false,
|
|
894
|
+
refreshTime: DateInterval.hour,
|
|
895
|
+
refetchBlurred: true,
|
|
896
|
+
refetchOnBlur: false,
|
|
897
|
+
refetchOnFocus: false,
|
|
898
|
+
refetchOnReconnect: false,
|
|
899
|
+
bounce: false,
|
|
900
|
+
bounceType: "debounce",
|
|
901
|
+
bounceTime: 400,
|
|
902
|
+
deepCompare: true
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
// src/hooks/use-submit/use-submit.hooks.ts
|
|
906
|
+
import { useMemo as useMemo2, useRef as useRef5 } from "react";
|
|
907
|
+
import {
|
|
908
|
+
Request as Request2,
|
|
909
|
+
getRequestKey as getRequestKey2,
|
|
910
|
+
sendRequest
|
|
911
|
+
} from "@hyper-fetch/core";
|
|
912
|
+
import { useDidMount as useDidMount3 } from "@better-hooks/lifecycle";
|
|
913
|
+
import { useDebounce as useDebounce2, useThrottle as useThrottle2 } from "@better-hooks/performance";
|
|
914
|
+
var useSubmit = (request, options = useSubmitDefaultOptions) => {
|
|
915
|
+
const [globalConfig] = useConfigProvider();
|
|
916
|
+
const mergedOptions = useMemo2(
|
|
917
|
+
() => __spreadValues(__spreadValues(__spreadValues({}, useSubmitDefaultOptions), globalConfig.useSubmitConfig), options),
|
|
918
|
+
[JSON.stringify(globalConfig.useSubmitConfig), JSON.stringify(options)]
|
|
919
|
+
);
|
|
920
|
+
const { disabled, dependencyTracking, initialData, bounce, bounceType, bounceTime, deepCompare } = mergedOptions;
|
|
921
|
+
const { client } = request;
|
|
922
|
+
const { cache, submitDispatcher: dispatcher, loggerManager } = client;
|
|
923
|
+
const logger = useRef5(loggerManager.init("useSubmit")).current;
|
|
924
|
+
const requestDebounce = useDebounce2({ delay: bounceTime });
|
|
925
|
+
const requestThrottle = useThrottle2({
|
|
926
|
+
interval: bounceTime,
|
|
927
|
+
timeout: "bounceTimeout" in mergedOptions ? mergedOptions.bounceTimeout : bounceTime
|
|
928
|
+
});
|
|
929
|
+
const bounceResolver = useRef5(() => null);
|
|
930
|
+
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
931
|
+
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
932
|
+
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState({
|
|
933
|
+
logger,
|
|
934
|
+
request,
|
|
935
|
+
dispatcher,
|
|
936
|
+
initialData,
|
|
937
|
+
deepCompare,
|
|
938
|
+
dependencyTracking
|
|
939
|
+
});
|
|
940
|
+
const [callbacks, listeners] = useRequestEvents({
|
|
941
|
+
logger,
|
|
942
|
+
actions,
|
|
943
|
+
request,
|
|
944
|
+
dispatcher,
|
|
945
|
+
setCacheData
|
|
946
|
+
});
|
|
947
|
+
const { addDataListener, addLifecycleListeners } = listeners;
|
|
948
|
+
const handleSubmit = (submitOptions) => {
|
|
949
|
+
const requestClone = request.clone(submitOptions);
|
|
950
|
+
if (disabled) {
|
|
951
|
+
logger.warning(`Cannot submit request`, { disabled, submitOptions });
|
|
952
|
+
return Promise.resolve({
|
|
953
|
+
data: null,
|
|
954
|
+
error: new Error("Cannot submit request. Option 'disabled' is enabled"),
|
|
955
|
+
status: null,
|
|
956
|
+
extra: request.client.defaultExtra
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
const triggerRequest = () => {
|
|
960
|
+
addDataListener(requestClone);
|
|
961
|
+
return sendRequest(requestClone, __spreadProps(__spreadValues({
|
|
962
|
+
dispatcherType: "submit"
|
|
963
|
+
}, submitOptions), {
|
|
964
|
+
onSettle: (requestId, cmd) => {
|
|
965
|
+
var _a;
|
|
966
|
+
addLifecycleListeners(requestClone, requestId);
|
|
967
|
+
(_a = submitOptions == null ? void 0 : submitOptions.onSettle) == null ? void 0 : _a.call(submitOptions, requestId, cmd);
|
|
968
|
+
}
|
|
969
|
+
}));
|
|
970
|
+
};
|
|
971
|
+
return new Promise((resolve) => {
|
|
972
|
+
const performSubmit = () => __async(void 0, null, function* () {
|
|
973
|
+
logger.debug(`Submitting request`, { disabled, submitOptions });
|
|
974
|
+
if (bounce) {
|
|
975
|
+
const bouncedResolve = bounceResolver.current;
|
|
976
|
+
bounceResolver.current = (value) => {
|
|
977
|
+
bouncedResolve(value);
|
|
978
|
+
resolve(value);
|
|
979
|
+
};
|
|
980
|
+
bounceFunction(() => __async(void 0, null, function* () {
|
|
981
|
+
const callback = bounceResolver.current;
|
|
982
|
+
bounceResolver.current = () => null;
|
|
983
|
+
const value = yield triggerRequest();
|
|
984
|
+
callback(value);
|
|
985
|
+
}));
|
|
986
|
+
} else {
|
|
987
|
+
const value = yield triggerRequest();
|
|
988
|
+
resolve(value);
|
|
989
|
+
}
|
|
990
|
+
});
|
|
991
|
+
performSubmit();
|
|
992
|
+
});
|
|
993
|
+
};
|
|
994
|
+
const handleInvalidation = (invalidateKey) => {
|
|
995
|
+
if (invalidateKey && invalidateKey instanceof Request2) {
|
|
996
|
+
cache.invalidate(getRequestKey2(invalidateKey));
|
|
997
|
+
} else if (invalidateKey && !(invalidateKey instanceof Request2)) {
|
|
998
|
+
cache.invalidate(invalidateKey);
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
const refetch = (invalidateKey) => {
|
|
1002
|
+
if (!invalidateKey)
|
|
1003
|
+
return;
|
|
1004
|
+
if (invalidateKey && Array.isArray(invalidateKey)) {
|
|
1005
|
+
invalidateKey.forEach(handleInvalidation);
|
|
1006
|
+
} else if (invalidateKey && !Array.isArray(invalidateKey)) {
|
|
1007
|
+
handleInvalidation(invalidateKey);
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
const handlers = {
|
|
1011
|
+
onSubmitSuccess: callbacks.onSuccess,
|
|
1012
|
+
onSubmitError: callbacks.onError,
|
|
1013
|
+
onSubmitFinished: callbacks.onFinished,
|
|
1014
|
+
onSubmitRequestStart: callbacks.onRequestStart,
|
|
1015
|
+
onSubmitResponseStart: callbacks.onResponseStart,
|
|
1016
|
+
onSubmitDownloadProgress: callbacks.onDownloadProgress,
|
|
1017
|
+
onSubmitUploadProgress: callbacks.onUploadProgress,
|
|
1018
|
+
onSubmitOfflineError: callbacks.onOfflineError,
|
|
1019
|
+
onSubmitAbort: callbacks.onAbort
|
|
1020
|
+
};
|
|
1021
|
+
useDidMount3(() => {
|
|
1022
|
+
addDataListener(request);
|
|
1023
|
+
});
|
|
1024
|
+
return __spreadProps(__spreadValues(__spreadValues({
|
|
1025
|
+
submit: handleSubmit,
|
|
1026
|
+
get data() {
|
|
1027
|
+
setRenderKey("data");
|
|
1028
|
+
return state.data;
|
|
1029
|
+
},
|
|
1030
|
+
get error() {
|
|
1031
|
+
setRenderKey("error");
|
|
1032
|
+
return state.error;
|
|
1033
|
+
},
|
|
1034
|
+
get submitting() {
|
|
1035
|
+
setRenderKey("loading");
|
|
1036
|
+
return state.loading;
|
|
1037
|
+
},
|
|
1038
|
+
get status() {
|
|
1039
|
+
setRenderKey("status");
|
|
1040
|
+
return state.status;
|
|
1041
|
+
},
|
|
1042
|
+
get success() {
|
|
1043
|
+
setRenderKey("success");
|
|
1044
|
+
return state.success;
|
|
1045
|
+
},
|
|
1046
|
+
get extra() {
|
|
1047
|
+
setRenderKey("extra");
|
|
1048
|
+
return state.extra;
|
|
1049
|
+
},
|
|
1050
|
+
get retries() {
|
|
1051
|
+
setRenderKey("retries");
|
|
1052
|
+
return state.retries;
|
|
1053
|
+
},
|
|
1054
|
+
get timestamp() {
|
|
1055
|
+
setRenderKey("timestamp");
|
|
1056
|
+
return state.timestamp;
|
|
1057
|
+
},
|
|
1058
|
+
abort: callbacks.abort
|
|
1059
|
+
}, actions), handlers), {
|
|
1060
|
+
bounce: getBounceData(bounceData),
|
|
1061
|
+
refetch
|
|
1062
|
+
});
|
|
1063
|
+
};
|
|
1064
|
+
|
|
1065
|
+
// src/hooks/use-submit/use-submit.constants.ts
|
|
1066
|
+
var useSubmitDefaultOptions = {
|
|
1067
|
+
disabled: false,
|
|
1068
|
+
dependencyTracking: true,
|
|
1069
|
+
initialData: null,
|
|
1070
|
+
bounce: false,
|
|
1071
|
+
bounceType: "debounce",
|
|
1072
|
+
bounceTime: 400,
|
|
1073
|
+
deepCompare: true
|
|
1074
|
+
};
|
|
1075
|
+
|
|
1076
|
+
// src/hooks/use-queue/use-queue.hooks.ts
|
|
1077
|
+
import { useState as useState2, useRef as useRef6 } from "react";
|
|
1078
|
+
import { getRequestDispatcher } from "@hyper-fetch/core";
|
|
1079
|
+
import { useDidMount as useDidMount4, useDidUpdate as useDidUpdate4 } from "@better-hooks/lifecycle";
|
|
1080
|
+
var useQueue = (request, options = useQueueDefaultOptions) => {
|
|
1081
|
+
const [globalConfig] = useConfigProvider();
|
|
1082
|
+
const { queueType = "auto" } = __spreadValues(__spreadValues(__spreadValues({}, useQueueDefaultOptions), globalConfig.useQueueConfig), options);
|
|
1083
|
+
const { abortKey, queueKey, client } = request;
|
|
1084
|
+
const { requestManager } = client;
|
|
1085
|
+
const [dispatcher] = getRequestDispatcher(request, queueType);
|
|
1086
|
+
const unmountCallbacks = useRef6(null);
|
|
1087
|
+
const [stopped, setStopped] = useState2(false);
|
|
1088
|
+
const [requests, setRequests] = useState2([]);
|
|
1089
|
+
const createRequestsArray = (queueElements) => {
|
|
1090
|
+
return queueElements.map((req) => __spreadProps(__spreadValues({}, req), {
|
|
1091
|
+
stopRequest: () => dispatcher.stopRequest(queueKey, req.requestId),
|
|
1092
|
+
startRequest: () => dispatcher.startRequest(queueKey, req.requestId),
|
|
1093
|
+
deleteRequest: () => dispatcher.delete(queueKey, req.requestId, abortKey)
|
|
1094
|
+
}));
|
|
1095
|
+
};
|
|
1096
|
+
const mergePayloadType = (requestId, data) => {
|
|
1097
|
+
setRequests((prev) => prev.map((el) => el.requestId === requestId ? __spreadValues(__spreadValues({}, el), data) : el));
|
|
1098
|
+
};
|
|
1099
|
+
const getInitialState2 = () => {
|
|
1100
|
+
const requestQueue = dispatcher.getQueue(queueKey);
|
|
1101
|
+
setStopped(requestQueue.stopped);
|
|
1102
|
+
setRequests(createRequestsArray(requestQueue.requests));
|
|
1103
|
+
};
|
|
1104
|
+
const updateQueueState = (values) => {
|
|
1105
|
+
setStopped(values.stopped);
|
|
1106
|
+
setRequests(createRequestsArray(values.requests));
|
|
1107
|
+
};
|
|
1108
|
+
const mountEvents = () => {
|
|
1109
|
+
var _a;
|
|
1110
|
+
const unmountChange = dispatcher.events.onQueueChange(queueKey, updateQueueState);
|
|
1111
|
+
const unmountStatus = dispatcher.events.onQueueStatus(queueKey, updateQueueState);
|
|
1112
|
+
const unmountDownload = requestManager.events.onDownloadProgress(queueKey, (progress, { requestId }) => {
|
|
1113
|
+
mergePayloadType(requestId, { downloading: progress });
|
|
1114
|
+
});
|
|
1115
|
+
const unmountUpload = requestManager.events.onUploadProgress(queueKey, (progress, { requestId }) => {
|
|
1116
|
+
mergePayloadType(requestId, { uploading: progress });
|
|
1117
|
+
});
|
|
1118
|
+
const unmount = () => {
|
|
1119
|
+
unmountStatus();
|
|
1120
|
+
unmountChange();
|
|
1121
|
+
unmountDownload();
|
|
1122
|
+
unmountUpload();
|
|
1123
|
+
};
|
|
1124
|
+
(_a = unmountCallbacks.current) == null ? void 0 : _a.call(unmountCallbacks);
|
|
1125
|
+
unmountCallbacks.current = unmount;
|
|
1126
|
+
return unmount;
|
|
1127
|
+
};
|
|
1128
|
+
useDidMount4(getInitialState2);
|
|
1129
|
+
useDidUpdate4(mountEvents, [stopped, requests, setRequests, setStopped], true);
|
|
1130
|
+
return {
|
|
1131
|
+
stopped,
|
|
1132
|
+
requests,
|
|
1133
|
+
stop: () => dispatcher.stop(queueKey),
|
|
1134
|
+
pause: () => dispatcher.pause(queueKey),
|
|
1135
|
+
start: () => dispatcher.start(queueKey)
|
|
1136
|
+
};
|
|
1137
|
+
};
|
|
1138
|
+
|
|
1139
|
+
// src/hooks/use-queue/use-queue.constants.ts
|
|
1140
|
+
var useQueueDefaultOptions = {
|
|
1141
|
+
queueType: "auto"
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
// src/hooks/use-cache/use-cache.hooks.ts
|
|
1145
|
+
import { useRef as useRef7 } from "react";
|
|
1146
|
+
import { getRequestDispatcher as getRequestDispatcher2, Request as Request3, getRequestKey as getRequestKey3 } from "@hyper-fetch/core";
|
|
1147
|
+
var useCache = (request, options = useCacheDefaultOptions) => {
|
|
1148
|
+
const { cacheKey, client } = request;
|
|
1149
|
+
const { cache, loggerManager } = client;
|
|
1150
|
+
const logger = useRef7(loggerManager.init("useCache")).current;
|
|
1151
|
+
const [dispatcher] = getRequestDispatcher2(request);
|
|
1152
|
+
const [globalConfig] = useConfigProvider();
|
|
1153
|
+
const { dependencyTracking, initialData, deepCompare } = __spreadValues(__spreadValues(__spreadValues({}, useCacheDefaultOptions), globalConfig.useCacheConfig), options);
|
|
1154
|
+
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState({
|
|
1155
|
+
logger,
|
|
1156
|
+
request,
|
|
1157
|
+
dispatcher,
|
|
1158
|
+
initialData,
|
|
1159
|
+
deepCompare,
|
|
1160
|
+
dependencyTracking
|
|
1161
|
+
});
|
|
1162
|
+
const [callbacks] = useRequestEvents({
|
|
1163
|
+
logger,
|
|
1164
|
+
actions,
|
|
1165
|
+
request,
|
|
1166
|
+
dispatcher,
|
|
1167
|
+
setCacheData
|
|
1168
|
+
});
|
|
1169
|
+
const refetch = (invalidateKey) => {
|
|
1170
|
+
if (invalidateKey instanceof Request3) {
|
|
1171
|
+
cache.invalidate(getRequestKey3(invalidateKey, true));
|
|
1172
|
+
} else if (invalidateKey) {
|
|
1173
|
+
cache.invalidate(invalidateKey);
|
|
1174
|
+
} else {
|
|
1175
|
+
cache.invalidate(cacheKey);
|
|
1176
|
+
}
|
|
1177
|
+
};
|
|
1178
|
+
return __spreadProps(__spreadValues({
|
|
1179
|
+
get data() {
|
|
1180
|
+
setRenderKey("data");
|
|
1181
|
+
return state.data;
|
|
1182
|
+
},
|
|
1183
|
+
get error() {
|
|
1184
|
+
setRenderKey("error");
|
|
1185
|
+
return state.error;
|
|
1186
|
+
},
|
|
1187
|
+
get loading() {
|
|
1188
|
+
setRenderKey("loading");
|
|
1189
|
+
return state.loading;
|
|
1190
|
+
},
|
|
1191
|
+
get status() {
|
|
1192
|
+
setRenderKey("status");
|
|
1193
|
+
return state.status;
|
|
1194
|
+
},
|
|
1195
|
+
get success() {
|
|
1196
|
+
setRenderKey("success");
|
|
1197
|
+
return state.success;
|
|
1198
|
+
},
|
|
1199
|
+
get extra() {
|
|
1200
|
+
setRenderKey("extra");
|
|
1201
|
+
return state.extra;
|
|
1202
|
+
},
|
|
1203
|
+
get retries() {
|
|
1204
|
+
setRenderKey("retries");
|
|
1205
|
+
return state.retries;
|
|
1206
|
+
},
|
|
1207
|
+
get timestamp() {
|
|
1208
|
+
setRenderKey("timestamp");
|
|
1209
|
+
return state.timestamp;
|
|
1210
|
+
},
|
|
1211
|
+
onCacheError: callbacks.onError,
|
|
1212
|
+
onCacheSuccess: callbacks.onSuccess,
|
|
1213
|
+
onCacheChange: callbacks.onFinished
|
|
1214
|
+
}, actions), {
|
|
1215
|
+
refetch
|
|
1216
|
+
});
|
|
1217
|
+
};
|
|
1218
|
+
|
|
1219
|
+
// src/hooks/use-cache/use-cache.constants.ts
|
|
1220
|
+
var useCacheDefaultOptions = {
|
|
1221
|
+
dependencyTracking: true,
|
|
1222
|
+
initialData: null,
|
|
1223
|
+
deepCompare: true
|
|
1224
|
+
};
|
|
1225
|
+
|
|
1226
|
+
// src/hooks/use-listener/use-listener.hooks.ts
|
|
1227
|
+
import { useRef as useRef8 } from "react";
|
|
1228
|
+
import { useDidUpdate as useDidUpdate5, useWillUnmount as useWillUnmount2 } from "@better-hooks/lifecycle";
|
|
1229
|
+
var useListener = (listener, options) => {
|
|
1230
|
+
const [globalConfig] = useConfigProvider();
|
|
1231
|
+
const { dependencyTracking } = __spreadValues(__spreadValues({}, globalConfig.useListener), options);
|
|
1232
|
+
const onEventCallback = useRef8(null);
|
|
1233
|
+
const removeListenerRef = useRef8(null);
|
|
1234
|
+
const [state, actions, callbacks, { setRenderKey }] = useSocketState(listener.socket, { dependencyTracking });
|
|
1235
|
+
const stopListener = () => {
|
|
1236
|
+
var _a;
|
|
1237
|
+
(_a = removeListenerRef.current) == null ? void 0 : _a.call(removeListenerRef);
|
|
1238
|
+
};
|
|
1239
|
+
const listen = () => {
|
|
1240
|
+
stopListener();
|
|
1241
|
+
removeListenerRef.current = listener.listen({
|
|
1242
|
+
callback: ({ data, extra }) => {
|
|
1243
|
+
var _a;
|
|
1244
|
+
(_a = onEventCallback.current) == null ? void 0 : _a.call(onEventCallback, { data, extra });
|
|
1245
|
+
actions.setData(data);
|
|
1246
|
+
actions.setTimestamp(+new Date());
|
|
1247
|
+
}
|
|
1248
|
+
});
|
|
1249
|
+
};
|
|
1250
|
+
useDidUpdate5(
|
|
1251
|
+
() => {
|
|
1252
|
+
listen();
|
|
1253
|
+
},
|
|
1254
|
+
[listener],
|
|
1255
|
+
true
|
|
1256
|
+
);
|
|
1257
|
+
useWillUnmount2(() => {
|
|
1258
|
+
stopListener();
|
|
1259
|
+
});
|
|
1260
|
+
const additionalCallbacks = {
|
|
1261
|
+
onEvent: (callback) => {
|
|
1262
|
+
onEventCallback.current = callback;
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
return __spreadProps(__spreadValues(__spreadValues(__spreadValues({
|
|
1266
|
+
get data() {
|
|
1267
|
+
setRenderKey("data");
|
|
1268
|
+
return state.data;
|
|
1269
|
+
},
|
|
1270
|
+
get connected() {
|
|
1271
|
+
setRenderKey("connected");
|
|
1272
|
+
return state.connected;
|
|
1273
|
+
},
|
|
1274
|
+
get connecting() {
|
|
1275
|
+
setRenderKey("connecting");
|
|
1276
|
+
return state.connecting;
|
|
1277
|
+
},
|
|
1278
|
+
get timestamp() {
|
|
1279
|
+
setRenderKey("timestamp");
|
|
1280
|
+
return state.timestamp;
|
|
1281
|
+
}
|
|
1282
|
+
}, actions), callbacks), additionalCallbacks), {
|
|
1283
|
+
listen
|
|
1284
|
+
});
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
// src/hooks/use-emitter/use-emitter.hooks.ts
|
|
1288
|
+
import { useRef as useRef9 } from "react";
|
|
1289
|
+
var useEmitter = (emitter, options) => {
|
|
1290
|
+
const [globalConfig] = useConfigProvider();
|
|
1291
|
+
const { dependencyTracking } = __spreadValues(__spreadValues({}, globalConfig.useEmitter), options);
|
|
1292
|
+
const onEventCallback = useRef9(null);
|
|
1293
|
+
const [state, actions, callbacks, { setRenderKey }] = useSocketState(emitter.socket, { dependencyTracking });
|
|
1294
|
+
const additionalCallbacks = {
|
|
1295
|
+
onEvent: (callback) => {
|
|
1296
|
+
onEventCallback.current = callback;
|
|
1297
|
+
}
|
|
1298
|
+
};
|
|
1299
|
+
const emit = (emitOptions) => {
|
|
1300
|
+
var _a;
|
|
1301
|
+
actions.setTimestamp(+new Date());
|
|
1302
|
+
(_a = onEventCallback.current) == null ? void 0 : _a.call(onEventCallback, emitter);
|
|
1303
|
+
return emitter.emit(emitOptions);
|
|
1304
|
+
};
|
|
1305
|
+
return __spreadProps(__spreadValues(__spreadValues(__spreadValues({
|
|
1306
|
+
get connected() {
|
|
1307
|
+
setRenderKey("connected");
|
|
1308
|
+
return state.connected;
|
|
1309
|
+
},
|
|
1310
|
+
get connecting() {
|
|
1311
|
+
setRenderKey("connecting");
|
|
1312
|
+
return state.connecting;
|
|
1313
|
+
},
|
|
1314
|
+
get timestamp() {
|
|
1315
|
+
setRenderKey("timestamp");
|
|
1316
|
+
return state.timestamp;
|
|
1317
|
+
}
|
|
1318
|
+
}, actions), callbacks), additionalCallbacks), {
|
|
1319
|
+
emit,
|
|
1320
|
+
reconnect: emitter.socket.reconnect
|
|
1321
|
+
});
|
|
1322
|
+
};
|
|
1323
|
+
|
|
1324
|
+
// src/hooks/use-event-messages/use-event-messages.hooks.ts
|
|
1325
|
+
import { useRef as useRef10 } from "react";
|
|
1326
|
+
import { useDidUpdate as useDidUpdate6 } from "@better-hooks/lifecycle";
|
|
1327
|
+
var useEventMessages = (socket, options) => {
|
|
1328
|
+
const [globalConfig] = useConfigProvider();
|
|
1329
|
+
const { dependencyTracking = false, filter } = __spreadValues(__spreadValues({}, globalConfig.useEventMessages), options);
|
|
1330
|
+
const onEventCallback = useRef10(null);
|
|
1331
|
+
const [state, actions, callbacks, { setRenderKey }] = useSocketState(socket, { dependencyTracking });
|
|
1332
|
+
useDidUpdate6(
|
|
1333
|
+
() => {
|
|
1334
|
+
const unmountListener = socket.events.onListenerEvent(({ endpoint, data, extra }) => {
|
|
1335
|
+
var _a;
|
|
1336
|
+
const filterFn = typeof filter === "function" ? () => filter(endpoint, data) : () => filter.includes(endpoint);
|
|
1337
|
+
const isFiltered = filter ? filterFn() : false;
|
|
1338
|
+
if (!isFiltered) {
|
|
1339
|
+
(_a = onEventCallback.current) == null ? void 0 : _a.call(onEventCallback, data, extra);
|
|
1340
|
+
actions.setData(data);
|
|
1341
|
+
actions.setTimestamp(+new Date());
|
|
1342
|
+
}
|
|
1343
|
+
});
|
|
1344
|
+
return unmountListener;
|
|
1345
|
+
},
|
|
1346
|
+
[socket, filter],
|
|
1347
|
+
true
|
|
1348
|
+
);
|
|
1349
|
+
const additionalCallbacks = {
|
|
1350
|
+
onEvent: (callback) => {
|
|
1351
|
+
onEventCallback.current = callback;
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
return __spreadValues(__spreadValues(__spreadValues({
|
|
1355
|
+
get data() {
|
|
1356
|
+
setRenderKey("data");
|
|
1357
|
+
return state.data;
|
|
1358
|
+
},
|
|
1359
|
+
get connected() {
|
|
1360
|
+
setRenderKey("connected");
|
|
1361
|
+
return state.connected;
|
|
1362
|
+
},
|
|
1363
|
+
get connecting() {
|
|
1364
|
+
setRenderKey("connecting");
|
|
1365
|
+
return state.connecting;
|
|
1366
|
+
},
|
|
1367
|
+
get timestamp() {
|
|
1368
|
+
setRenderKey("timestamp");
|
|
1369
|
+
return state.timestamp;
|
|
1370
|
+
}
|
|
1371
|
+
}, actions), callbacks), additionalCallbacks);
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
// src/hooks/use-app-manager/use-app-manager.hooks.ts
|
|
1375
|
+
import { useState as useState3 } from "react";
|
|
1376
|
+
import { useDidMount as useDidMount5 } from "@better-hooks/lifecycle";
|
|
1377
|
+
var useAppManager = (client) => {
|
|
1378
|
+
const [online, setIsOnline] = useState3(client.appManager.isOnline);
|
|
1379
|
+
const [focused, setIsFocused] = useState3(client.appManager.isFocused);
|
|
1380
|
+
const mountEvents = () => {
|
|
1381
|
+
const unmountIsOnline = client.appManager.events.onOnline(() => setIsOnline(true));
|
|
1382
|
+
const unmountIsOffline = client.appManager.events.onOffline(() => setIsOnline(false));
|
|
1383
|
+
const unmountIsFocus = client.appManager.events.onFocus(() => setIsFocused(true));
|
|
1384
|
+
const unmountIsBlur = client.appManager.events.onBlur(() => setIsFocused(false));
|
|
1385
|
+
return () => {
|
|
1386
|
+
unmountIsOnline();
|
|
1387
|
+
unmountIsOffline();
|
|
1388
|
+
unmountIsFocus();
|
|
1389
|
+
unmountIsBlur();
|
|
1390
|
+
};
|
|
1391
|
+
};
|
|
1392
|
+
const setOnline = (isOnline) => {
|
|
1393
|
+
client.appManager.setOnline(isOnline);
|
|
1394
|
+
};
|
|
1395
|
+
const setFocused = (isFocused) => {
|
|
1396
|
+
client.appManager.setFocused(isFocused);
|
|
1397
|
+
};
|
|
1398
|
+
useDidMount5(mountEvents);
|
|
1399
|
+
return { isOnline: online, isFocused: focused, setOnline, setFocused };
|
|
1400
|
+
};
|
|
1401
|
+
export {
|
|
1402
|
+
ConfigProvider,
|
|
1403
|
+
getBounceData,
|
|
1404
|
+
getDetailsState,
|
|
1405
|
+
getInitialState,
|
|
1406
|
+
getRefreshTime,
|
|
1407
|
+
getTimestamp,
|
|
1408
|
+
getValidCacheData,
|
|
1409
|
+
initialSocketState,
|
|
1410
|
+
initialState,
|
|
1411
|
+
isEmpty,
|
|
1412
|
+
isEqual,
|
|
1413
|
+
isStaleCacheData,
|
|
1414
|
+
useAppManager,
|
|
1415
|
+
useCache,
|
|
1416
|
+
useCacheDefaultOptions,
|
|
1417
|
+
useConfigProvider,
|
|
1418
|
+
useEmitter,
|
|
1419
|
+
useEventMessages,
|
|
1420
|
+
useFetch,
|
|
1421
|
+
useFetchDefaultOptions,
|
|
1422
|
+
useListener,
|
|
1423
|
+
useQueue,
|
|
1424
|
+
useQueueDefaultOptions,
|
|
1425
|
+
useRequestEvents,
|
|
1426
|
+
useSocketState,
|
|
1427
|
+
useSubmit,
|
|
1428
|
+
useSubmitDefaultOptions,
|
|
1429
|
+
useTrackedState
|
|
1430
|
+
};
|
|
2
1431
|
//# sourceMappingURL=index.esm.js.map
|