@hyper-fetch/react 3.3.4 → 4.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/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +3 -3
- package/dist/index.d.ts +60 -23
- package/dist/index.esm.js +1 -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 +25 -20
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +3 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +78 -28
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +24 -5
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +35 -16
- package/src/hooks/use-cache/use-cache.hooks.ts +8 -0
- package/src/hooks/use-emitter/use-emitter.hooks.ts +0 -2
- package/src/hooks/use-fetch/use-fetch.constants.ts +0 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +12 -4
- package/src/hooks/use-fetch/use-fetch.types.ts +26 -13
- package/src/hooks/use-queue/use-queue.hooks.ts +2 -2
- package/src/hooks/use-queue/use-queue.types.ts +2 -2
- package/src/hooks/use-submit/use-submit.constants.ts +0 -1
- package/src/hooks/use-submit/use-submit.hooks.ts +42 -11
- package/src/hooks/use-submit/use-submit.types.ts +26 -13
- package/tsconfig.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _hyper_fetch_core from '@hyper-fetch/core';
|
|
2
|
+
import { RequestInstance, Dispatcher, LoggerType, CacheValueType, ExtractResponseType, ExtractErrorType, ResponseDetailsType, ExtractAdapterReturnType, RequestEventType, ProgressType, ResponseReturnType, ExtractAdapterType, ExtractAdapterStatusType, ExtractAdapterExtraType, NullableType, RequiredKeys, RequestSendType, QueueElementType, ClientInstance } from '@hyper-fetch/core';
|
|
2
3
|
import * as _hyper_fetch_sockets from '@hyper-fetch/sockets';
|
|
3
4
|
import { SocketInstance, ListenerInstance, ExtractListenerAdapterType, EmitterInstance } from '@hyper-fetch/sockets';
|
|
4
5
|
import React from 'react';
|
|
@@ -123,7 +124,7 @@ declare const getBounceData: (bounceData: {
|
|
|
123
124
|
type UseTrackedStateProps<T extends RequestInstance> = {
|
|
124
125
|
request: T;
|
|
125
126
|
logger: LoggerType;
|
|
126
|
-
initialData:
|
|
127
|
+
initialData: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>> | null;
|
|
127
128
|
dispatcher: Dispatcher;
|
|
128
129
|
dependencyTracking: boolean;
|
|
129
130
|
defaultCacheEmitting?: boolean;
|
|
@@ -152,9 +153,17 @@ type UseTrackedStateType<T extends RequestInstance = RequestInstance> = {
|
|
|
152
153
|
*/
|
|
153
154
|
loading: boolean;
|
|
154
155
|
/**
|
|
155
|
-
* Request
|
|
156
|
+
* Request status
|
|
156
157
|
*/
|
|
157
|
-
status:
|
|
158
|
+
status: ExtractAdapterStatusType<ExtractAdapterType<T>>;
|
|
159
|
+
/**
|
|
160
|
+
* Request additional response data
|
|
161
|
+
*/
|
|
162
|
+
extra: ExtractAdapterExtraType<ExtractAdapterType<T>>;
|
|
163
|
+
/**
|
|
164
|
+
* Information whether request succeeded
|
|
165
|
+
*/
|
|
166
|
+
success: boolean;
|
|
158
167
|
/**
|
|
159
168
|
* Request attempts
|
|
160
169
|
*/
|
|
@@ -180,7 +189,15 @@ type UseTrackedStateActions<T extends RequestInstance> = {
|
|
|
180
189
|
/**
|
|
181
190
|
* Action to set custom status. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
|
|
182
191
|
*/
|
|
183
|
-
setStatus: (status:
|
|
192
|
+
setStatus: (status: ExtractAdapterStatusType<ExtractAdapterType<T>>, emitToCache?: boolean) => void;
|
|
193
|
+
/**
|
|
194
|
+
* Action to set custom success. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
|
|
195
|
+
*/
|
|
196
|
+
setSuccess: (success: boolean, emitToCache?: boolean) => void;
|
|
197
|
+
/**
|
|
198
|
+
* Action to set custom additional data. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
|
|
199
|
+
*/
|
|
200
|
+
setExtra: (extra: ExtractAdapterExtraType<ExtractAdapterType<T>>, emitToCache?: boolean) => void;
|
|
184
201
|
/**
|
|
185
202
|
* Action to set custom retries count. We can do it locally(inside hook state) or in cache(all related sources) with 'emitToCache' option
|
|
186
203
|
*/
|
|
@@ -203,11 +220,11 @@ declare const initialState: UseTrackedStateType;
|
|
|
203
220
|
*/
|
|
204
221
|
declare const useTrackedState: <T extends RequestInstance>({ request, dispatcher, initialData, deepCompare, dependencyTracking, defaultCacheEmitting, }: UseTrackedStateProps<T>) => UseTrackedStateReturn<T>;
|
|
205
222
|
|
|
206
|
-
declare const getDetailsState: (state?: UseTrackedStateType<RequestInstance>, details?: Partial<
|
|
223
|
+
declare const getDetailsState: (state?: UseTrackedStateType<RequestInstance>, details?: Partial<ResponseDetailsType>) => ResponseDetailsType;
|
|
207
224
|
declare const isStaleCacheData: (cacheTime: number, cacheTimestamp: NullableType<Date | number>) => boolean;
|
|
208
|
-
declare const getValidCacheData: <T extends RequestInstance>(request: T, initialData: ExtractAdapterReturnType<T>, cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T
|
|
225
|
+
declare const getValidCacheData: <T extends RequestInstance>(request: T, initialData: ExtractAdapterReturnType<T>, cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, _hyper_fetch_core.AdapterInstance>) => CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, _hyper_fetch_core.AdapterInstance>;
|
|
209
226
|
declare const getTimestamp: (timestamp?: NullableType<number | Date>) => Date;
|
|
210
|
-
declare const getInitialState: <T extends RequestInstance>(initialData:
|
|
227
|
+
declare const getInitialState: <T extends RequestInstance>(initialData: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>, dispatcher: Dispatcher, request: T) => UseTrackedStateType<T>;
|
|
211
228
|
|
|
212
229
|
declare const useSocketState: <DataType>(socket: SocketInstance, { dependencyTracking }: UseSocketStateProps) => readonly [UseSocketStateType<DataType>, {
|
|
213
230
|
setData: (data: DataType) => void;
|
|
@@ -288,23 +305,33 @@ type UseFetchOptionsType<T extends RequestInstance> = {
|
|
|
288
305
|
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
|
|
289
306
|
*/
|
|
290
307
|
bounce?: boolean;
|
|
308
|
+
/**
|
|
309
|
+
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
310
|
+
*/
|
|
311
|
+
deepCompare?: boolean | typeof isEqual;
|
|
312
|
+
} & ({
|
|
291
313
|
/**
|
|
292
314
|
* Possibility to choose between debounce and throttle approaches
|
|
293
315
|
*/
|
|
294
|
-
bounceType?: "debounce"
|
|
316
|
+
bounceType?: "debounce";
|
|
295
317
|
/**
|
|
296
318
|
* How long it should bounce requests.
|
|
297
319
|
*/
|
|
298
320
|
bounceTime?: number;
|
|
321
|
+
} | {
|
|
299
322
|
/**
|
|
300
|
-
*
|
|
323
|
+
* Possibility to choose between debounce and throttle approaches
|
|
301
324
|
*/
|
|
302
|
-
|
|
325
|
+
bounceType: "throttle";
|
|
303
326
|
/**
|
|
304
|
-
*
|
|
327
|
+
* How long it should bounce requests.
|
|
305
328
|
*/
|
|
306
|
-
|
|
307
|
-
|
|
329
|
+
bounceTime?: number;
|
|
330
|
+
/**
|
|
331
|
+
* ONLY in throttle mode - options for handling last bounce event
|
|
332
|
+
*/
|
|
333
|
+
bounceTimeout?: number;
|
|
334
|
+
});
|
|
308
335
|
type UseFetchReturnType<T extends RequestInstance> = UseTrackedStateType<T> & UseTrackedStateActions<T> & UseRequestEventsActionsType<T> & {
|
|
309
336
|
/**
|
|
310
337
|
* Data related to current state of the bounce usage
|
|
@@ -353,27 +380,37 @@ type UseSubmitOptionsType<T extends RequestInstance> = {
|
|
|
353
380
|
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
|
|
354
381
|
*/
|
|
355
382
|
bounce?: boolean;
|
|
383
|
+
/**
|
|
384
|
+
* If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
|
|
385
|
+
*/
|
|
386
|
+
dependencyTracking?: boolean;
|
|
387
|
+
/**
|
|
388
|
+
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
389
|
+
*/
|
|
390
|
+
deepCompare?: boolean | typeof isEqual;
|
|
391
|
+
} & ({
|
|
356
392
|
/**
|
|
357
393
|
* Possibility to choose between debounce and throttle approaches
|
|
358
394
|
*/
|
|
359
|
-
bounceType?: "debounce"
|
|
395
|
+
bounceType?: "debounce";
|
|
360
396
|
/**
|
|
361
397
|
* How long it should bounce requests.
|
|
362
398
|
*/
|
|
363
399
|
bounceTime?: number;
|
|
400
|
+
} | {
|
|
364
401
|
/**
|
|
365
|
-
*
|
|
402
|
+
* Possibility to choose between debounce and throttle approaches
|
|
366
403
|
*/
|
|
367
|
-
|
|
404
|
+
bounceType: "throttle";
|
|
368
405
|
/**
|
|
369
|
-
*
|
|
406
|
+
* How long it should interval requests.
|
|
370
407
|
*/
|
|
371
|
-
|
|
408
|
+
bounceTime?: number;
|
|
372
409
|
/**
|
|
373
|
-
*
|
|
410
|
+
* ONLY in throttle mode - options for handling last bounce event
|
|
374
411
|
*/
|
|
375
|
-
|
|
376
|
-
};
|
|
412
|
+
bounceTimeout?: number;
|
|
413
|
+
});
|
|
377
414
|
type UseSubmitReturnType<RequestType extends RequestInstance> = Omit<UseTrackedStateType<RequestType>, "loading"> & UseTrackedStateActions<RequestType> & {
|
|
378
415
|
/**
|
|
379
416
|
* Callback which allows to cancel ongoing requests from given queueKey.
|
|
@@ -458,7 +495,7 @@ declare const useQueue: <Request_1 extends RequestInstance>(request: Request_1,
|
|
|
458
495
|
type UseQueueOptionsType = {
|
|
459
496
|
queueType?: "auto" | "fetch" | "submit";
|
|
460
497
|
};
|
|
461
|
-
type QueueRequest<Request extends RequestInstance> =
|
|
498
|
+
type QueueRequest<Request extends RequestInstance> = QueueElementType<Request> & {
|
|
462
499
|
/**
|
|
463
500
|
* Uploading progress for given request
|
|
464
501
|
*/
|
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Le=Object.defineProperty,Me=Object.defineProperties;var we=Object.getOwnPropertyDescriptors;var be=Object.getOwnPropertySymbols;var Ae=Object.prototype.hasOwnProperty,Ke=Object.prototype.propertyIsEnumerable;var Se=(e,t,r)=>t in e?Le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,m=(e,t)=>{for(var r in t||(t={}))Ae.call(t,r)&&Se(e,r,t[r]);if(be)for(var r of be(t))Ke.call(t,r)&&Se(e,r,t[r]);return e},F=(e,t)=>Me(e,we(t));var se=(e,t,r)=>new Promise((g,i)=>{var p=n=>{try{c(r.next(n))}catch(l){i(l)}},d=n=>{try{c(r.throw(n))}catch(l){i(l)}},c=n=>n.done?g(n.value):Promise.resolve(n.value).then(p,d);c((r=r.apply(e,t)).next())});import{useRef as Ye}from"react";import{useDidUpdate as Te,useDidMount as Ze}from"@better-hooks/lifecycle";import{useDebounce as Ue,useThrottle as _e}from"@better-hooks/performance";import{Request as ke,getRequestKey as et}from"@hyper-fetch/core";import{useRef as V}from"react";import{useWillUnmount as Be}from"@better-hooks/lifecycle";var _=({request:e,dispatcher:t,logger:r,actions:g,setCacheData:i})=>{let{cache:p,requestManager:d}=e.client,c=V(null),n=V(null),l=V(null),R=V(null),b=V(null),x=V(null),u=V(null),E=V(null),T=V(null),S=V(new Map),U=V(null),P=o=>{let y=S.current.get(o);y==null||y.unmount(),S.current.delete(o)},q=()=>{let o=S.current;Array.from(o.values()).forEach(a=>{a.unmount()}),o.clear()},O=(o,y,a)=>{var G,w,K,B,Z;let{isOffline:I,isFailed:W,isCanceled:j}=a;e.offline&&I&&W?(r.debug("Performing offline error callback",{data:y,details:a}),(G=R.current)==null||G.call(R,{response:y[1],request:o,details:a})):j?(r.debug("Performing abort callback",{data:y,details:a}),(w=l.current)==null||w.call(l,{response:y[1],request:o,details:a})):W?(r.debug("Performing error callback",{data:y,details:a}),(B=n.current)==null||B.call(n,{response:y[1],request:o,details:a})):(r.debug("Performing success callback",{data:y,details:a}),(K=c.current)==null||K.call(c,{response:y[0],request:o,details:a})),(Z=b.current)==null||Z.call(b,{response:y,request:o,details:a})},s=o=>({loading:y})=>{let a=!y&&!t.hasRunningRequests(o);(y||a)&&g.setLoading(y,!1)},f=(o,y)=>{var a;(a=E.current)==null||a.call(E,o,y)},h=(o,y)=>{var a;(a=T.current)==null||a.call(T,o,y)},C=o=>y=>{var a;(a=x.current)==null||a.call(x,{request:o,details:y})},L=o=>y=>{var a;(a=u.current)==null||a.call(u,{request:o,details:y})},v=o=>(y,a)=>{O(o,y,a)},A=({requestId:o})=>{P(o)},Q=()=>{var o;(o=U.current)==null||o.unmount(),U.current=null},ne=o=>{let y=d.events.onLoading(o.queueKey,s(o.queueKey)),a=p.events.onData(o.cacheKey,i),I=()=>{y(),a()};return Q(),U.current={unmount:I},I},J=(o,y)=>{if(!y){q();let{queueKey:B,cacheKey:Z}=o,N=d.events.onRequestStart(B,C(o)),$=d.events.onResponseStart(B,L(o)),oe=d.events.onUploadProgress(B,h),pe=d.events.onDownloadProgress(B,f),le=d.events.onResponse(Z,v(o)),ue=()=>{pe(),oe(),N(),$(),le()};return S.current.set(B,{unmount:ue}),ue}let a=d.events.onRemoveById(y,A),I=d.events.onRequestStartById(y,C(o)),W=d.events.onResponseStartById(y,L(o)),j=d.events.onResponseById(y,v(o)),G=d.events.onUploadProgressById(y,h),w=d.events.onDownloadProgressById(y,f),K=()=>{a(),w(),G(),I(),W(),j()};return S.current.set(y,{unmount:K}),K},Y=()=>{let{abortKey:o}=e;t.getAllRunningRequest().forEach(a=>{a.request.abortKey===o&&t.delete(a.request.queueKey,a.requestId,o)})};return Be(()=>{q(),Q()}),[{abort:Y,onSuccess:o=>{c.current=o},onError:o=>{n.current=o},onAbort:o=>{l.current=o},onOfflineError:o=>{R.current=o},onFinished:o=>{b.current=o},onRequestStart:o=>{x.current=o},onResponseStart:o=>{u.current=o},onDownloadProgress:o=>{E.current=o},onUploadProgress:o=>{T.current=o}},{addDataListener:ne,clearDataListener:Q,addLifecycleListeners:J,removeLifecycleListener:P,clearLifecycleListeners:q}]};var H={data:null,error:null,loading:!1,status:null,retries:0,timestamp:null};import{useRef as xe}from"react";import{useDidUpdate as Ee,useForceUpdate as Ne}from"@better-hooks/lifecycle";var he=e=>{let t=Object.prototype.toString.call(e);return Array.isArray(e)?!e.length:typeof e=="object"&&e!==null&&t==="[object Object]"?!Object.keys(e).length:!1},ce=(e,t)=>{let r=Object.prototype.toString.call(e),g=Object.prototype.toString.call(t),i=typeof e,p=typeof t,d=n=>i===n&&p===n,c=n=>r===n&&g===n;return r!==g?!1:e===null&&t===null||d("number")&&Number.isNaN(e)&&Number.isNaN(t)||he(e)&&he(t)?!0:Array.isArray(e)&&Array.isArray(t)?e.length!==t.length?!1:!e.some((n,l)=>!ce(n,t[l])):d("object")&&c("[object Object]")?Object.keys(e).length!==Object.keys(t).length?!1:!Object.entries(e).some(([n,l])=>!ce(l,t[n])):e instanceof Date&&t instanceof Date?+e==+t:e===t};var ie=e=>F(m({},e),{throttle:void 0,debounce:void 0});var X=(e,t)=>m({retries:(e==null?void 0:e.retries)||0,timestamp:+new Date,isFailed:!1,isCanceled:!1,isOffline:!1},t),de=(e,t)=>t?+new Date>+t+e:!0,Qe=(e,t,r)=>!de(e.cacheTime,r==null?void 0:r.details.timestamp)&&r?r:t?{data:t,details:X(),cacheTime:1e3,clearKey:e.client.cache.clearKey,garbageCollection:e.garbageCollection}:null,je=e=>e?new Date(e):null,fe=(e,t,r)=>{var l,R,b;let{client:g,cacheKey:i}=r,{cache:p}=g,d=p.get(i),c=Qe(r,e,d),n=t.hasRunningRequests(r.queueKey);return F(m({},H),{data:((l=c==null?void 0:c.data)==null?void 0:l[0])||H.data,error:((R=c==null?void 0:c.data)==null?void 0:R[1])||H.error,status:((b=c==null?void 0:c.data)==null?void 0:b[2])||H.status,retries:(c==null?void 0:c.details.retries)||H.retries,timestamp:je((c==null?void 0:c.details.timestamp)||H.timestamp),loading:n})};var ee=({request:e,dispatcher:t,initialData:r,deepCompare:g,dependencyTracking:i,defaultCacheEmitting:p=!0})=>{let{client:d,cacheKey:c,queueKey:n,cacheTime:l}=e,{cache:R,requestManager:b}=d,x=Ne(),u=xe(fe(r,t,e)),E=xe([]),T=()=>{let s=R.get(c);return de(l,(s==null?void 0:s.details.timestamp)||u.current.timestamp)},S=s=>{E.current.some(h=>s.includes(h))&&x()},U=s=>{E.current.includes(s)||E.current.push(s)};Ee(()=>{u.current.loading=t.hasRunningRequests(n);let s=fe(r,t,e),f=(r==null?void 0:r[0])===u.current.data,h=!!(u.current.data||u.current.error)&&!f,C=!h&&u.current.data,L=h&&!u.current.data;(C||L)&&(u.current=s)},[c,n],!0),Ee(()=>{(()=>{i||Object.keys(u.current).forEach(f=>U(f))})()},[i],!0);let P=(s,f)=>typeof g=="function"?g(s,f):g?ce(s,f):!1,q=s=>se(void 0,null,function*(){let f={data:s.data[0],error:s.data[1],status:s.data[2],retries:s.details.retries,timestamp:new Date(s.details.timestamp),loading:u.current.loading},h=Object.keys(f).filter(C=>{let L=C,v=u.current[L],A=f[L];return!P(v,A)});u.current=m(m({},u.current),f),S(h)}),O={setData:(s,f=p)=>{if(f){let h=u.current;R.set(e,[s,h.error,h.status],X(u.current))}else u.current.data=s,S(["data"])},setError:(s,f=p)=>{if(f){let h=u.current;R.set(e,[h.data,s,h.status],X(u.current,{isFailed:!!s}))}else u.current.error=s,S(["error"])},setLoading:(s,f=!0)=>{f?b.events.emitLoading(n,"",{queueKey:n,requestId:"",loading:s,isRetry:!1,isOffline:!1}):(u.current.loading=s,S(["loading"]))},setStatus:(s,f=p)=>{if(f){let h=u.current;R.set(e,[h.data,h.error,s],X(u.current))}else u.current.status=s,S(["status"])},setRetries:(s,f=p)=>{if(f){let h=u.current;R.set(e,[h.data,h.error,h.status],X(u.current,{retries:s}))}else u.current.retries=s,S(["retries"])},setTimestamp:(s,f=p)=>{if(f){let h=u.current;R.set(e,[h.data,h.error,h.status],X(u.current,{timestamp:+s}))}else u.current.timestamp=s,S(["timestamp"])}};return[u.current,O,{setRenderKey:U,setCacheData:q,getStaleStatus:T}]};import{useRef as z}from"react";import{useDidMount as Ve,useDidUpdate as Je,useForceUpdate as We}from"@better-hooks/lifecycle";var te=(e,{dependencyTracking:t})=>{let r=We(),g=z(null),i=z(null),p=z(null),d=z(null),c=z(null),n=z(null),l=z(qe),R=z([]),b=T=>{R.current.some(U=>T.includes(U))&&r()},x=T=>{R.current.includes(T)||R.current.push(T)};Je(()=>{(()=>{t||Object.keys(l.current).forEach(S=>x(S))})()},[t],!0);let u={setData:T=>{l.current.data=T,b(["data"])},setConnected:T=>{l.current.connected=T,b(["data"])},setConnecting:T=>{l.current.connecting=T,b(["data"])},setTimestamp:T=>{l.current.timestamp=T,b(["timestamp"])}},E={onOpen:T=>{g.current=T},onClose:T=>{i.current=T},onError:T=>{p.current=T},onConnecting:T=>{d.current=T},onReconnecting:T=>{c.current=T},onReconnectingStop:T=>{n.current=T}};return Ve(()=>{let T=e.events.onError(s=>{var f;(f=p.current)==null||f.call(p,s)}),S=e.events.onConnecting(()=>{var s;u.setConnecting(!0),(s=d.current)==null||s.call(d)}),U=e.events.onOpen(()=>{var s;u.setConnected(!0),(s=g.current)==null||s.call(g)}),P=e.events.onClose(()=>{var s;u.setConnected(!1),(s=i.current)==null||s.call(i)}),q=e.events.onReconnecting(s=>{var f;(f=c.current)==null||f.call(c,s)}),O=e.events.onReconnectingStop(s=>{var f;(f=n.current)==null||f.call(n,s)});return()=>{T(),S(),U(),P(),q(),O()}}),[l.current,u,E,{setRenderKey:x}]};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 ve=Ge.createContext([{},()=>null]),yn=({children:e,config:t})=>{let[r,g]=He(t||{}),i=ze(()=>[r,g],[r]);return Xe(ve.Provider,{value:i,children:e})},M=()=>$e(ve);var Fn=(e,t=D)=>{let[r]=M(),{dependencies:g=D.dependencies,disabled:i=D.disabled,dependencyTracking:p=D.dependencyTracking,revalidateOnMount:d=D.revalidateOnMount,initialData:c=D.initialData,refresh:n=D.refresh,refreshTime:l=D.refreshTime,refreshBlurred:R=D.refreshBlurred,refreshOnBlur:b=D.refreshOnBlur,refreshOnFocus:x=D.refreshOnFocus,refreshOnReconnect:u=D.refreshOnReconnect,bounce:E=D.bounce,bounceType:T=D.bounceType,bounceTime:S=D.bounceTime,bounceTimeout:U=D.bounceTimeout,deepCompare:P=D.deepCompare}=m(m(m({},D),r.useFetchConfig),t),q=JSON.stringify(e.dump()),O=Ue({delay:S}),s=_e({interval:S,timeout:U}),f=Ue({delay:l}),{cacheKey:h,queueKey:C,client:L}=e,{cache:v,fetchDispatcher:A,appManager:Q,loggerManager:ne}=L,J=Ye(ne.init("useFetch")).current,Y=T==="throttle"?s:O,o=T==="throttle"?s.throttle:O.debounce,[y,a,{setRenderKey:I,setCacheData:W,getStaleStatus:j}]=ee({logger:J,request:e,dispatcher:A,initialData:c,deepCompare:P,dependencyTracking:p}),[G,w]=_({logger:J,actions:a,request:e,dispatcher:A,setCacheData:W}),{addDataListener:K,addLifecycleListeners:B,clearDataListener:Z}=w,N=()=>{i?J.debug("Cannot add to fetch queue",{disabled:i}):(J.debug("Fetching data"),A.add(e))};function $(){!n||f.debounce(()=>{let k=!Q.isFocused,re=A.hasRunningRequests(e.queueKey),me=A.getIsActiveQueue(e.queueKey),ae=re||me;(k&&R&&!ae||!k&&!ae)&&(N(),J.debug("Performing refresh request")),$()})}let oe=k=>{k&&k instanceof ke?v.revalidate(et(k)):k&&!(k instanceof ke)&&v.revalidate(k)},pe=k=>{k&&Array.isArray(k)?k.forEach(oe):k&&!Array.isArray(k)?oe(k):(N(),$())},le=()=>{let k=j(),re=A.getIsActiveQueue(C);(d||k&&!re)&&N()},ue=()=>{E?(J.debug(`Bounce request with ${T}`,{queueKey:C,request:e}),o(()=>N())):N()};return Te(()=>{K(e),B(e);let k=Q.events.onFocus(()=>{x&&(N(),$())}),re=Q.events.onBlur(()=>{b&&(N(),$())}),me=Q.events.onOnline(()=>{u&&(N(),$())}),ae=v.events.onRevalidate(h,N);return()=>{Z(),k(),re(),me(),ae()}},[q],!0),Ze(le),Te(ue,[q,i,...g]),Te($,[q,...g,i,n,l],!0),F(m(m({get data(){return I("data"),y.data},get error(){return I("error"),y.error},get loading(){return I("loading"),y.loading},get status(){return I("status"),y.status},get retries(){return I("retries"),y.retries},get timestamp(){return I("timestamp"),y.timestamp},bounce:ie(Y)},a),G),{revalidate:pe})};var wn=(e,t)=>{if(t){let r=Date.now()-+t;return r<e?e-r:e}return e};import{DateInterval as tt}from"@hyper-fetch/core";var D={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,bounceTimeout:200,deepCompare:!0};import{useMemo as nt,useRef as Oe}from"react";import{Request as Ce,getRequestKey as rt,requestSendRequest as st}from"@hyper-fetch/core";import{useDidMount as ot}from"@better-hooks/lifecycle";import{useDebounce as ut,useThrottle as at}from"@better-hooks/performance";var pr=(e,t=ye)=>{let[r]=M(),g=nt(()=>m(m(m({},ye),r.useSubmitConfig),t),[JSON.stringify(r.useSubmitConfig),JSON.stringify(t)]),{disabled:i,dependencyTracking:p,initialData:d,bounce:c,bounceType:n,bounceTime:l,bounceTimeout:R,deepCompare:b}=g,{client:x}=e,{cache:u,submitDispatcher:E,loggerManager:T}=x,S=Oe(T.init("useSubmit")).current,U=ut({delay:l}),P=at({interval:l,timeout:R}),q=Oe(()=>null),O=n==="throttle"?P:U,s=n==="throttle"?P.throttle:U.debounce,[f,h,{setRenderKey:C,setCacheData:L}]=ee({logger:S,request:e,dispatcher:E,initialData:d,deepCompare:b,dependencyTracking:p}),[v,A]=_({logger:S,actions:h,request:e,dispatcher:E,setCacheData:L}),{addDataListener:Q,addLifecycleListeners:ne}=A,J=a=>{let I=e.clone(a);if(i)return S.warning("Cannot submit request",{disabled:i,submitOptions:a}),Promise.resolve([null,new Error("Cannot submit request. Option 'disabled' is enabled"),0]);let W=()=>(Q(I),st(I,F(m({dispatcherType:"submit"},a),{onSettle:(j,G)=>{var w;ne(I,j),(w=a==null?void 0:a.onSettle)==null||w.call(a,j,G)}})));return new Promise(j=>{(()=>se(void 0,null,function*(){if(S.debug("Submitting request",{disabled:i,submitOptions:a}),c){let w=q.current;q.current=K=>{w(K),j(K)},s(()=>se(void 0,null,function*(){let K=q.current;q.current=()=>null;let B=yield W();K(B)}))}else{let w=yield W();j(w)}}))()})},Y=a=>{a&&a instanceof Ce?u.revalidate(rt(a)):a&&!(a instanceof Ce)&&u.revalidate(a)},o=a=>{!a||(a&&Array.isArray(a)?a.forEach(Y):a&&!Array.isArray(a)&&Y(a))},y={onSubmitSuccess:v.onSuccess,onSubmitError:v.onError,onSubmitFinished:v.onFinished,onSubmitRequestStart:v.onRequestStart,onSubmitResponseStart:v.onResponseStart,onSubmitDownloadProgress:v.onDownloadProgress,onSubmitUploadProgress:v.onUploadProgress,onSubmitOfflineError:v.onOfflineError,onSubmitAbort:v.onAbort};return ot(()=>{Q(e)}),F(m(m({submit:J,get data(){return C("data"),f.data},get error(){return C("error"),f.error},get submitting(){return C("loading"),f.loading},get status(){return C("status"),f.status},get retries(){return C("retries"),f.retries},get timestamp(){return C("timestamp"),f.timestamp},abort:v.abort},h),y),{bounce:ie(O),revalidate:o})};var ye={disabled:!1,dependencyTracking:!0,initialData:null,bounce:!1,bounceType:"debounce",bounceTime:400,bounceTimeout:200,deepCompare:!0};import{useState as De,useRef as ct}from"react";import{getRequestDispatcher as it}from"@hyper-fetch/core";import{useDidMount as pt,useDidUpdate as lt}from"@better-hooks/lifecycle";var Or=(e,t=ge)=>{let[r]=M(),{queueType:g="auto"}=m(m(m({},ge),r.useQueueConfig),t),{abortKey:i,queueKey:p,client:d}=e,{requestManager:c}=d,[n]=it(e,g),l=ct(null),[R,b]=De(!1),[x,u]=De([]),E=q=>q.map(O=>F(m({},O),{stopRequest:()=>n.stopRequest(p,O.requestId),startRequest:()=>n.startRequest(p,O.requestId),deleteRequest:()=>n.delete(p,O.requestId,i)})),T=(q,O)=>{u(s=>s.map(f=>f.requestId===q?m(m({},f),O):f))},S=()=>{let q=n.getQueue(p);b(q.stopped),u(E(q.requests))},U=q=>{b(q.stopped),u(E(q.requests))},P=()=>{var C;let q=n.events.onQueueChange(p,U),O=n.events.onQueueStatus(p,U),s=c.events.onDownloadProgress(p,(L,{requestId:v})=>{T(v,{downloading:L})}),f=c.events.onUploadProgress(p,(L,{requestId:v})=>{T(v,{uploading:L})}),h=()=>{O(),q(),s(),f()};return(C=l.current)==null||C.call(l),l.current=h,h};return pt(S),lt(P,[R,x,u,b],!0),{stopped:R,requests:x,stop:()=>n.stop(p),pause:()=>n.pause(p),start:()=>n.start(p)}};var ge={queueType:"auto"};import{useRef as mt}from"react";import{getRequestDispatcher as dt,Request as ft,getRequestKey as Tt}from"@hyper-fetch/core";var Jr=(e,t=Re)=>{let{cacheKey:r,client:g}=e,{cache:i,loggerManager:p}=g,d=mt(p.init("useCache")).current,[c]=dt(e),[n]=M(),{dependencyTracking:l,initialData:R,deepCompare:b}=m(m(m({},Re),n.useCacheConfig),t),[x,u,{setRenderKey:E,setCacheData:T}]=ee({logger:d,request:e,dispatcher:c,initialData:R,deepCompare:b,dependencyTracking:l}),[S]=_({logger:d,actions:u,request:e,dispatcher:c,setCacheData:T}),U=P=>{P instanceof ft?i.revalidate(Tt(P,!0)):P?i.revalidate(P):i.revalidate(r)};return F(m({get data(){return E("data"),x.data},get error(){return E("error"),x.error},get loading(){return E("loading"),x.loading},get status(){return E("status"),x.status},get retries(){return E("retries"),x.retries},get timestamp(){return E("timestamp"),x.timestamp},onCacheError:S.onError,onCacheSuccess:S.onSuccess,onCacheChange:S.onFinished},u),{revalidate:U})};var Re={dependencyTracking:!0,initialData:null,deepCompare:!0};import{useRef as Pe}from"react";import{useDidUpdate as yt,useWillUnmount as gt}from"@better-hooks/lifecycle";var ns=(e,t)=>{let[r]=M(),{dependencyTracking:g}=m(m({},r.useListener),t),i=Pe(null),p=Pe(null),[d,c,n,{setRenderKey:l}]=te(e.socket,{dependencyTracking:g}),R=()=>{var u;(u=p.current)==null||u[0]()},b=()=>{R(),p.current=e.listen((u,E)=>{var T;(T=i.current)==null||T.call(i,u,E),c.setData(u),c.setTimestamp(+new Date)})};yt(()=>{b()},[e],!0),gt(()=>{R()});let x={onEvent:u=>{i.current=u}};return F(m(m(m({get data(){return l("data"),d.data},get connected(){return l("connected"),d.connected},get connecting(){return l("connecting"),d.connecting},get timestamp(){return l("timestamp"),d.timestamp}},c),n),x),{listen:b})};import{useRef as Rt}from"react";var ls=(e,t)=>{let[r]=M(),{dependencyTracking:g}=m(m({},r.useEmitter),t),i=Rt(null),[p,d,c,{setRenderKey:n}]=te(e.socket,{dependencyTracking:g}),l={onEvent:b=>{i.current=b}},R=(...b)=>{var x;return d.setTimestamp(+new Date),(x=i.current)==null||x.call(i,e),e.emit(...b)};return F(m(m(m({get connected(){return n("connected"),p.connected},get connecting(){return n("connecting"),p.connecting},get timestamp(){return n("timestamp"),p.timestamp}},d),c),l),{emit:R,reconnect:e.socket.reconnect})};import{useRef as bt}from"react";import{useDidUpdate as St}from"@better-hooks/lifecycle";var hs=(e,t)=>{let[r]=M(),{dependencyTracking:g=!1,filter:i}=m(m({},r.useEventMessages),t),p=bt(null),[d,c,n,{setRenderKey:l}]=te(e,{dependencyTracking:g});St(()=>e.events.onListenerEvent((x,u)=>{var S;(i?(typeof i=="function"?()=>i(x,u):()=>i.includes(x.name))():!1)||((S=p.current)==null||S.call(p,x,u),c.setData(x),c.setTimestamp(+new Date))}),[e,i],!0);let R={onEvent:b=>{p.current=b}};return m(m(m({get data(){return l("data"),d.data},get connected(){return l("connected"),d.connected},get connecting(){return l("connecting"),d.connecting},get timestamp(){return l("timestamp"),d.timestamp}},c),n),R)};import{useState as Ie}from"react";import{useDidMount as ht}from"@better-hooks/lifecycle";var Cs=e=>{let[t,r]=Ie(e.appManager.isOnline),[g,i]=Ie(e.appManager.isFocused),p=()=>{let n=e.appManager.events.onOnline(()=>r(!0)),l=e.appManager.events.onOffline(()=>r(!1)),R=e.appManager.events.onFocus(()=>i(!0)),b=e.appManager.events.onBlur(()=>i(!1));return()=>{n(),l(),R(),b()}},d=n=>{e.appManager.setOnline(n)},c=n=>{e.appManager.setFocused(n)};return ht(p),{isOnline:t,isFocused:g,setOnline:d,setFocused:c}};export{yn as ConfigProvider,ie as getBounceData,X as getDetailsState,fe as getInitialState,wn as getRefreshTime,je as getTimestamp,Qe as getValidCacheData,qe as initialSocketState,H as initialState,he as isEmpty,ce as isEqual,de as isStaleCacheData,Cs as useAppManager,Jr as useCache,Re as useCacheDefaultOptions,M as useConfigProvider,ls as useEmitter,hs as useEventMessages,Fn as useFetch,D as useFetchDefaultOptions,ns as useListener,Or as useQueue,ge as useQueueDefaultOptions,_ as useRequestEvents,te as useSocketState,pr as useSubmit,ye as useSubmitDefaultOptions,ee as useTrackedState};
|
|
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};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|