@better-typed/react-hyper-fetch
Advanced tools
Comparing version 0.0.34 to 0.0.35
@@ -1,6 +0,16 @@ | ||
import { FetchCommandInstance, ExtractFetchReturn, ExtractResponse, ExtractError, CacheValueType, QueueLoadingEventType, FetchProgressType, FetchBuilderInstance, DateInterval, ExtractClientOptions, QueueDumpValueType, RequiredKeys } from "@better-typed/hyper-fetch"; | ||
// TBD - suspense | ||
declare const useFetch: <T extends FetchCommandInstance>(command: T, { dependencies, disabled, dependencyTracking, revalidateOnMount, initialData, refresh, refreshTime, refreshBlurred, refreshOnTabBlur, refreshOnTabFocus, refreshOnReconnect, debounce, debounceTime, | ||
// suspense = useFetchDefaultOptions.suspense | ||
shouldThrow }?: UseFetchOptionsType<T>) => UseFetchReturnType<T>; | ||
import { FetchCommandInstance, ExtractFetchReturn, ExtractResponse, ExtractError, CacheValueType, QueueLoadingEventType, FetchProgressType, ExtractClientOptions, FetchBuilderInstance, DateInterval, QueueDumpValueType, RequiredKeys } from "@better-typed/hyper-fetch"; | ||
declare const useFetch: <T extends FetchCommandInstance>(command: T, { dependencies, disabled, dependencyTracking, revalidateOnMount, initialData, refresh, refreshTime, refreshBlurred, refreshOnTabBlur, refreshOnTabFocus, refreshOnReconnect, debounce, debounceTime, deepCompare }?: UseFetchOptionsType<T>) => UseFetchReturnType<T>; | ||
/** | ||
* Check if value is empty | ||
* @param value any object or primitive | ||
* @returns true when value is empty | ||
*/ | ||
declare const isEmpty: (value: unknown) => boolean; | ||
/** | ||
* Allow to deep compare any passed values | ||
* @param firstValue unknown | ||
* @param secondValue unknown | ||
* @returns true when elements are equal | ||
*/ | ||
declare const isEqual: (firstValue: unknown, secondValue: unknown) => boolean; | ||
type UseDependentStateType<DataType = any, ErrorType = any> = { | ||
@@ -31,2 +41,8 @@ data: null | DataType; | ||
}; | ||
type OnRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
type OnProgressCallbackType = (progress: FetchProgressType) => void; | ||
type UseFetchOptionsType<T extends FetchCommandInstance> = { | ||
@@ -47,15 +63,14 @@ dependencies?: any[]; | ||
suspense?: boolean; | ||
shouldThrow?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
type UseFetchReturnType<T extends FetchCommandInstance> = Omit<UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, "data"> & { | ||
data: null | ExtractResponse<T>; | ||
type UseFetchReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onRequest: (callback: OnFetchRequestCallbackType) => void; | ||
onSuccess: (callback: OnFetchSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnFetchErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFetchFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onRequest: (callback: OnRequestCallbackType) => void; | ||
onSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnProgressCallbackType) => void; | ||
isRefreshed: boolean; | ||
@@ -67,9 +82,3 @@ isRefreshingError: boolean; | ||
}; | ||
type OnFetchRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnFetchSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnFetchErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnFetchFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnFetchStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
type OnFetchProgressCallbackType = (progress: FetchProgressType) => void; | ||
declare const useFetchDefaultOptions: { | ||
declare const useFetchDefaultOptions$0: { | ||
dependencies: never[]; | ||
@@ -88,7 +97,5 @@ disabled: boolean; | ||
debounceTime: number; | ||
shouldThrow: boolean; | ||
deepCompare: boolean; | ||
}; | ||
declare const useSubmit: <T extends FetchCommandInstance>(command: T, { disabled, dependencyTracking, initialData, debounce, debounceTime, | ||
// suspense = useSubmitDefaultOptions.suspense, | ||
shouldThrow }?: UseSubmitOptionsType<T>) => UseSubmitReturnType<T>; | ||
declare const useSubmit: <T extends FetchCommandInstance>(command: T, { disabled, dependencyTracking, initialData, debounce, debounceTime, deepCompare }?: UseSubmitOptionsType<T>) => UseSubmitReturnType<T>; | ||
type UseSubmitOptionsType<T extends FetchCommandInstance> = { | ||
@@ -104,25 +111,20 @@ disabled?: boolean; | ||
dependencyTracking?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
type UseSubmitReturnType<T extends FetchCommandInstance> = Omit<UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, "refreshError" | "loading"> & { | ||
onSubmitRequest: (callback: OnSubmitRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSubmitSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnSubmitErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnSubmitFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
revalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onSubmitRequest: (callback: OnRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnProgressCallbackType) => void; | ||
submit: (...parameters: Parameters<T["send"]>) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
submitting: boolean; | ||
isStale: boolean; | ||
isDebouncing: boolean; | ||
invalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
}; | ||
type OnSubmitRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnSubmitSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnSubmitErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnSubmitFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnSubmitStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
type OnSubmitProgressCallbackType = (progress: FetchProgressType) => void; | ||
declare const useSubmitDefaultOptions: { | ||
@@ -138,2 +140,3 @@ disabled: boolean; | ||
invalidate: never[]; | ||
deepCompare: boolean; | ||
}; | ||
@@ -156,6 +159,7 @@ declare const useQueue: <Command extends FetchCommandInstance>(command: Command, options?: UseQueueOptions) => { | ||
declare const useQueueDefaultOptions: RequiredKeys<UseQueueOptions>; | ||
declare const useCache: <T extends FetchCommandInstance>(command: T, { dependencyTracking, initialData }?: UseCacheOptionsType<T>) => UseCacheReturnType<T>; | ||
declare const useCache: <T extends FetchCommandInstance>(command: T, { dependencyTracking, initialData, deepCompare }?: UseCacheOptionsType<T>) => UseCacheReturnType<T>; | ||
declare const useCacheDefaultOptions: { | ||
dependencyTracking: boolean; | ||
initialData: null; | ||
deepCompare: boolean; | ||
}; | ||
@@ -165,17 +169,13 @@ type UseCacheOptionsType<T extends FetchCommandInstance> = { | ||
initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["response"] | null; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
type UseCacheReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & UseDependentStateActions<ExtractResponse<T>, ExtractError<T>> & { | ||
onSuccess: (callback: OnCacheSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnCacheErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnCacheFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onChange: (callback: OnCacheChangeCallbackType<ExtractFetchReturn<T>>) => void; | ||
type UseCacheReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onCacheSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onCacheError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onCacheChange: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
isStale: boolean; | ||
isRefreshingError: boolean; | ||
revalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
invalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
}; | ||
type OnCacheRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnCacheSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnCacheErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnCacheFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnCacheChangeCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type UseAppManagerType = { | ||
@@ -186,2 +186,2 @@ isFocused: boolean; | ||
declare const useAppManager: <B extends FetchBuilderInstance>(builder: B) => UseAppManagerType; | ||
export { useFetch, UseFetchOptionsType, UseFetchReturnType, OnFetchRequestCallbackType, OnFetchSuccessCallbackType, OnFetchErrorCallbackType, OnFetchFinishedCallbackType, OnFetchStartCallbackType, OnFetchProgressCallbackType, useFetchDefaultOptions, useSubmit, UseSubmitOptionsType, UseSubmitReturnType, OnSubmitRequestCallbackType, OnSubmitSuccessCallbackType, OnSubmitErrorCallbackType, OnSubmitFinishedCallbackType, OnSubmitStartCallbackType, OnSubmitProgressCallbackType, useSubmitDefaultOptions, useQueue, UseQueueOptions, QueueRequest, useQueueDefaultOptions, useCache, useCacheDefaultOptions, UseCacheOptionsType, UseCacheReturnType, OnCacheRequestCallbackType, OnCacheSuccessCallbackType, OnCacheErrorCallbackType, OnCacheFinishedCallbackType, OnCacheChangeCallbackType, useAppManager }; | ||
export { useFetch, UseFetchOptionsType, UseFetchReturnType, useFetchDefaultOptions$0 as useFetchDefaultOptions, useSubmit, UseSubmitOptionsType, UseSubmitReturnType, useSubmitDefaultOptions, useQueue, UseQueueOptions, QueueRequest, useQueueDefaultOptions, useCache, useCacheDefaultOptions, UseCacheOptionsType, UseCacheReturnType, useAppManager, isEmpty, isEqual }; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("@better-typed/react-lifecycle-hooks"),t=require("@better-typed/hyper-fetch");function n(e,r,t,n,s,a,i){try{var o=e[a](i),u=o.value}catch(e){return void t(e)}o.done?r(u):Promise.resolve(u).then(n,s)}function s(e){return function(){var r=this,t=arguments;return new Promise((function(s,a){var i=e.apply(r,t);function o(e){n(i,s,a,o,u,"next",e)}function u(e){n(i,s,a,o,u,"throw",e)}o(void 0)}))}}function a(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function i(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,s,a=[],i=!0,o=!1;try{for(t=t.call(e);!(i=(n=t.next()).done)&&(a.push(n.value),!r||a.length!==r);i=!0);}catch(e){o=!0,s=e}finally{try{i||null==t.return||t.return()}finally{if(o)throw s}}return a}}(e,r)||function(e,r){if(e){if("string"==typeof e)return a(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?a(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var o=Object.prototype,u=o.hasOwnProperty,c="function"==typeof Symbol?Symbol:{},l=c.iterator||"@@iterator",d=c.asyncIterator||"@@asyncIterator",f=c.toStringTag||"@@toStringTag";function p(e,r,t,n){var s=r&&r.prototype instanceof g?r:g,a=Object.create(s.prototype),i=new P(n||[]);return a._invoke=function(e,r,t){var n="suspendedStart";return function(s,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===s)throw a;return q()}for(t.method=s,t.arg=a;;){var i=t.delegate;if(i){var o=j(i,t);if(o){if(o===v)continue;return o}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var u=h(e,r,t);if("normal"===u.type){if(n=t.done?"completed":"suspendedYield",u.arg===v)continue;return{value:u.arg,done:t.done}}"throw"===u.type&&(n="completed",t.method="throw",t.arg=u.arg)}}}(e,t,i),a}function h(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}var v={};function g(){}function m(){}function y(){}var b={};b[l]=function(){return this};var O=Object.getPrototypeOf,w=O&&O(O(T([])));w&&w!==o&&u.call(w,l)&&(b=w);var x=y.prototype=g.prototype=Object.create(b);function R(e){["next","throw","return"].forEach((function(r){e[r]=function(e){return this._invoke(r,e)}}))}function D(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===m||"GeneratorFunction"===(r.displayName||r.name))}function E(e,r){function t(n,s,a,i){var o=h(e[n],e,s);if("throw"!==o.type){var c=o.arg,l=c.value;return l&&"object"==typeof l&&u.call(l,"__await")?r.resolve(l.__await).then((function(e){t("next",e,a,i)}),(function(e){t("throw",e,a,i)})):r.resolve(l).then((function(e){c.value=e,a(c)}),(function(e){return t("throw",e,a,i)}))}i(o.arg)}var n;this._invoke=function(e,s){function a(){return new r((function(r,n){t(e,s,r,n)}))}return n=n?n.then(a,a):a()}}function j(e,r){var t=e.iterator[r.method];if(undefined===t){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=undefined,j(e,r),"throw"===r.method))return v;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var n=h(t,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,v;var s=n.arg;return s?s.done?(r[e.resultName]=s.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=undefined),r.delegate=null,v):s:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,v)}function S(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function k(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function P(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(S,this),this.reset(!0)}function T(e){if(e){var r=e[l];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var t=-1,n=function r(){for(;++t<e.length;)if(u.call(e,t))return r.value=e[t],r.done=!1,r;return r.value=undefined,r.done=!0,r};return n.next=n}}return{next:q}}function q(){return{value:undefined,done:!0}}m.prototype=x.constructor=y,y.constructor=m,y[f]=m.displayName="GeneratorFunction",R(E.prototype),E.prototype[d]=function(){return this},R(x),x[f]="Generator",x[l]=function(){return this},x.toString=function(){return"[object Generator]"},P.prototype={constructor:P,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=undefined,this.done=!1,this.delegate=null,this.method="next",this.arg=undefined,this.tryEntries.forEach(k),!e)for(var r in this)"t"===r.charAt(0)&&u.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=undefined)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function t(t,n){return a.type="throw",a.arg=e,r.next=t,n&&(r.method="next",r.arg=undefined),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var s=this.tryEntries[n],a=s.completion;if("root"===s.tryLoc)return t("end");if(s.tryLoc<=this.prev){var i=u.call(s,"catchLoc"),o=u.call(s,"finallyLoc");if(i&&o){if(this.prev<s.catchLoc)return t(s.catchLoc,!0);if(this.prev<s.finallyLoc)return t(s.finallyLoc)}else if(i){if(this.prev<s.catchLoc)return t(s.catchLoc,!0)}else{if(!o)throw new Error("try statement without catch or finally");if(this.prev<s.finallyLoc)return t(s.finallyLoc)}}}},abrupt:function(e,r){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc<=this.prev&&u.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var s=n;break}}s&&("break"===e||"continue"===e)&&s.tryLoc<=r&&r<=s.finallyLoc&&(s=null);var a=s?s.completion:{};return a.type=e,a.arg=r,s?(this.method="next",this.next=s.finallyLoc,v):this.complete(a)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),v},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),k(t),v}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var s=n.arg;k(t)}return s}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:T(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=undefined),v}};var L={wrap:p,isGeneratorFunction:D,AsyncIterator:E,mark:function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,y):(e.__proto__=y,f in e||(e[f]="GeneratorFunction")),e.prototype=Object.create(x),e},awrap:function(e){return{__await:e}},async:function(e,r,t,n,s){void 0===s&&(s=Promise);var a=new E(p(e,r,t,n),s);return D(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},keys:function(e){var r=[];for(var t in e)r.push(t);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},values:T},F={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function M(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var K=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,C=(e,r)=>!r||!!e&&+new Date>+r+e;function N(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function Q(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?N(Object(t),!0).forEach((function(r){M(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):N(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var U=(e,r,t)=>{var n,s,a,i;return Q(Q({},F),{},{data:(null==r||null===(n=r.response)||void 0===n?void 0:n[0])||F.data,error:(null==r||null===(s=r.response)||void 0===s?void 0:s[1])||F.error,status:(null==r||null===(a=r.response)||void 0===a?void 0:a[2])||F.status,retries:(null==r?void 0:r.retries)||F.retries,timestamp:(i=(null==r?void 0:r.timestamp)||F.timestamp,i?new Date(i):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:null!=t?t:F.loading})};function _(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function I(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?_(Object(t),!0).forEach((function(r){M(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):_(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var A=(t,n,a,o)=>{var u,c=t.builder,l=t.cacheKey,d=t.queueKey,f=c.appManager,p=c.fetchQueue,h=c.cache,v=i(e.useState(!1),2),g=v[0],m=v[1],y=i(e.useState(+new Date),2)[1],b=e.useRef(U(t,(u=n)?{response:u,retries:0,timestamp:+new Date,isRefreshed:!1}:null)),O=e.useRef([]),w=e=>{O.current.find((r=>e.includes(r)))&&y(+new Date)};r.useDidUpdate((()=>{(function(){var e=s(L.mark((function e(){var r,s,i,o;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c.cache.get(l);case 2:return r=e.sent,s=r||K(n),e.next=6,a.get(d);case 6:i=e.sent,o=b.current.loading||!!i.requests.length,b.current=U(t,s,o),y(+new Date),m(!0);case 11:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}})()()}),[...o],!0),r.useDidMount((()=>{var e=f.events.onFocus((()=>{b.current.isFocused=!0,w(["isFocused"])})),r=f.events.onBlur((()=>{b.current.isFocused=!1,w(["isFocused"])})),t=f.events.onOnline((()=>{b.current.isOnline=!0,w(["isOnline"])})),n=f.events.onOffline((()=>{b.current.isOnline=!1,w(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var x,R,D,E,j,S,k,P,T,q,F={setCacheData:(q=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=6;break}return e.next=4,h.set(I({cache:t.cache,cacheKey:l},r));case 4:e.next=9;break;case 6:n={data:r.response[0],error:r.response[1],status:r.response[2],retries:r.retries,timestamp:new Date(r.timestamp),retryError:r.retryError,refreshError:r.refreshError,isRefreshed:r.isRefreshed,loading:!1},b.current=I(I({},b.current),n),w(Object.keys(n));case 9:case"end":return e.stop()}}),e)}))),function(e){return q.apply(this,arguments)}),setData:(T=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[r,n.error,n.status],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.data=r,w(["data"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return T.apply(this,arguments)}),setError:(P=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.error=r,w(["error"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return P.apply(this,arguments)}),setLoading:(k=s(L.mark((function e(r){var t=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:t.length>1&&void 0!==t[1]&&!t[1]?(b.current.loading=r,w(["loading"])):p.events.setLoading(l,{isLoading:r,isRetry:!1});case 2:case"end":return e.stop()}}),e)}))),function(e){return k.apply(this,arguments)}),setStatus:(S=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,r],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.status=r,w(["status"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return S.apply(this,arguments)}),setRefreshed:(j=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:n.retries,isRefreshed:r});case 5:e.next=9;break;case 7:b.current.isRefreshed=r,w(["isRefreshed"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return j.apply(this,arguments)}),setRefreshError:(E=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:b.current.refreshError=r,w(["refreshError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return E.apply(this,arguments)}),setRetryError:(D=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:b.current.retryError=r,w(["retryError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return D.apply(this,arguments)}),setRetries:(R=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:r,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.retries=r,w(["retries"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return R.apply(this,arguments)}),setTimestamp:(x=s(L.mark((function e(r){var n,s=arguments;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s.length>1&&void 0!==s[1]&&!s[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:n.retries,isRefreshed:n.isRefreshed,timestamp:r?+r:void 0});case 5:e.next=9;break;case 7:b.current.timestamp=r,w(["timestamp"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return x.apply(this,arguments)})};return[b.current,F,e=>{O.current.push(e)},g]},J=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var s=()=>{n.current.timer&&clearTimeout(n.current.timer),n.current.timer=null},a=e=>{s(),n.current.timer=setTimeout((()=>{n.current.timer=null,e()}),n.current.time)};return r.useWillUnmount(s),{debounce:a,resetDebounce:s,active:!!n.current.timer}},B=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var s=()=>{n.current.timer&&clearInterval(n.current.timer),n.current.timer=null},a=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;s(),n.current.timer=setInterval((()=>{e()}),r<=0?n.current.time:r)};return r.useWillUnmount(s),{interval:a,resetInterval:s,active:!!n.current.timer}},G={dependencies:[],disabled:!1,dependencyTracking:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:t.DateInterval.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,debounce:!1,debounceTime:400,shouldThrow:!1};function W(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function Y(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?W(Object(t),!0).forEach((function(r){M(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):W(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var $={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,invalidate:[]};function z(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function H(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?z(Object(t),!0).forEach((function(r){M(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):z(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var V={queueType:"auto"};function X(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function Z(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?X(Object(t),!0).forEach((function(r){M(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):X(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var ee={dependencyTracking:!0,initialData:null};exports.useAppManager=t=>{var n=i(e.useState(t.appManager.isOnline),2),s=n[0],a=n[1],o=i(e.useState(t.appManager.isFocused),2),u=o[0],c=o[1];return r.useDidMount((()=>{var e=t.appManager.events.onOnline((()=>a(!0))),r=t.appManager.events.onOffline((()=>a(!1))),n=t.appManager.events.onFocus((()=>c(!0))),s=t.appManager.events.onBlur((()=>c(!1)));return()=>{e(),r(),n(),s()}})),{isOnline:s,isFocused:u}},exports.useCache=function(n){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:ee,o=a.dependencyTracking,u=void 0===o?ee.dependencyTracking:o,c=a.initialData,l=void 0===c?ee.initialData:c,d=n.cacheTime,f=n.cacheKey,p=n.queueKey,h=n.builder,v=n.dump(),g=h.cache,m=h.fetchQueue,y=h.loggerManager,b=e.useRef(y.init("useCache")).current,O=A(n,l,m,[JSON.stringify(v)]),w=i(O,4),x=w[0],R=w[1],D=w[2],E=w[3],j=e.useRef(null),S=e.useRef(null),k=e.useRef(null),P=e.useRef(null),T=e=>{if(e){var r,t,n,s=e[2]||0,a=!(!e[0]||e[1]),i=!!(!e[1]&&s>=200&&s<=400);if(a||i)null==j||null===(t=j.current)||void 0===t||t.call(j,e[0]);else null==S||null===(n=S.current)||void 0===n||n.call(S,e[1]);null==k||null===(r=k.current)||void 0===r||r.call(k,e)}else b.debug("No response to perform callbacks")},q=function(){var e=s(L.mark((function e(r){return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return b.debug("Received new data"),T(r.response),e.next=4,R.setCacheData(r,!1);case 4:return e.next=6,R.setLoading(!1,!1);case 6:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),F=function(){var e=s(L.mark((function e(r,t,n){return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return b.debug("Received equal data event"),T(r.response),e.next=4,R.setRefreshed(t,!1);case 4:return e.next=6,R.setTimestamp(new Date(n),!1);case 6:return e.next=8,R.setLoading(!1,!1);case 8:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),M=e=>{e&&e instanceof t.FetchCommand?g.events.revalidate("/".concat(t.getCommandKey(e,!0),"/")):e?g.events.revalidate(e):g.events.revalidate(f)},K=e=>{var r=e.isLoading;R.setLoading(r,!1)},N=()=>{var e=m.events.onLoading(p,K),r=g.events.get(f,q),t=g.events.getEqualData(f,F);return()=>{e(),r(),t()}},Q=()=>{u||Object.keys(x).forEach((e=>D(e)))};return r.useDidUpdate((()=>(Q(),N())),[JSON.stringify(v)],!0),r.useDidUpdate((()=>{E&&T([x.data,x.error,x.status])}),[JSON.stringify(v),E],!0),Z(Z({get data(){return D("data"),x.data},get error(){return D("error"),x.error},get loading(){return D("loading"),x.loading},get status(){return D("status"),x.status},get retryError(){return D("retryError"),x.retryError},get refreshError(){return D("refreshError"),x.refreshError},get isRefreshed(){return D("isRefreshed"),x.isRefreshed},get retries(){return D("retries"),x.retries},get timestamp(){return D("timestamp"),x.timestamp},get isOnline(){return D("isOnline"),x.isOnline},get isFocused(){return D("isFocused"),x.isFocused},get isRefreshingError(){return D("error"),D("isRefreshed"),!!x.error&&x.isRefreshed},get isStale(){return D("timestamp"),C(d,x.timestamp)},onSuccess:e=>{j.current=e},onError:e=>{S.current=e},onFinished:e=>{k.current=e},onChange:e=>{P.current=e}},R),{},{revalidate:M})},exports.useCacheDefaultOptions=ee,exports.useFetch=function(n){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:G,o=a.dependencies,u=void 0===o?G.dependencies:o,c=a.disabled,l=void 0===c?G.disabled:c,d=a.dependencyTracking,f=void 0===d?G.dependencyTracking:d,p=a.revalidateOnMount,h=void 0===p?G.revalidateOnMount:p,v=a.initialData,g=void 0===v?G.initialData:v,m=a.refresh,y=void 0===m?G.refresh:m,b=a.refreshTime,O=void 0===b?G.refreshTime:b,w=a.refreshBlurred,x=void 0===w?G.refreshBlurred:w,R=a.refreshOnTabBlur,D=void 0===R?G.refreshOnTabBlur:R,E=a.refreshOnTabFocus,j=void 0===E?G.refreshOnTabFocus:E,S=a.refreshOnReconnect,k=void 0===S?G.refreshOnReconnect:S,P=a.debounce,T=void 0===P?G.debounce:P,q=a.debounceTime,F=void 0===q?G.debounceTime:q,M=a.shouldThrow,K=void 0===M?G.shouldThrow:M,N=n.cacheTime,Q=n.cacheKey,U=n.queueKey,_=n.builder,I=n.dump(),W=J(F),Y=B(O),$=_.cache,z=_.fetchQueue,H=_.appManager,V=_.commandManager,X=_.loggerManager,Z=e.useRef(X.init("useFetch")).current,ee=A(n,g,z,[JSON.stringify(I)]),re=i(ee,4),te=re[0],ne=re[1],se=re[2],ae=re[3],ie=e.useRef(null),oe=e.useRef(null),ue=e.useRef(null),ce=e.useRef(null),le=e.useRef(null),de=e.useRef(null),fe=e.useRef(null),pe=e.useRef(null),he=()=>{l?Z.debug("Cannot add to fetch queue",{disabled:l}):(Z.debug("Adding request to fetch queue"),z.add(n))},ve=()=>{Y.resetInterval();var e=te.timestamp,r=O;if(e){var t=+new Date-+e;r=t>=0&&t<O?O-t:0}y&&(Z.debug("Starting refresh counter, request will be send in ".concat(r,"ms")),Y.interval(s(L.mark((function e(){var r,t,n,s;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,z.get(U);case 2:r=e.sent,t=!H.isFocused,n=t&&x||H.isFocused,!(s=!!r.requests.length)&&n?(Z.debug("Performing refresh request",{hasQueueElements:s,canRefresh:n,isFocused:H.isFocused,timestamp:te.timestamp}),he(),Y.resetInterval()):Z.debug("Cannot trigger refresh request",{hasQueueElements:s,canRefresh:n,isFocused:H.isFocused,timestamp:te.timestamp});case 8:case"end":return e.stop()}}),e)}))),r))},ge=e=>{if(e){var r,t,n,s=e[2]||0,a=!(!e[0]||e[1]),i=!!(!e[1]&&s>=200&&s<=400);if(a||i)null==oe||null===(t=oe.current)||void 0===t||t.call(oe,e[0]);else if(null==ue||null===(n=ue.current)||void 0===n||n.call(ue,e[1]),K)throw{message:"Fetching Error.",error:e[1]};null==ce||null===(r=ce.current)||void 0===r||r.call(ce,e)}else Z.debug("No response to perform callbacks")},me=function(){var e=s(L.mark((function e(r){return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return Z.debug("Received new data"),ge(r.response),e.next=4,ne.setCacheData(r,!1);case 4:return e.next=6,ne.setLoading(!1,!1);case 6:ve();case 7:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),ye=function(){var e=s(L.mark((function e(r,t,n){return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return Z.debug("Received equal data event"),ge(r.response),e.next=4,ne.setRefreshed(t,!1);case 4:return e.next=6,ne.setTimestamp(new Date(n),!1);case 6:return e.next=8,ne.setLoading(!1,!1);case 8:ve();case 9:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),be=e=>{var r,t=e.isLoading,n=e.isRetry;ne.setLoading(t,!1),null===(r=ie.current)||void 0===r||r.call(ie,{isRetry:n})},Oe=()=>{he()},we=e=>{e&&e instanceof t.FetchCommand?$.events.revalidate("/".concat(t.getCommandKey(e,!0),"/")):e?$.events.revalidate(e):Oe()},xe=e=>{var r;null==fe||null===(r=fe.current)||void 0===r||r.call(fe,e)},Re=e=>{var r;null==pe||null===(r=pe.current)||void 0===r||r.call(pe,e)},De=e=>{var r;null==le||null===(r=le.current)||void 0===r||r.call(le,e)},Ee=e=>{var r;null==le||null===(r=le.current)||void 0===r||r.call(le,e)},je=()=>{var e=H.events.onFocus((()=>{j&&he(),ve()})),r=H.events.onBlur((()=>{D&&he(),ve()})),t=H.events.onOnline((()=>{k&&he(),ve()})),n=_.appManager.events.onOffline((()=>{ve()})),s=V.events.onDownloadProgress(U,xe),a=V.events.onUploadProgress(U,Re),i=V.events.onRequestStart(U,De),o=V.events.onResponseStart(U,Ee),u=z.events.onLoading(U,be),c=$.events.get(Q,me),l=$.events.getEqualData(Q,ye),d=$.events.onRevalidate(Q,Oe);return()=>{e(),r(),t(),n(),s(),a(),i(),o(),u(),c(),l(),d()}},Se=()=>{f||Object.keys(te).forEach((e=>se(e)))};return r.useDidUpdate((()=>{var e=ae&&C(N,te.timestamp);(h||e)&&he()}),[ae]),r.useDidUpdate((()=>(Se(),je())),[JSON.stringify(I)],!0),r.useDidUpdate((()=>{ae&&ge([te.data,te.error,te.status])}),[JSON.stringify(I),ae],!0),r.useDidUpdate((()=>{!z.getRequestCount(U)&&T?(Z.debug("Debouncing request",{queueKey:U,command:n}),W.debounce((()=>he()))):he()}),[JSON.stringify(I),...u,l]),r.useDidUpdate((()=>{ve()}),[JSON.stringify(I),...u,l,y,O],!0),{get data(){return se("data"),te.data},get error(){return se("error"),te.error},get loading(){return se("loading"),te.loading},get status(){return se("status"),te.status},get retryError(){return se("retryError"),te.retryError},get refreshError(){return se("refreshError"),te.refreshError},get isRefreshed(){return se("isRefreshed"),te.isRefreshed},get retries(){return se("retries"),te.retries},get timestamp(){return se("timestamp"),te.timestamp},get isOnline(){return se("isOnline"),te.isOnline},get isFocused(){return se("isFocused"),te.isFocused},get isRefreshingError(){return se("error"),se("isRefreshed"),!!te.error&&te.isRefreshed},get isStale(){return se("timestamp"),C(N,te.timestamp)},onRequest:e=>{ie.current=e},onSuccess:e=>{oe.current=e},onError:e=>{ue.current=e},onFinished:e=>{ce.current=e},onRequestStart:e=>{le.current=e},onResponseStart:e=>{de.current=e},onDownloadProgress:e=>{fe.current=e},onUploadProgress:e=>{pe.current=e},actions:ne,isDebouncing:W.active,refresh:we}},exports.useFetchDefaultOptions=G,exports.useQueue=function(n){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:V,o=a.queueType,u=void 0===o?V.queueType:o,c=n.queueKey,l=n.builder,d=l.commandManager,f=e.useRef(t.getCommandQueue(n,u)),p=e.useState(!0),h=i(p,2),v=h[0],g=h[1],m=e.useState(!1),y=i(m,2),b=y[0],O=y[1],w=e.useState([]),x=i(w,2),R=x[0],D=x[1],E=function(){var e=s(L.mark((function e(){var r,t;return L.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=f.current,e.next=3,r.get(c);case 3:t=e.sent,O(t.stopped),D(t.requests),g(!1);case 7:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),j=()=>{var e=f.current,r=e.events.onQueueChange(c,(e=>{O(e.stopped),D([...e.requests])})),t=d.events.onDownloadProgress(c,((e,r)=>{var t=r.requestId;D((r=>r.map((r=>r.requestId===t?H(H({},r),{},{downloading:e}):r))))})),n=d.events.onDownloadProgress(c,((e,r)=>{var t=r.requestId;D((r=>r.map((r=>r.requestId===t?H(H({},r),{},{uploading:e}):r))))})),s=e.events.onQueueStatus(c,(e=>{O(e.stopped),D([...e.requests])}));return()=>{s(),r(),t(),n()}};return r.useDidMount((()=>{E()})),r.useDidUpdate((()=>j()),[b,R,D,O],!0),{connecting:v,stopped:b,requests:R,stopQueue:()=>f.current.stopQueue(c),pauseQueue:()=>f.current.pauseQueue(c),startQueue:()=>f.current.startQueue(c)}},exports.useQueueDefaultOptions=V,exports.useSubmit=function(n){var s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:$,a=s.disabled,o=void 0===a?$.disabled:a,u=s.dependencyTracking,c=void 0===u?$.dependencyTracking:u,l=s.initialData,d=void 0===l?$.initialData:l,f=s.debounce,p=void 0===f?$.debounce:f,h=s.debounceTime,v=void 0===h?$.debounceTime:h,g=s.shouldThrow,m=void 0===g?$.shouldThrow:g,y=n.cacheTime,b=n.cacheKey,O=n.queueKey,w=n.builder,x=n.invalidate,R=J(v),D=w.cache,E=w.submitQueue,j=w.commandManager,S=w.loggerManager,k=e.useRef(S.init("useSubmit")).current,P=n.dump(),T=A(n,d,E,[JSON.stringify(P)]),q=i(T,3),L=q[0],F=q[1],M=q[2],K=e.useRef(null),N=e.useRef(null),Q=e.useRef(null),U=e.useRef(null),_=e.useRef(null),I=e.useRef(null),B=e.useRef(null),G=e.useRef(null),W=function(){var e=arguments.length<=0?void 0:arguments[0];if(o)k.debug("Cannot add to submit queue",{disabled:o,options:e});else{if(k.debug("Adding request to submit queue",{disabled:o,options:e}),!p)return n.send(Y(Y({},e),{},{queueType:"submit"}));R.debounce((()=>{n.send(Y(Y({},e),{},{queueType:"submit"}))}))}},z=e=>{e&&(e&&e instanceof t.FetchCommand?D.events.revalidate("/".concat(t.getCommandKey(e,!0),"/")):D.events.revalidate(e))},H=e=>{if(e){var r,t,n,s=e[2]||0,a=!(!e[0]||e[1]),i=!!(!e[1]&&s>=200&&s<=400);if(a||i)null==N||null===(t=N.current)||void 0===t||t.call(N,e[0]),null==x||x.forEach((e=>z(e)));else if(null==Q||null===(n=Q.current)||void 0===n||n.call(Q,e[1]),m)throw{message:"Fetching Error.",error:e[1]};null==U||null===(r=U.current)||void 0===r||r.call(U,e)}else k.debug("No response to perform callbacks")},V=e=>{H(e.response),F.setLoading(!1,!1),F.setCacheData(e,!1)},X=(e,r,t)=>{H(e.response),F.setRefreshed(r,!1),F.setTimestamp(new Date(t),!1),F.setLoading(!1,!1)},Z=e=>{var r,t=e.isLoading,n=e.isRetry;F.setLoading(t,!1),null===(r=K.current)||void 0===r||r.call(K,{isRetry:n})},ee=e=>{var r;null==B||null===(r=B.current)||void 0===r||r.call(B,e)},re=e=>{var r;null==G||null===(r=G.current)||void 0===r||r.call(G,e)},te=e=>{var r;null==_||null===(r=_.current)||void 0===r||r.call(_,e)},ne=e=>{var r;null==_||null===(r=_.current)||void 0===r||r.call(_,e)},se=()=>{var e=j.events.onDownloadProgress(O,ee),r=j.events.onUploadProgress(O,re),t=j.events.onRequestStart(O,te),n=j.events.onResponseStart(O,ne),s=E.events.onLoading(O,Z),a=D.events.get(b,V),i=D.events.getEqualData(b,X);return()=>{e(),r(),t(),n(),s(),a(),i()}},ae=()=>{c||Object.keys(L).forEach((e=>M(e)))};return r.useDidUpdate((()=>(ae(),se())),[JSON.stringify(P)],!0),{submit:W,get data(){return M("data"),L.data},get error(){return M("error"),L.error},get submitting(){return M("loading"),L.loading},get status(){return M("status"),L.status},get retryError(){return M("retryError"),L.retryError},get retries(){return M("retries"),L.retries},get timestamp(){return M("timestamp"),L.timestamp},get isOnline(){return M("isOnline"),L.isOnline},get isFocused(){return M("isFocused"),L.isFocused},get isStale(){return C(y,L.timestamp)},onSubmitRequest:e=>{K.current=e},onSubmitSuccess:e=>{N.current=e},onSubmitError:e=>{Q.current=e},onSubmitFinished:e=>{U.current=e},onSubmitRequestStart:e=>{_.current=e},onSubmitResponseStart:e=>{I.current=e},onSubmitDownloadProgress:e=>{B.current=e},onSubmitUploadProgress:e=>{G.current=e},actions:F,isDebouncing:!1,isRefreshed:!1,revalidate:z}},exports.useSubmitDefaultOptions=$; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("@better-typed/react-lifecycle-hooks"),t=require("@better-typed/hyper-fetch");function n(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function a(e,r,t,n,a,o,s){try{var i=e[o](s),u=i.value}catch(e){return void t(e)}i.done?r(u):Promise.resolve(u).then(n,a)}function o(e){return function(){var r=this,t=arguments;return new Promise((function(n,o){var s=e.apply(r,t);function i(e){a(s,n,o,i,u,"next",e)}function u(e){a(s,n,o,i,u,"throw",e)}i(void 0)}))}}function s(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function i(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,a,o=[],s=!0,i=!1;try{for(t=t.call(e);!(s=(n=t.next()).done)&&(o.push(n.value),!r||o.length!==r);s=!0);}catch(e){i=!0,a=e}finally{try{s||null==t.return||t.return()}finally{if(i)throw a}}return o}}(e,r)||function(e,r){if(e){if("string"==typeof e)return s(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?s(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var u=Object.prototype,c=u.hasOwnProperty,l="function"==typeof Symbol?Symbol:{},d=l.iterator||"@@iterator",p=l.asyncIterator||"@@asyncIterator",f=l.toStringTag||"@@toStringTag";function h(e,r,t,n){var a=r&&r.prototype instanceof m?r:m,o=Object.create(a.prototype),s=new T(n||[]);return o._invoke=function(e,r,t){var n="suspendedStart";return function(a,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===a)throw o;return F()}for(t.method=a,t.arg=o;;){var s=t.delegate;if(s){var i=E(s,t);if(i){if(i===g)continue;return i}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var u=v(e,r,t);if("normal"===u.type){if(n=t.done?"completed":"suspendedYield",u.arg===g)continue;return{value:u.arg,done:t.done}}"throw"===u.type&&(n="completed",t.method="throw",t.arg=u.arg)}}}(e,t,s),o}function v(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}var g={};function m(){}function y(){}function b(){}var O={};O[d]=function(){return this};var w=Object.getPrototypeOf,j=w&&w(w(q([])));j&&j!==u&&c.call(j,d)&&(O=j);var x=b.prototype=m.prototype=Object.create(O);function R(e){["next","throw","return"].forEach((function(r){e[r]=function(e){return this._invoke(r,e)}}))}function D(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===y||"GeneratorFunction"===(r.displayName||r.name))}function S(e,r){function t(n,a,o,s){var i=v(e[n],e,a);if("throw"!==i.type){var u=i.arg,l=u.value;return l&&"object"==typeof l&&c.call(l,"__await")?r.resolve(l.__await).then((function(e){t("next",e,o,s)}),(function(e){t("throw",e,o,s)})):r.resolve(l).then((function(e){u.value=e,o(u)}),(function(e){return t("throw",e,o,s)}))}s(i.arg)}var n;this._invoke=function(e,a){function o(){return new r((function(r,n){t(e,a,r,n)}))}return n=n?n.then(o,o):o()}}function E(e,r){var t=e.iterator[r.method];if(undefined===t){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=undefined,E(e,r),"throw"===r.method))return g;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return g}var n=v(t,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,g;var a=n.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=undefined),r.delegate=null,g):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,g)}function k(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function P(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function T(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(k,this),this.reset(!0)}function q(e){if(e){var r=e[d];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var t=-1,n=function r(){for(;++t<e.length;)if(c.call(e,t))return r.value=e[t],r.done=!1,r;return r.value=undefined,r.done=!0,r};return n.next=n}}return{next:F}}function F(){return{value:undefined,done:!0}}y.prototype=x.constructor=b,b.constructor=y,b[f]=y.displayName="GeneratorFunction",R(S.prototype),S.prototype[p]=function(){return this},R(x),x[f]="Generator",x[d]=function(){return this},x.toString=function(){return"[object Generator]"},T.prototype={constructor:T,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=undefined,this.done=!1,this.delegate=null,this.method="next",this.arg=undefined,this.tryEntries.forEach(P),!e)for(var r in this)"t"===r.charAt(0)&&c.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=undefined)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function t(t,n){return o.type="throw",o.arg=e,r.next=t,n&&(r.method="next",r.arg=undefined),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var a=this.tryEntries[n],o=a.completion;if("root"===a.tryLoc)return t("end");if(a.tryLoc<=this.prev){var s=c.call(a,"catchLoc"),i=c.call(a,"finallyLoc");if(s&&i){if(this.prev<a.catchLoc)return t(a.catchLoc,!0);if(this.prev<a.finallyLoc)return t(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return t(a.catchLoc,!0)}else{if(!i)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return t(a.finallyLoc)}}}},abrupt:function(e,r){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc<=this.prev&&c.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var a=n;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var o=a?a.completion:{};return o.type=e,o.arg=r,a?(this.method="next",this.next=a.finallyLoc,g):this.complete(o)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),g},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),P(t),g}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var a=n.arg;P(t)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:q(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=undefined),g}};var C={wrap:h,isGeneratorFunction:D,AsyncIterator:S,mark:function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,b):(e.__proto__=b,f in e||(e[f]="GeneratorFunction")),e.prototype=Object.create(x),e},awrap:function(e){return{__await:e}},async:function(e,r,t,n,a){void 0===a&&(a=Promise);var o=new S(h(e,r,t,n),a);return D(r)?o:o.next().then((function(e){return e.done?e.value:o.next()}))},keys:function(e){var r=[];for(var t in e)r.push(t);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},values:q},L=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var a=()=>{n.current.timer&&clearTimeout(n.current.timer),n.current.timer=null},o=e=>{a(),n.current.timer=setTimeout((()=>{n.current.timer=null,e()}),n.current.time)};return r.useWillUnmount(a),{debounce:o,resetDebounce:a,active:!!n.current.timer}},M=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var a=()=>{n.current.timer&&clearInterval(n.current.timer),n.current.timer=null},o=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;a(),n.current.timer=setInterval((()=>{e()}),r<=0?n.current.time:r)};return r.useWillUnmount(a),{interval:o,resetInterval:a,active:!!n.current.timer}},K=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,N=(e,r)=>!r||!!e&&+new Date>+r+e,A=e=>{var r=Object.prototype.toString.call(e);return Array.isArray(e)?!e.length:"object"==typeof e&&null!==e&&"[object Object]"===r&&!Object.keys(e).length},I=(e,r)=>{var t,n=Object.prototype.toString.call(e),a=Object.prototype.toString.call(r),o=typeof e,s=typeof r,u=e=>o===e&&s===e;return n===a&&(null===e&&null===r||(!!(u("number")&&Number.isNaN(e)&&Number.isNaN(r))||(!(!A(e)||!A(r))||(Array.isArray(e)&&Array.isArray(r)?e.length===r.length&&!e.some(((e,t)=>!I(e,r[t]))):u("object")&&(n===(t="[object Object]")&&a===t)?Object.keys(e).length===Object.keys(r).length&&!Object.entries(e).some((e=>{var t=i(e,2),n=t[0],a=t[1];return!I(a,r[n])})):e instanceof Date&&r instanceof Date?+e==+r:e===r))))},Q={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function _(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function U(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?_(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):_(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var B=(e,r,t)=>{var n,a,o,s;return U(U({},Q),{},{data:(null==r||null===(n=r.response)||void 0===n?void 0:n[0])||Q.data,error:(null==r||null===(a=r.response)||void 0===a?void 0:a[1])||Q.error,status:(null==r||null===(o=r.response)||void 0===o?void 0:o[2])||Q.status,retries:(null==r?void 0:r.retries)||Q.retries,timestamp:(s=(null==r?void 0:r.timestamp)||Q.timestamp,s?new Date(s):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:null!=t?t:Q.loading})};function G(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function J(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?G(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):G(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var z=(t,n,a,s)=>{var u,c=t.builder,l=t.cacheKey,d=t.queueKey,p=c.appManager,f=c.fetchQueue,h=c.cache,v=i(e.useState(!1),2),g=v[0],m=v[1],y=i(e.useState(+new Date),2)[1],b=e.useRef(B(t,(u=n)?{response:u,retries:0,timestamp:+new Date,isRefreshed:!1}:null)),O=e.useRef([]),w=e=>{O.current.find((r=>e.includes(r)))&&y(+new Date)};r.useDidUpdate((()=>{(function(){var e=o(C.mark((function e(){var r,o,s,i;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c.cache.get(l);case 2:return r=e.sent,o=r||K(n),e.next=6,a.get(d);case 6:s=e.sent,i=b.current.loading||!!s.requests.length,b.current=B(t,o,i),y(+new Date),m(!0);case 11:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}})()()}),[...s],!0),r.useDidMount((()=>{var e=p.events.onFocus((()=>{b.current.isFocused=!0,w(["isFocused"])})),r=p.events.onBlur((()=>{b.current.isFocused=!1,w(["isFocused"])})),t=p.events.onOnline((()=>{b.current.isOnline=!0,w(["isOnline"])})),n=p.events.onOffline((()=>{b.current.isOnline=!1,w(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var j,x,R,D,S,E,k,P,T,q,F={setCacheData:(q=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=6;break}return e.next=4,h.set(J({cache:t.cache,cacheKey:l},r));case 4:e.next=9;break;case 6:n={data:r.response[0],error:r.response[1],status:r.response[2],retries:r.retries,timestamp:new Date(r.timestamp),retryError:r.retryError,refreshError:r.refreshError,isRefreshed:r.isRefreshed,loading:!1},b.current=J(J({},b.current),n),w(Object.keys(n));case 9:case"end":return e.stop()}}),e)}))),function(e){return q.apply(this,arguments)}),setData:(T=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[r,n.error,n.status],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.data=r,w(["data"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return T.apply(this,arguments)}),setError:(P=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.error=r,w(["error"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return P.apply(this,arguments)}),setLoading:(k=o(C.mark((function e(r){var t=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:t.length>1&&void 0!==t[1]&&!t[1]?(b.current.loading=r,w(["loading"])):f.events.setLoading(l,{isLoading:r,isRetry:!1});case 2:case"end":return e.stop()}}),e)}))),function(e){return k.apply(this,arguments)}),setStatus:(E=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,r],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.status=r,w(["status"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return E.apply(this,arguments)}),setRefreshed:(S=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:n.retries,isRefreshed:r});case 5:e.next=9;break;case 7:b.current.isRefreshed=r,w(["isRefreshed"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return S.apply(this,arguments)}),setRefreshError:(D=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:b.current.refreshError=r,w(["refreshError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return D.apply(this,arguments)}),setRetryError:(R=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:b.current.retryError=r,w(["retryError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return R.apply(this,arguments)}),setRetries:(x=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:r,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:b.current.retries=r,w(["retries"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return x.apply(this,arguments)}),setTimestamp:(j=o(C.mark((function e(r){var n,a=arguments;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a.length>1&&void 0!==a[1]&&!a[1]){e.next=7;break}return n=b.current,e.next=5,h.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:n.retries,isRefreshed:n.isRefreshed,timestamp:r?+r:void 0});case 5:e.next=9;break;case 7:b.current.timestamp=r,w(["timestamp"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return j.apply(this,arguments)})};return[b.current,F,e=>{O.current.push(e)},g]},W=t=>{var n=t.command,a=t.queue,s=t.dependencyTracking,u=t.initialData,c=t.logger,l=t.deepCompare,d=n.builder,p=n.cacheKey,f=n.queueKey,h=d.commandManager,v=d.cache,g=n.dump(),m=i(e.useState(!1),2),y=m[0],b=m[1],O=i(z(n,u,a,[JSON.stringify(g)]),4),w=O[0],j=O[1],x=O[2],R=O[3],D=e.useRef(null),S=e.useRef(null),E=e.useRef(null),k=e.useRef(null),P=e.useRef(null),T=e.useRef(null),q=e.useRef(null),F=e.useRef(null),L=e.useRef(null),M=e=>{if(e){var r,t,n,a=e[2]||0,o=!(!e[0]||e[1]),s=!!(!e[1]&&a>=200&&a<=400);if(o||s)c.debug("Performing success callback",{status:a,hasSuccessState:o,hasSuccessStatus:s,response:e,hasCallback:!(null==S||!S.current)}),null===(t=S.current)||void 0===t||t.call(S,e[0]);else c.debug("Performing error callback",{status:a,hasSuccessState:o,hasSuccessStatus:s,response:e,hasCallback:!(null==E||!E.current)}),null===(n=E.current)||void 0===n||n.call(E,e[1]);null===(r=k.current)||void 0===r||r.call(k,e)}else c.debug("No response to perform callbacks")},K=function(){var e=o(C.mark((function e(r){var t;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(c.debug("Received new data"),M(r.response),t="function"==typeof l?l:I,!(!!l&&t(r.response,[w.data,w.error,w.status]))){e.next=9;break}return e.next=7,j.setTimestamp(r.timestamp?new Date(r.timestamp):null);case 7:e.next=11;break;case 9:return e.next=11,j.setCacheData(r,!1);case 11:return e.next=13,j.setLoading(!1,!1);case 13:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),N=e=>{var r,t=e.isLoading,n=e.isRetry;j.setLoading(t,!1),null===(r=D.current)||void 0===r||r.call(D,{isRetry:n})},A=e=>{var r;null==q||null===(r=q.current)||void 0===r||r.call(q,e)},Q=e=>{var r;null==F||null===(r=F.current)||void 0===r||r.call(F,e)},_=e=>{var r;null==P||null===(r=P.current)||void 0===r||r.call(P,e)},U=e=>{var r;null==P||null===(r=P.current)||void 0===r||r.call(P,e)};return r.useDidUpdate((()=>{return s||Object.keys(w).forEach((e=>x(e))),r=h.events.onDownloadProgress(f,A),t=h.events.onUploadProgress(f,Q),n=h.events.onRequestStart(f,_),o=h.events.onResponseStart(f,U),i=a.events.onLoading(f,N),u=v.events.get(p,K),c=()=>{r(),t(),n(),o(),i(),u()},null===(e=L.current)||void 0===e||e.call(L),L.current=c,c;var e,r,t,n,o,i,u,c}),[JSON.stringify(g)],!0),r.useDidUpdate((()=>{R&&!y&&(M([w.data,w.error,w.status]),b(!0))}),[JSON.stringify(g),R],!0),[w,{actions:j,onRequest:e=>{D.current=e},onSuccess:e=>{S.current=e},onError:e=>{E.current=e},onFinished:e=>{k.current=e},onRequestStart:e=>{P.current=e},onResponseStart:e=>{T.current=e},onDownloadProgress:e=>{q.current=e},onUploadProgress:e=>{F.current=e}},{setRenderKey:x,initialized:R}]};function Y(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function $(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?Y(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):Y(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}t.DateInterval.hour;var H={dependencies:[],disabled:!1,dependencyTracking:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:t.DateInterval.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,debounce:!1,debounceTime:400,deepCompare:!0};function V(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function X(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?V(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):V(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var Z={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,invalidate:[],deepCompare:!0};function ee(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function re(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ee(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ee(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var te={queueType:"auto"};function ne(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function ae(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ne(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ne(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var oe={dependencyTracking:!0,initialData:null,deepCompare:!0};exports.isEmpty=A,exports.isEqual=I,exports.useAppManager=t=>{var n=i(e.useState(t.appManager.isOnline),2),a=n[0],o=n[1],s=i(e.useState(t.appManager.isFocused),2),u=s[0],c=s[1];return r.useDidMount((()=>{var e=t.appManager.events.onOnline((()=>o(!0))),r=t.appManager.events.onOffline((()=>o(!1))),n=t.appManager.events.onFocus((()=>c(!0))),a=t.appManager.events.onBlur((()=>c(!1)));return()=>{e(),r(),n(),a()}})),{isOnline:a,isFocused:u}},exports.useCache=function(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:oe,a=n.dependencyTracking,o=void 0===a?oe.dependencyTracking:a,s=n.initialData,u=void 0===s?oe.initialData:s,c=n.deepCompare,l=void 0===c?oe.deepCompare:c,d=r.cacheTime,p=r.cacheKey,f=r.builder,h=f.cache,v=f.fetchQueue,g=f.loggerManager,m=e.useRef(g.init("useCache")).current,y=W({command:r,queue:v,dependencyTracking:o,initialData:u,logger:m,deepCompare:l}),b=i(y,3),O=b[0],w=b[1],j=b[2].setRenderKey,x=e=>{e&&e instanceof t.FetchCommand?h.events.revalidate("/".concat(t.getCommandKey(e,!0),"/")):e?h.events.revalidate(e):h.events.revalidate(p)},R={actions:w.actions,onCacheError:w.onError,onCacheSuccess:w.onSuccess,onCacheChange:w.onFinished};return ae(ae({get data(){return j("data"),O.data},get error(){return j("error"),O.error},get loading(){return j("loading"),O.loading},get status(){return j("status"),O.status},get retryError(){return j("retryError"),O.retryError},get refreshError(){return j("refreshError"),O.refreshError},get isRefreshed(){return j("isRefreshed"),O.isRefreshed},get retries(){return j("retries"),O.retries},get timestamp(){return j("timestamp"),O.timestamp},get isOnline(){return j("isOnline"),O.isOnline},get isFocused(){return j("isFocused"),O.isFocused},get isRefreshingError(){return j("error"),j("isRefreshed"),!!O.error&&O.isRefreshed},get isStale(){return j("timestamp"),N(d,O.timestamp)}},R),{},{invalidate:x})},exports.useCacheDefaultOptions=oe,exports.useFetch=function(n){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:H,s=a.dependencies,u=void 0===s?H.dependencies:s,c=a.disabled,l=void 0===c?H.disabled:c,d=a.dependencyTracking,p=void 0===d?H.dependencyTracking:d,f=a.revalidateOnMount,h=void 0===f?H.revalidateOnMount:f,v=a.initialData,g=void 0===v?H.initialData:v,m=a.refresh,y=void 0===m?H.refresh:m,b=a.refreshTime,O=void 0===b?H.refreshTime:b,w=a.refreshBlurred,j=void 0===w?H.refreshBlurred:w,x=a.refreshOnTabBlur,R=void 0===x?H.refreshOnTabBlur:x,D=a.refreshOnTabFocus,S=void 0===D?H.refreshOnTabFocus:D,E=a.refreshOnReconnect,k=void 0===E?H.refreshOnReconnect:E,P=a.debounce,T=void 0===P?H.debounce:P,q=a.debounceTime,F=void 0===q?H.debounceTime:q,K=a.deepCompare,A=void 0===K?H.deepCompare:K,I=n.cacheTime,Q=n.cacheKey,_=n.queueKey,U=n.builder,B=n.dump(),G=L(F),J=M(O),z=e.useRef(null),Y=U.cache,V=U.fetchQueue,X=U.appManager,Z=U.loggerManager,ee=e.useRef(Z.init("useFetch")).current,re=W({command:n,queue:V,dependencyTracking:p,initialData:g,logger:ee,deepCompare:A}),te=i(re,3),ne=te[0],ae=te[1],oe=te[2],se=oe.setRenderKey,ie=oe.initialized,ue=()=>{l?ee.debug("Cannot add to fetch queue",{disabled:l}):(ee.debug("Adding request to fetch queue"),V.add(n))},ce=()=>{J.resetInterval();var e=ne.timestamp,r=O;if(e){var t=+new Date-+e;r=t>=0&&t<O?O-t:0}y&&(ee.debug("Starting refresh counter, request will be send in ".concat(r,"ms")),J.interval(o(C.mark((function e(){var r,t,n,a;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,V.get(_);case 2:r=e.sent,t=!X.isFocused,n=t&&j||X.isFocused,!(a=!!r.requests.length)&&n?(ee.debug("Performing refresh request",{hasQueueElements:a,canRefresh:n,isFocused:X.isFocused,timestamp:ne.timestamp}),ue(),J.resetInterval()):ee.debug("Cannot trigger refresh request",{hasQueueElements:a,canRefresh:n,isFocused:X.isFocused,timestamp:ne.timestamp});case 8:case"end":return e.stop()}}),e)}))),r))},le=()=>{ue()},de=e=>{e&&e instanceof t.FetchCommand?Y.events.revalidate("/".concat(t.getCommandKey(e,!0),"/")):e?Y.events.revalidate(e):le()},pe=()=>{var e,r=X.events.onFocus((()=>{S&&ue(),ce()})),t=X.events.onBlur((()=>{R&&ue(),ce()})),n=X.events.onOnline((()=>{k&&ue(),ce()})),a=U.appManager.events.onOffline((()=>{ce()})),o=Y.events.onRevalidate(Q,le),s=()=>{r(),t(),n(),a(),o()};return null===(e=z.current)||void 0===e||e.call(z),z.current=s,s};return r.useDidUpdate((()=>{var e=ie&&N(I,ne.timestamp);(h||e)&&ue()}),[ie]),r.useDidUpdate(pe,[JSON.stringify(B)],!0),r.useDidUpdate((()=>{!V.getRequestCount(_)&&T?(ee.debug("Debouncing request",{queueKey:_,command:n}),G.debounce((()=>ue()))):ue()}),[JSON.stringify(B),...u,l]),r.useDidUpdate((()=>{ce()}),[JSON.stringify(B),...u,l,y,O],!0),$($({get data(){return se("data"),ne.data},get error(){return se("error"),ne.error},get loading(){return se("loading"),ne.loading},get status(){return se("status"),ne.status},get retryError(){return se("retryError"),ne.retryError},get refreshError(){return se("refreshError"),ne.refreshError},get isRefreshed(){return se("isRefreshed"),ne.isRefreshed},get retries(){return se("retries"),ne.retries},get timestamp(){return se("timestamp"),ne.timestamp},get isOnline(){return se("isOnline"),ne.isOnline},get isFocused(){return se("isFocused"),ne.isFocused},get isRefreshingError(){return se("error"),se("isRefreshed"),!!ne.error&&ne.isRefreshed},get isStale(){return se("timestamp"),N(I,ne.timestamp)}},ae),{},{isDebouncing:G.active,refresh:de})},exports.useFetchDefaultOptions=H,exports.useQueue=function(n){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:te,s=a.queueType,u=void 0===s?te.queueType:s,c=n.queueKey,l=n.builder,d=l.commandManager,p=e.useRef(t.getCommandQueue(n,u)),f=e.useRef(null),h=e.useState(!0),v=i(h,2),g=v[0],m=v[1],y=e.useState(!1),b=i(y,2),O=b[0],w=b[1],j=e.useState([]),x=i(j,2),R=x[0],D=x[1],S=function(){var e=o(C.mark((function e(){var r,t;return C.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=p.current,e.next=3,r.get(c);case 3:t=e.sent,w(t.stopped),D(t.requests),m(!1);case 7:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),E=()=>{var e,r=p.current,t=r.events.onQueueChange(c,(e=>{w(e.stopped),D([...e.requests])})),n=d.events.onDownloadProgress(c,((e,r)=>{var t=r.requestId;D((r=>r.map((r=>r.requestId===t?re(re({},r),{},{downloading:e}):r))))})),a=d.events.onDownloadProgress(c,((e,r)=>{var t=r.requestId;D((r=>r.map((r=>r.requestId===t?re(re({},r),{},{uploading:e}):r))))})),o=r.events.onQueueStatus(c,(e=>{w(e.stopped),D([...e.requests])})),s=()=>{o(),t(),n(),a()};return null===(e=f.current)||void 0===e||e.call(f),f.current=s,s};return r.useDidMount((()=>{S()})),r.useDidUpdate((()=>E()),[O,R,D,w],!0),{connecting:g,stopped:O,requests:R,stopQueue:()=>p.current.stopQueue(c),pauseQueue:()=>p.current.pauseQueue(c),startQueue:()=>p.current.startQueue(c)}},exports.useQueueDefaultOptions=te,exports.useSubmit=function(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Z,a=n.disabled,o=void 0===a?Z.disabled:a,s=n.dependencyTracking,u=void 0===s?Z.dependencyTracking:s,c=n.initialData,l=void 0===c?Z.initialData:c,d=n.debounce,p=void 0===d?Z.debounce:d,f=n.debounceTime,h=void 0===f?Z.debounceTime:f,v=n.deepCompare,g=void 0===v?Z.deepCompare:v,m=r.cacheTime,y=r.builder,b=L(h),O=y.cache,w=y.submitQueue,j=y.loggerManager,x=e.useRef(j.init("useSubmit")).current,R=W({command:r,queue:w,dependencyTracking:u,initialData:l,logger:x,deepCompare:g}),D=i(R,3),S=D[0],E=D[1],k=D[2].setRenderKey,P=function(){var e=arguments.length<=0?void 0:arguments[0];if(o)x.debug("Cannot add to submit queue",{disabled:o,options:e});else{if(x.debug("Adding request to submit queue",{disabled:o,options:e}),!p)return r.send(X(X({},e),{},{queueType:"submit"}));b.debounce((()=>{r.send(X(X({},e),{},{queueType:"submit"}))}))}},T=e=>{e&&(e&&e instanceof t.FetchCommand?O.events.revalidate("/".concat(t.getCommandKey(e,!0),"/")):O.events.revalidate(e))},q={actions:E.actions,onSubmitRequest:E.onRequest,onSubmitSuccess:E.onSuccess,onSubmitError:E.onError,onSubmitFinished:E.onFinished,onSubmitRequestStart:E.onRequestStart,onSubmitResponseStart:E.onResponseStart,onSubmitDownloadProgress:E.onDownloadProgress,onSubmitUploadProgress:E.onUploadProgress};return X(X({submit:P,get data(){return k("data"),S.data},get error(){return k("error"),S.error},get submitting(){return k("loading"),S.loading},get status(){return k("status"),S.status},get retryError(){return k("retryError"),S.retryError},get retries(){return k("retries"),S.retries},get timestamp(){return k("timestamp"),S.timestamp},get isOnline(){return k("isOnline"),S.isOnline},get isFocused(){return k("isFocused"),S.isFocused},get isStale(){return N(m,S.timestamp)}},q),{},{isDebouncing:!1,isRefreshed:!1,invalidate:T})},exports.useSubmitDefaultOptions=Z; | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,5 +0,19 @@ | ||
import { FetchCommandInstance, CacheValueType, ExtractResponse, ExtractError, ExtractFetchReturn, QueueLoadingEventType, FetchProgressType, DateInterval, QueueDumpValueType, ExtractClientOptions, RequiredKeys, FetchBuilderInstance } from '@better-typed/hyper-fetch'; | ||
import { FetchCommandInstance, CacheValueType, QueueLoadingEventType, FetchProgressType, ExtractResponse, ExtractError, ExtractFetchReturn, DateInterval, QueueDumpValueType, ExtractClientOptions, RequiredKeys, FetchBuilderInstance } from '@better-typed/hyper-fetch'; | ||
declare const useFetch: <T extends FetchCommandInstance>(command: T, { dependencies, disabled, dependencyTracking, revalidateOnMount, initialData, refresh, refreshTime, refreshBlurred, refreshOnTabBlur, refreshOnTabFocus, refreshOnReconnect, debounce, debounceTime, shouldThrow, }?: UseFetchOptionsType<T>) => UseFetchReturnType<T>; | ||
declare const useFetch: <T extends FetchCommandInstance>(command: T, { dependencies, disabled, dependencyTracking, revalidateOnMount, initialData, refresh, refreshTime, refreshBlurred, refreshOnTabBlur, refreshOnTabFocus, refreshOnReconnect, debounce, debounceTime, deepCompare, }?: UseFetchOptionsType<T>) => UseFetchReturnType<T>; | ||
/** | ||
* Check if value is empty | ||
* @param value any object or primitive | ||
* @returns true when value is empty | ||
*/ | ||
declare const isEmpty: (value: unknown) => boolean; | ||
/** | ||
* Allow to deep compare any passed values | ||
* @param firstValue unknown | ||
* @param secondValue unknown | ||
* @returns true when elements are equal | ||
*/ | ||
declare const isEqual: (firstValue: unknown, secondValue: unknown) => boolean; | ||
declare type UseDependentStateType<DataType = any, ErrorType = any> = { | ||
@@ -31,2 +45,9 @@ data: null | DataType; | ||
declare type OnRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
declare type OnSuccessCallbackType<DataType> = (data: DataType) => void; | ||
declare type OnErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
declare type OnFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
declare type OnStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
declare type OnProgressCallbackType = (progress: FetchProgressType) => void; | ||
declare type UseFetchOptionsType<T extends FetchCommandInstance> = { | ||
@@ -47,15 +68,14 @@ dependencies?: any[]; | ||
suspense?: boolean; | ||
shouldThrow?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
declare type UseFetchReturnType<T extends FetchCommandInstance> = Omit<UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, "data"> & { | ||
data: null | ExtractResponse<T>; | ||
declare type UseFetchReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onRequest: (callback: OnFetchRequestCallbackType) => void; | ||
onSuccess: (callback: OnFetchSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnFetchErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFetchFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onRequest: (callback: OnRequestCallbackType) => void; | ||
onSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnProgressCallbackType) => void; | ||
isRefreshed: boolean; | ||
@@ -67,8 +87,2 @@ isRefreshingError: boolean; | ||
}; | ||
declare type OnFetchRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
declare type OnFetchSuccessCallbackType<DataType> = (data: DataType) => void; | ||
declare type OnFetchErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
declare type OnFetchFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
declare type OnFetchStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
declare type OnFetchProgressCallbackType = (progress: FetchProgressType) => void; | ||
@@ -89,6 +103,6 @@ declare const useFetchDefaultOptions: { | ||
debounceTime: number; | ||
shouldThrow: boolean; | ||
deepCompare: boolean; | ||
}; | ||
declare const useSubmit: <T extends FetchCommandInstance>(command: T, { disabled, dependencyTracking, initialData, debounce, debounceTime, shouldThrow, }?: UseSubmitOptionsType<T>) => UseSubmitReturnType<T>; | ||
declare const useSubmit: <T extends FetchCommandInstance>(command: T, { disabled, dependencyTracking, initialData, debounce, debounceTime, deepCompare, }?: UseSubmitOptionsType<T>) => UseSubmitReturnType<T>; | ||
@@ -105,25 +119,20 @@ declare type UseSubmitOptionsType<T extends FetchCommandInstance> = { | ||
dependencyTracking?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
declare type UseSubmitReturnType<T extends FetchCommandInstance> = Omit<UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, "refreshError" | "loading"> & { | ||
onSubmitRequest: (callback: OnSubmitRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSubmitSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnSubmitErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnSubmitFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
revalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onSubmitRequest: (callback: OnRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnProgressCallbackType) => void; | ||
submit: (...parameters: Parameters<T["send"]>) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
submitting: boolean; | ||
isStale: boolean; | ||
isDebouncing: boolean; | ||
invalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
}; | ||
declare type OnSubmitRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
declare type OnSubmitSuccessCallbackType<DataType> = (data: DataType) => void; | ||
declare type OnSubmitErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
declare type OnSubmitFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
declare type OnSubmitStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
declare type OnSubmitProgressCallbackType = (progress: FetchProgressType) => void; | ||
@@ -140,2 +149,3 @@ declare const useSubmitDefaultOptions: { | ||
invalidate: never[]; | ||
deepCompare: boolean; | ||
}; | ||
@@ -162,3 +172,3 @@ | ||
declare const useCache: <T extends FetchCommandInstance>(command: T, { dependencyTracking, initialData, }?: UseCacheOptionsType<T>) => UseCacheReturnType<T>; | ||
declare const useCache: <T extends FetchCommandInstance>(command: T, { dependencyTracking, initialData, deepCompare, }?: UseCacheOptionsType<T>) => UseCacheReturnType<T>; | ||
@@ -168,2 +178,3 @@ declare const useCacheDefaultOptions: { | ||
initialData: null; | ||
deepCompare: boolean; | ||
}; | ||
@@ -174,17 +185,13 @@ | ||
initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["response"] | null; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
declare type UseCacheReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & UseDependentStateActions<ExtractResponse<T>, ExtractError<T>> & { | ||
onSuccess: (callback: OnCacheSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnCacheErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnCacheFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onChange: (callback: OnCacheChangeCallbackType<ExtractFetchReturn<T>>) => void; | ||
declare type UseCacheReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onCacheSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onCacheError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onCacheChange: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
isStale: boolean; | ||
isRefreshingError: boolean; | ||
revalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
invalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
}; | ||
declare type OnCacheRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
declare type OnCacheSuccessCallbackType<DataType> = (data: DataType) => void; | ||
declare type OnCacheErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
declare type OnCacheFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
declare type OnCacheChangeCallbackType<ResponseType> = (response: ResponseType) => void; | ||
@@ -197,2 +204,2 @@ declare type UseAppManagerType = { | ||
export { OnCacheChangeCallbackType, OnCacheErrorCallbackType, OnCacheFinishedCallbackType, OnCacheRequestCallbackType, OnCacheSuccessCallbackType, OnFetchErrorCallbackType, OnFetchFinishedCallbackType, OnFetchProgressCallbackType, OnFetchRequestCallbackType, OnFetchStartCallbackType, OnFetchSuccessCallbackType, OnSubmitErrorCallbackType, OnSubmitFinishedCallbackType, OnSubmitProgressCallbackType, OnSubmitRequestCallbackType, OnSubmitStartCallbackType, OnSubmitSuccessCallbackType, QueueRequest, UseCacheOptionsType, UseCacheReturnType, UseFetchOptionsType, UseFetchReturnType, UseQueueOptions, UseSubmitOptionsType, UseSubmitReturnType, useAppManager, useCache, useCacheDefaultOptions, useFetch, useFetchDefaultOptions, useQueue, useQueueDefaultOptions, useSubmit, useSubmitDefaultOptions }; | ||
export { QueueRequest, UseCacheOptionsType, UseCacheReturnType, UseFetchOptionsType, UseFetchReturnType, UseQueueOptions, UseSubmitOptionsType, UseSubmitReturnType, isEmpty, isEqual, useAppManager, useCache, useCacheDefaultOptions, useFetch, useFetchDefaultOptions, useQueue, useQueueDefaultOptions, useSubmit, useSubmitDefaultOptions }; |
@@ -1,6 +0,16 @@ | ||
import { FetchCommandInstance, ExtractFetchReturn, ExtractResponse, ExtractError, CacheValueType, QueueLoadingEventType, FetchProgressType, FetchBuilderInstance, DateInterval, ExtractClientOptions, QueueDumpValueType, RequiredKeys } from "@better-typed/hyper-fetch"; | ||
// TBD - suspense | ||
declare const useFetch: <T extends FetchCommandInstance>(command: T, { dependencies, disabled, dependencyTracking, revalidateOnMount, initialData, refresh, refreshTime, refreshBlurred, refreshOnTabBlur, refreshOnTabFocus, refreshOnReconnect, debounce, debounceTime, | ||
// suspense = useFetchDefaultOptions.suspense | ||
shouldThrow }?: UseFetchOptionsType<T>) => UseFetchReturnType<T>; | ||
import { FetchCommandInstance, ExtractFetchReturn, ExtractResponse, ExtractError, CacheValueType, QueueLoadingEventType, FetchProgressType, ExtractClientOptions, FetchBuilderInstance, DateInterval, QueueDumpValueType, RequiredKeys } from "@better-typed/hyper-fetch"; | ||
declare const useFetch: <T extends FetchCommandInstance>(command: T, { dependencies, disabled, dependencyTracking, revalidateOnMount, initialData, refresh, refreshTime, refreshBlurred, refreshOnTabBlur, refreshOnTabFocus, refreshOnReconnect, debounce, debounceTime, deepCompare }?: UseFetchOptionsType<T>) => UseFetchReturnType<T>; | ||
/** | ||
* Check if value is empty | ||
* @param value any object or primitive | ||
* @returns true when value is empty | ||
*/ | ||
declare const isEmpty: (value: unknown) => boolean; | ||
/** | ||
* Allow to deep compare any passed values | ||
* @param firstValue unknown | ||
* @param secondValue unknown | ||
* @returns true when elements are equal | ||
*/ | ||
declare const isEqual: (firstValue: unknown, secondValue: unknown) => boolean; | ||
type UseDependentStateType<DataType = any, ErrorType = any> = { | ||
@@ -31,2 +41,8 @@ data: null | DataType; | ||
}; | ||
type OnRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
type OnProgressCallbackType = (progress: FetchProgressType) => void; | ||
type UseFetchOptionsType<T extends FetchCommandInstance> = { | ||
@@ -47,15 +63,14 @@ dependencies?: any[]; | ||
suspense?: boolean; | ||
shouldThrow?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
type UseFetchReturnType<T extends FetchCommandInstance> = Omit<UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, "data"> & { | ||
data: null | ExtractResponse<T>; | ||
type UseFetchReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onRequest: (callback: OnFetchRequestCallbackType) => void; | ||
onSuccess: (callback: OnFetchSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnFetchErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFetchFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onRequest: (callback: OnRequestCallbackType) => void; | ||
onSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnProgressCallbackType) => void; | ||
isRefreshed: boolean; | ||
@@ -67,9 +82,3 @@ isRefreshingError: boolean; | ||
}; | ||
type OnFetchRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnFetchSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnFetchErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnFetchFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnFetchStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
type OnFetchProgressCallbackType = (progress: FetchProgressType) => void; | ||
declare const useFetchDefaultOptions: { | ||
declare const useFetchDefaultOptions$0: { | ||
dependencies: never[]; | ||
@@ -88,7 +97,5 @@ disabled: boolean; | ||
debounceTime: number; | ||
shouldThrow: boolean; | ||
deepCompare: boolean; | ||
}; | ||
declare const useSubmit: <T extends FetchCommandInstance>(command: T, { disabled, dependencyTracking, initialData, debounce, debounceTime, | ||
// suspense = useSubmitDefaultOptions.suspense, | ||
shouldThrow }?: UseSubmitOptionsType<T>) => UseSubmitReturnType<T>; | ||
declare const useSubmit: <T extends FetchCommandInstance>(command: T, { disabled, dependencyTracking, initialData, debounce, debounceTime, deepCompare }?: UseSubmitOptionsType<T>) => UseSubmitReturnType<T>; | ||
type UseSubmitOptionsType<T extends FetchCommandInstance> = { | ||
@@ -104,25 +111,20 @@ disabled?: boolean; | ||
dependencyTracking?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
type UseSubmitReturnType<T extends FetchCommandInstance> = Omit<UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, "refreshError" | "loading"> & { | ||
onSubmitRequest: (callback: OnSubmitRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSubmitSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnSubmitErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnSubmitFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
revalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onSubmitRequest: (callback: OnRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnProgressCallbackType) => void; | ||
submit: (...parameters: Parameters<T["send"]>) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
submitting: boolean; | ||
isStale: boolean; | ||
isDebouncing: boolean; | ||
invalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
}; | ||
type OnSubmitRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnSubmitSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnSubmitErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnSubmitFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnSubmitStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
type OnSubmitProgressCallbackType = (progress: FetchProgressType) => void; | ||
declare const useSubmitDefaultOptions: { | ||
@@ -138,2 +140,3 @@ disabled: boolean; | ||
invalidate: never[]; | ||
deepCompare: boolean; | ||
}; | ||
@@ -156,6 +159,7 @@ declare const useQueue: <Command extends FetchCommandInstance>(command: Command, options?: UseQueueOptions) => { | ||
declare const useQueueDefaultOptions: RequiredKeys<UseQueueOptions>; | ||
declare const useCache: <T extends FetchCommandInstance>(command: T, { dependencyTracking, initialData }?: UseCacheOptionsType<T>) => UseCacheReturnType<T>; | ||
declare const useCache: <T extends FetchCommandInstance>(command: T, { dependencyTracking, initialData, deepCompare }?: UseCacheOptionsType<T>) => UseCacheReturnType<T>; | ||
declare const useCacheDefaultOptions: { | ||
dependencyTracking: boolean; | ||
initialData: null; | ||
deepCompare: boolean; | ||
}; | ||
@@ -165,17 +169,13 @@ type UseCacheOptionsType<T extends FetchCommandInstance> = { | ||
initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["response"] | null; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
type UseCacheReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & UseDependentStateActions<ExtractResponse<T>, ExtractError<T>> & { | ||
onSuccess: (callback: OnCacheSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnCacheErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnCacheFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onChange: (callback: OnCacheChangeCallbackType<ExtractFetchReturn<T>>) => void; | ||
type UseCacheReturnType<T extends FetchCommandInstance> = UseDependentStateType<ExtractResponse<T>, ExtractError<T>> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onCacheSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onCacheError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onCacheChange: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
isStale: boolean; | ||
isRefreshingError: boolean; | ||
revalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
invalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
}; | ||
type OnCacheRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
type OnCacheSuccessCallbackType<DataType> = (data: DataType) => void; | ||
type OnCacheErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
type OnCacheFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type OnCacheChangeCallbackType<ResponseType> = (response: ResponseType) => void; | ||
type UseAppManagerType = { | ||
@@ -186,2 +186,2 @@ isFocused: boolean; | ||
declare const useAppManager: <B extends FetchBuilderInstance>(builder: B) => UseAppManagerType; | ||
export { useFetch, UseFetchOptionsType, UseFetchReturnType, OnFetchRequestCallbackType, OnFetchSuccessCallbackType, OnFetchErrorCallbackType, OnFetchFinishedCallbackType, OnFetchStartCallbackType, OnFetchProgressCallbackType, useFetchDefaultOptions, useSubmit, UseSubmitOptionsType, UseSubmitReturnType, OnSubmitRequestCallbackType, OnSubmitSuccessCallbackType, OnSubmitErrorCallbackType, OnSubmitFinishedCallbackType, OnSubmitStartCallbackType, OnSubmitProgressCallbackType, useSubmitDefaultOptions, useQueue, UseQueueOptions, QueueRequest, useQueueDefaultOptions, useCache, useCacheDefaultOptions, UseCacheOptionsType, UseCacheReturnType, OnCacheRequestCallbackType, OnCacheSuccessCallbackType, OnCacheErrorCallbackType, OnCacheFinishedCallbackType, OnCacheChangeCallbackType, useAppManager }; | ||
export { useFetch, UseFetchOptionsType, UseFetchReturnType, useFetchDefaultOptions$0 as useFetchDefaultOptions, useSubmit, UseSubmitOptionsType, UseSubmitReturnType, useSubmitDefaultOptions, useQueue, UseQueueOptions, QueueRequest, useQueueDefaultOptions, useCache, useCacheDefaultOptions, UseCacheOptionsType, UseCacheReturnType, useAppManager, isEmpty, isEqual }; |
@@ -1,2 +0,2 @@ | ||
import{useState as e,useRef as r}from"react";import{useDidUpdate as t,useDidMount as n,useWillUnmount as a}from"@better-typed/react-lifecycle-hooks";import{FetchCommand as i,getCommandKey as o,DateInterval as s,getCommandQueue as u}from"@better-typed/hyper-fetch";function c(e,r,t,n,a,i,o){try{var s=e[i](o),u=s.value}catch(e){return void t(e)}s.done?r(u):Promise.resolve(u).then(n,a)}function l(e){return function(){var r=this,t=arguments;return new Promise((function(n,a){var i=e.apply(r,t);function o(e){c(i,n,a,o,s,"next",e)}function s(e){c(i,n,a,o,s,"throw",e)}o(void 0)}))}}function d(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function f(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,a,i=[],o=!0,s=!1;try{for(t=t.call(e);!(o=(n=t.next()).done)&&(i.push(n.value),!r||i.length!==r);o=!0);}catch(e){s=!0,a=e}finally{try{o||null==t.return||t.return()}finally{if(s)throw a}}return i}}(e,r)||function(e,r){if(e){if("string"==typeof e)return d(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?d(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var p=Object.prototype,h=p.hasOwnProperty,v="function"==typeof Symbol?Symbol:{},g=v.iterator||"@@iterator",m=v.asyncIterator||"@@asyncIterator",y=v.toStringTag||"@@toStringTag";function b(e,r,t,n){var a=r&&r.prototype instanceof x?r:x,i=Object.create(a.prototype),o=new N(n||[]);return i._invoke=function(e,r,t){var n="suspendedStart";return function(a,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===a)throw i;return I()}for(t.method=a,t.arg=i;;){var o=t.delegate;if(o){var s=q(o,t);if(s){if(s===w)continue;return s}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var u=O(e,r,t);if("normal"===u.type){if(n=t.done?"completed":"suspendedYield",u.arg===w)continue;return{value:u.arg,done:t.done}}"throw"===u.type&&(n="completed",t.method="throw",t.arg=u.arg)}}}(e,t,o),i}function O(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}var w={};function x(){}function E(){}function j(){}var R={};R[g]=function(){return this};var k=Object.getPrototypeOf,D=k&&k(k(K([])));D&&D!==p&&h.call(D,g)&&(R=D);var P=j.prototype=x.prototype=Object.create(R);function S(e){["next","throw","return"].forEach((function(r){e[r]=function(e){return this._invoke(r,e)}}))}function T(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===E||"GeneratorFunction"===(r.displayName||r.name))}function L(e,r){function t(n,a,i,o){var s=O(e[n],e,a);if("throw"!==s.type){var u=s.arg,c=u.value;return c&&"object"==typeof c&&h.call(c,"__await")?r.resolve(c.__await).then((function(e){t("next",e,i,o)}),(function(e){t("throw",e,i,o)})):r.resolve(c).then((function(e){u.value=e,i(u)}),(function(e){return t("throw",e,i,o)}))}o(s.arg)}var n;this._invoke=function(e,a){function i(){return new r((function(r,n){t(e,a,r,n)}))}return n=n?n.then(i,i):i()}}function q(e,r){var t=e.iterator[r.method];if(undefined===t){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=undefined,q(e,r),"throw"===r.method))return w;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return w}var n=O(t,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,w;var a=n.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=undefined),r.delegate=null,w):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,w)}function F(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function M(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function N(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(F,this),this.reset(!0)}function K(e){if(e){var r=e[g];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var t=-1,n=function r(){for(;++t<e.length;)if(h.call(e,t))return r.value=e[t],r.done=!1,r;return r.value=undefined,r.done=!0,r};return n.next=n}}return{next:I}}function I(){return{value:undefined,done:!0}}E.prototype=P.constructor=j,j.constructor=E,j[y]=E.displayName="GeneratorFunction",S(L.prototype),L.prototype[m]=function(){return this},S(P),P[y]="Generator",P[g]=function(){return this},P.toString=function(){return"[object Generator]"},N.prototype={constructor:N,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=undefined,this.done=!1,this.delegate=null,this.method="next",this.arg=undefined,this.tryEntries.forEach(M),!e)for(var r in this)"t"===r.charAt(0)&&h.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=undefined)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function t(t,n){return i.type="throw",i.arg=e,r.next=t,n&&(r.method="next",r.arg=undefined),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var a=this.tryEntries[n],i=a.completion;if("root"===a.tryLoc)return t("end");if(a.tryLoc<=this.prev){var o=h.call(a,"catchLoc"),s=h.call(a,"finallyLoc");if(o&&s){if(this.prev<a.catchLoc)return t(a.catchLoc,!0);if(this.prev<a.finallyLoc)return t(a.finallyLoc)}else if(o){if(this.prev<a.catchLoc)return t(a.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return t(a.finallyLoc)}}}},abrupt:function(e,r){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc<=this.prev&&h.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var a=n;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,w):this.complete(i)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),w},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),M(t),w}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var a=n.arg;M(t)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:K(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=undefined),w}};var _={wrap:b,isGeneratorFunction:T,AsyncIterator:L,mark:function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,j):(e.__proto__=j,y in e||(e[y]="GeneratorFunction")),e.prototype=Object.create(P),e},awrap:function(e){return{__await:e}},async:function(e,r,t,n,a){void 0===a&&(a=Promise);var i=new L(b(e,r,t,n),a);return T(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},keys:function(e){var r=[];for(var t in e)r.push(t);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},values:K},Q={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function C(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var A=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,J=(e,r)=>!r||!!e&&+new Date>+r+e;function B(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function G(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?B(Object(t),!0).forEach((function(r){C(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):B(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var U=(e,r,t)=>{var n,a,i,o;return G(G({},Q),{},{data:(null==r||null===(n=r.response)||void 0===n?void 0:n[0])||Q.data,error:(null==r||null===(a=r.response)||void 0===a?void 0:a[1])||Q.error,status:(null==r||null===(i=r.response)||void 0===i?void 0:i[2])||Q.status,retries:(null==r?void 0:r.retries)||Q.retries,timestamp:(o=(null==r?void 0:r.timestamp)||Q.timestamp,o?new Date(o):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:null!=t?t:Q.loading})};function Y(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function $(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?Y(Object(t),!0).forEach((function(r){C(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):Y(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var z=(a,i,o,s)=>{var u,c=a.builder,d=a.cacheKey,p=a.queueKey,h=c.appManager,v=c.fetchQueue,g=c.cache,m=f(e(!1),2),y=m[0],b=m[1],O=f(e(+new Date),2)[1],w=r(U(a,(u=i)?{response:u,retries:0,timestamp:+new Date,isRefreshed:!1}:null)),x=r([]),E=e=>{x.current.find((r=>e.includes(r)))&&O(+new Date)};t((()=>{(function(){var e=l(_.mark((function e(){var r,t,n,s;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c.cache.get(d);case 2:return r=e.sent,t=r||A(i),e.next=6,o.get(p);case 6:n=e.sent,s=w.current.loading||!!n.requests.length,w.current=U(a,t,s),O(+new Date),b(!0);case 11:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}})()()}),[...s],!0),n((()=>{var e=h.events.onFocus((()=>{w.current.isFocused=!0,E(["isFocused"])})),r=h.events.onBlur((()=>{w.current.isFocused=!1,E(["isFocused"])})),t=h.events.onOnline((()=>{w.current.isOnline=!0,E(["isOnline"])})),n=h.events.onOffline((()=>{w.current.isOnline=!1,E(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var j,R,k,D,P,S,T,L,q,F,M={setCacheData:(F=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=6;break}return e.next=4,g.set($({cache:a.cache,cacheKey:d},r));case 4:e.next=9;break;case 6:t={data:r.response[0],error:r.response[1],status:r.response[2],retries:r.retries,timestamp:new Date(r.timestamp),retryError:r.retryError,refreshError:r.refreshError,isRefreshed:r.isRefreshed,loading:!1},w.current=$($({},w.current),t),E(Object.keys(t));case 9:case"end":return e.stop()}}),e)}))),function(e){return F.apply(this,arguments)}),setData:(q=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[r,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed});case 5:e.next=9;break;case 7:w.current.data=r,E(["data"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return q.apply(this,arguments)}),setError:(L=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,r,t.status],retries:t.retries,isRefreshed:t.isRefreshed});case 5:e.next=9;break;case 7:w.current.error=r,E(["error"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return L.apply(this,arguments)}),setLoading:(T=l(_.mark((function e(r){var t=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:t.length>1&&void 0!==t[1]&&!t[1]?(w.current.loading=r,E(["loading"])):v.events.setLoading(d,{isLoading:r,isRetry:!1});case 2:case"end":return e.stop()}}),e)}))),function(e){return T.apply(this,arguments)}),setStatus:(S=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,t.error,r],retries:t.retries,isRefreshed:t.isRefreshed});case 5:e.next=9;break;case 7:w.current.status=r,E(["status"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return S.apply(this,arguments)}),setRefreshed:(P=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:r});case 5:e.next=9;break;case 7:w.current.isRefreshed=r,E(["isRefreshed"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return P.apply(this,arguments)}),setRefreshError:(D=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,r,t.status],retries:t.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:w.current.refreshError=r,E(["refreshError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return D.apply(this,arguments)}),setRetryError:(k=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,r,t.status],retries:t.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:w.current.retryError=r,E(["retryError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return k.apply(this,arguments)}),setRetries:(R=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,t.error,t.status],retries:r,isRefreshed:t.isRefreshed});case 5:e.next=9;break;case 7:w.current.retries=r,E(["retries"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return R.apply(this,arguments)}),setTimestamp:(j=l(_.mark((function e(r){var t,n=arguments;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n.length>1&&void 0!==n[1]&&!n[1]){e.next=7;break}return t=w.current,e.next=5,g.set({cache:a.cache,cacheKey:d,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed,timestamp:r?+r:void 0});case 5:e.next=9;break;case 7:w.current.timestamp=r,E(["timestamp"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return j.apply(this,arguments)})};return[w.current,M,e=>{x.current.push(e)},y]},H=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,t=r({time:e,timer:null});t.current.time=e;var n=()=>{t.current.timer&&clearTimeout(t.current.timer),t.current.timer=null},i=e=>{n(),t.current.timer=setTimeout((()=>{t.current.timer=null,e()}),t.current.time)};return a(n),{debounce:i,resetDebounce:n,active:!!t.current.timer}},V=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,t=r({time:e,timer:null});t.current.time=e;var n=()=>{t.current.timer&&clearInterval(t.current.timer),t.current.timer=null},i=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;n(),t.current.timer=setInterval((()=>{e()}),r<=0?t.current.time:r)};return a(n),{interval:i,resetInterval:n,active:!!t.current.timer}},W=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:X,a=n.dependencies,s=void 0===a?X.dependencies:a,u=n.disabled,c=void 0===u?X.disabled:u,d=n.dependencyTracking,p=void 0===d?X.dependencyTracking:d,h=n.revalidateOnMount,v=void 0===h?X.revalidateOnMount:h,g=n.initialData,m=void 0===g?X.initialData:g,y=n.refresh,b=void 0===y?X.refresh:y,O=n.refreshTime,w=void 0===O?X.refreshTime:O,x=n.refreshBlurred,E=void 0===x?X.refreshBlurred:x,j=n.refreshOnTabBlur,R=void 0===j?X.refreshOnTabBlur:j,k=n.refreshOnTabFocus,D=void 0===k?X.refreshOnTabFocus:k,P=n.refreshOnReconnect,S=void 0===P?X.refreshOnReconnect:P,T=n.debounce,L=void 0===T?X.debounce:T,q=n.debounceTime,F=void 0===q?X.debounceTime:q,M=n.shouldThrow,N=void 0===M?X.shouldThrow:M,K=e.cacheTime,I=e.cacheKey,Q=e.queueKey,C=e.builder,A=e.dump(),B=H(F),G=V(w),U=C.cache,Y=C.fetchQueue,$=C.appManager,W=C.commandManager,Z=C.loggerManager,ee=r(Z.init("useFetch")).current,re=z(e,m,Y,[JSON.stringify(A)]),te=f(re,4),ne=te[0],ae=te[1],ie=te[2],oe=te[3],se=r(null),ue=r(null),ce=r(null),le=r(null),de=r(null),fe=r(null),pe=r(null),he=r(null),ve=()=>{c?ee.debug("Cannot add to fetch queue",{disabled:c}):(ee.debug("Adding request to fetch queue"),Y.add(e))},ge=()=>{G.resetInterval();var e=ne.timestamp,r=w;if(e){var t=+new Date-+e;r=t>=0&&t<w?w-t:0}b&&(ee.debug("Starting refresh counter, request will be send in ".concat(r,"ms")),G.interval(l(_.mark((function e(){var r,t,n,a;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Y.get(Q);case 2:r=e.sent,t=!$.isFocused,n=t&&E||$.isFocused,!(a=!!r.requests.length)&&n?(ee.debug("Performing refresh request",{hasQueueElements:a,canRefresh:n,isFocused:$.isFocused,timestamp:ne.timestamp}),ve(),G.resetInterval()):ee.debug("Cannot trigger refresh request",{hasQueueElements:a,canRefresh:n,isFocused:$.isFocused,timestamp:ne.timestamp});case 8:case"end":return e.stop()}}),e)}))),r))},me=e=>{if(e){var r,t,n,a=e[2]||0,i=!(!e[0]||e[1]),o=!!(!e[1]&&a>=200&&a<=400);if(i||o)null==ue||null===(t=ue.current)||void 0===t||t.call(ue,e[0]);else if(null==ce||null===(n=ce.current)||void 0===n||n.call(ce,e[1]),N)throw{message:"Fetching Error.",error:e[1]};null==le||null===(r=le.current)||void 0===r||r.call(le,e)}else ee.debug("No response to perform callbacks")},ye=function(){var e=l(_.mark((function e(r){return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return ee.debug("Received new data"),me(r.response),e.next=4,ae.setCacheData(r,!1);case 4:return e.next=6,ae.setLoading(!1,!1);case 6:ge();case 7:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),be=function(){var e=l(_.mark((function e(r,t,n){return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return ee.debug("Received equal data event"),me(r.response),e.next=4,ae.setRefreshed(t,!1);case 4:return e.next=6,ae.setTimestamp(new Date(n),!1);case 6:return e.next=8,ae.setLoading(!1,!1);case 8:ge();case 9:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),Oe=e=>{var r,t=e.isLoading,n=e.isRetry;ae.setLoading(t,!1),null===(r=se.current)||void 0===r||r.call(se,{isRetry:n})},we=()=>{ve()},xe=e=>{e&&e instanceof i?U.events.revalidate("/".concat(o(e,!0),"/")):e?U.events.revalidate(e):we()},Ee=e=>{var r;null==pe||null===(r=pe.current)||void 0===r||r.call(pe,e)},je=e=>{var r;null==he||null===(r=he.current)||void 0===r||r.call(he,e)},Re=e=>{var r;null==de||null===(r=de.current)||void 0===r||r.call(de,e)},ke=e=>{var r;null==de||null===(r=de.current)||void 0===r||r.call(de,e)},De=()=>{var e=$.events.onFocus((()=>{D&&ve(),ge()})),r=$.events.onBlur((()=>{R&&ve(),ge()})),t=$.events.onOnline((()=>{S&&ve(),ge()})),n=C.appManager.events.onOffline((()=>{ge()})),a=W.events.onDownloadProgress(Q,Ee),i=W.events.onUploadProgress(Q,je),o=W.events.onRequestStart(Q,Re),s=W.events.onResponseStart(Q,ke),u=Y.events.onLoading(Q,Oe),c=U.events.get(I,ye),l=U.events.getEqualData(I,be),d=U.events.onRevalidate(I,we);return()=>{e(),r(),t(),n(),a(),i(),o(),s(),u(),c(),l(),d()}},Pe=()=>{p||Object.keys(ne).forEach((e=>ie(e)))};return t((()=>{var e=oe&&J(K,ne.timestamp);(v||e)&&ve()}),[oe]),t((()=>(Pe(),De())),[JSON.stringify(A)],!0),t((()=>{oe&&me([ne.data,ne.error,ne.status])}),[JSON.stringify(A),oe],!0),t((()=>{!Y.getRequestCount(Q)&&L?(ee.debug("Debouncing request",{queueKey:Q,command:e}),B.debounce((()=>ve()))):ve()}),[JSON.stringify(A),...s,c]),t((()=>{ge()}),[JSON.stringify(A),...s,c,b,w],!0),{get data(){return ie("data"),ne.data},get error(){return ie("error"),ne.error},get loading(){return ie("loading"),ne.loading},get status(){return ie("status"),ne.status},get retryError(){return ie("retryError"),ne.retryError},get refreshError(){return ie("refreshError"),ne.refreshError},get isRefreshed(){return ie("isRefreshed"),ne.isRefreshed},get retries(){return ie("retries"),ne.retries},get timestamp(){return ie("timestamp"),ne.timestamp},get isOnline(){return ie("isOnline"),ne.isOnline},get isFocused(){return ie("isFocused"),ne.isFocused},get isRefreshingError(){return ie("error"),ie("isRefreshed"),!!ne.error&&ne.isRefreshed},get isStale(){return ie("timestamp"),J(K,ne.timestamp)},onRequest:e=>{se.current=e},onSuccess:e=>{ue.current=e},onError:e=>{ce.current=e},onFinished:e=>{le.current=e},onRequestStart:e=>{de.current=e},onResponseStart:e=>{fe.current=e},onDownloadProgress:e=>{pe.current=e},onUploadProgress:e=>{he.current=e},actions:ae,isDebouncing:B.active,refresh:xe}},X={dependencies:[],disabled:!1,dependencyTracking:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:s.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,debounce:!1,debounceTime:400,shouldThrow:!1};function Z(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function ee(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?Z(Object(t),!0).forEach((function(r){C(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):Z(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var re=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:te,a=n.disabled,s=void 0===a?te.disabled:a,u=n.dependencyTracking,c=void 0===u?te.dependencyTracking:u,l=n.initialData,d=void 0===l?te.initialData:l,p=n.debounce,h=void 0===p?te.debounce:p,v=n.debounceTime,g=void 0===v?te.debounceTime:v,m=n.shouldThrow,y=void 0===m?te.shouldThrow:m,b=e.cacheTime,O=e.cacheKey,w=e.queueKey,x=e.builder,E=e.invalidate,j=H(g),R=x.cache,k=x.submitQueue,D=x.commandManager,P=x.loggerManager,S=r(P.init("useSubmit")).current,T=e.dump(),L=z(e,d,k,[JSON.stringify(T)]),q=f(L,3),F=q[0],M=q[1],N=q[2],K=r(null),I=r(null),_=r(null),Q=r(null),C=r(null),A=r(null),B=r(null),G=r(null),U=function(){var r=arguments.length<=0?void 0:arguments[0];if(s)S.debug("Cannot add to submit queue",{disabled:s,options:r});else{if(S.debug("Adding request to submit queue",{disabled:s,options:r}),!h)return e.send(ee(ee({},r),{},{queueType:"submit"}));j.debounce((()=>{e.send(ee(ee({},r),{},{queueType:"submit"}))}))}},Y=e=>{e&&(e&&e instanceof i?R.events.revalidate("/".concat(o(e,!0),"/")):R.events.revalidate(e))},$=e=>{if(e){var r,t,n,a=e[2]||0,i=!(!e[0]||e[1]),o=!!(!e[1]&&a>=200&&a<=400);if(i||o)null==I||null===(t=I.current)||void 0===t||t.call(I,e[0]),null==E||E.forEach((e=>Y(e)));else if(null==_||null===(n=_.current)||void 0===n||n.call(_,e[1]),y)throw{message:"Fetching Error.",error:e[1]};null==Q||null===(r=Q.current)||void 0===r||r.call(Q,e)}else S.debug("No response to perform callbacks")},V=e=>{$(e.response),M.setLoading(!1,!1),M.setCacheData(e,!1)},W=(e,r,t)=>{$(e.response),M.setRefreshed(r,!1),M.setTimestamp(new Date(t),!1),M.setLoading(!1,!1)},X=e=>{var r,t=e.isLoading,n=e.isRetry;M.setLoading(t,!1),null===(r=K.current)||void 0===r||r.call(K,{isRetry:n})},Z=e=>{var r;null==B||null===(r=B.current)||void 0===r||r.call(B,e)},re=e=>{var r;null==G||null===(r=G.current)||void 0===r||r.call(G,e)},ne=e=>{var r;null==C||null===(r=C.current)||void 0===r||r.call(C,e)},ae=e=>{var r;null==C||null===(r=C.current)||void 0===r||r.call(C,e)},ie=()=>{var e=D.events.onDownloadProgress(w,Z),r=D.events.onUploadProgress(w,re),t=D.events.onRequestStart(w,ne),n=D.events.onResponseStart(w,ae),a=k.events.onLoading(w,X),i=R.events.get(O,V),o=R.events.getEqualData(O,W);return()=>{e(),r(),t(),n(),a(),i(),o()}},oe=()=>{c||Object.keys(F).forEach((e=>N(e)))};return t((()=>(oe(),ie())),[JSON.stringify(T)],!0),{submit:U,get data(){return N("data"),F.data},get error(){return N("error"),F.error},get submitting(){return N("loading"),F.loading},get status(){return N("status"),F.status},get retryError(){return N("retryError"),F.retryError},get retries(){return N("retries"),F.retries},get timestamp(){return N("timestamp"),F.timestamp},get isOnline(){return N("isOnline"),F.isOnline},get isFocused(){return N("isFocused"),F.isFocused},get isStale(){return J(b,F.timestamp)},onSubmitRequest:e=>{K.current=e},onSubmitSuccess:e=>{I.current=e},onSubmitError:e=>{_.current=e},onSubmitFinished:e=>{Q.current=e},onSubmitRequestStart:e=>{C.current=e},onSubmitResponseStart:e=>{A.current=e},onSubmitDownloadProgress:e=>{B.current=e},onSubmitUploadProgress:e=>{G.current=e},actions:M,isDebouncing:!1,isRefreshed:!1,revalidate:Y}},te={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,invalidate:[]};function ne(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function ae(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ne(Object(t),!0).forEach((function(r){C(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ne(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var ie=function(a){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:oe,o=i.queueType,s=void 0===o?oe.queueType:o,c=a.queueKey,d=a.builder,p=d.commandManager,h=r(u(a,s)),v=e(!0),g=f(v,2),m=g[0],y=g[1],b=e(!1),O=f(b,2),w=O[0],x=O[1],E=e([]),j=f(E,2),R=j[0],k=j[1],D=function(){var e=l(_.mark((function e(){var r,t;return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=h.current,e.next=3,r.get(c);case 3:t=e.sent,x(t.stopped),k(t.requests),y(!1);case 7:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),P=()=>{var e=h.current,r=e.events.onQueueChange(c,(e=>{x(e.stopped),k([...e.requests])})),t=p.events.onDownloadProgress(c,((e,r)=>{var t=r.requestId;k((r=>r.map((r=>r.requestId===t?ae(ae({},r),{},{downloading:e}):r))))})),n=p.events.onDownloadProgress(c,((e,r)=>{var t=r.requestId;k((r=>r.map((r=>r.requestId===t?ae(ae({},r),{},{uploading:e}):r))))})),a=e.events.onQueueStatus(c,(e=>{x(e.stopped),k([...e.requests])}));return()=>{a(),r(),t(),n()}};return n((()=>{D()})),t((()=>P()),[w,R,k,x],!0),{connecting:m,stopped:w,requests:R,stopQueue:()=>h.current.stopQueue(c),pauseQueue:()=>h.current.pauseQueue(c),startQueue:()=>h.current.startQueue(c)}},oe={queueType:"auto"};function se(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function ue(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?se(Object(t),!0).forEach((function(r){C(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):se(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var ce=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:le,a=n.dependencyTracking,s=void 0===a?le.dependencyTracking:a,u=n.initialData,c=void 0===u?le.initialData:u,d=e.cacheTime,p=e.cacheKey,h=e.queueKey,v=e.builder,g=e.dump(),m=v.cache,y=v.fetchQueue,b=v.loggerManager,O=r(b.init("useCache")).current,w=z(e,c,y,[JSON.stringify(g)]),x=f(w,4),E=x[0],j=x[1],R=x[2],k=x[3],D=r(null),P=r(null),S=r(null),T=r(null),L=e=>{if(e){var r,t,n,a=e[2]||0,i=!(!e[0]||e[1]),o=!!(!e[1]&&a>=200&&a<=400);if(i||o)null==D||null===(t=D.current)||void 0===t||t.call(D,e[0]);else null==P||null===(n=P.current)||void 0===n||n.call(P,e[1]);null==S||null===(r=S.current)||void 0===r||r.call(S,e)}else O.debug("No response to perform callbacks")},q=function(){var e=l(_.mark((function e(r){return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return O.debug("Received new data"),L(r.response),e.next=4,j.setCacheData(r,!1);case 4:return e.next=6,j.setLoading(!1,!1);case 6:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),F=function(){var e=l(_.mark((function e(r,t,n){return _.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return O.debug("Received equal data event"),L(r.response),e.next=4,j.setRefreshed(t,!1);case 4:return e.next=6,j.setTimestamp(new Date(n),!1);case 6:return e.next=8,j.setLoading(!1,!1);case 8:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),M=e=>{e&&e instanceof i?m.events.revalidate("/".concat(o(e,!0),"/")):e?m.events.revalidate(e):m.events.revalidate(p)},N=e=>{var r=e.isLoading;j.setLoading(r,!1)},K=()=>{var e=y.events.onLoading(h,N),r=m.events.get(p,q),t=m.events.getEqualData(p,F);return()=>{e(),r(),t()}},I=()=>{s||Object.keys(E).forEach((e=>R(e)))};return t((()=>(I(),K())),[JSON.stringify(g)],!0),t((()=>{k&&L([E.data,E.error,E.status])}),[JSON.stringify(g),k],!0),ue(ue({get data(){return R("data"),E.data},get error(){return R("error"),E.error},get loading(){return R("loading"),E.loading},get status(){return R("status"),E.status},get retryError(){return R("retryError"),E.retryError},get refreshError(){return R("refreshError"),E.refreshError},get isRefreshed(){return R("isRefreshed"),E.isRefreshed},get retries(){return R("retries"),E.retries},get timestamp(){return R("timestamp"),E.timestamp},get isOnline(){return R("isOnline"),E.isOnline},get isFocused(){return R("isFocused"),E.isFocused},get isRefreshingError(){return R("error"),R("isRefreshed"),!!E.error&&E.isRefreshed},get isStale(){return R("timestamp"),J(d,E.timestamp)},onSuccess:e=>{D.current=e},onError:e=>{P.current=e},onFinished:e=>{S.current=e},onChange:e=>{T.current=e}},j),{},{revalidate:M})},le={dependencyTracking:!0,initialData:null},de=r=>{var t=f(e(r.appManager.isOnline),2),a=t[0],i=t[1],o=f(e(r.appManager.isFocused),2),s=o[0],u=o[1];return n((()=>{var e=r.appManager.events.onOnline((()=>i(!0))),t=r.appManager.events.onOffline((()=>i(!1))),n=r.appManager.events.onFocus((()=>u(!0))),a=r.appManager.events.onBlur((()=>u(!1)));return()=>{e(),t(),n(),a()}})),{isOnline:a,isFocused:s}};export{de as useAppManager,ce as useCache,le as useCacheDefaultOptions,W as useFetch,X as useFetchDefaultOptions,ie as useQueue,oe as useQueueDefaultOptions,re as useSubmit,te as useSubmitDefaultOptions}; | ||
import{useRef as e,useState as r}from"react";import{useWillUnmount as t,useDidUpdate as n,useDidMount as o}from"@better-typed/react-lifecycle-hooks";import{DateInterval as i,FetchCommand as a,getCommandKey as s,getCommandQueue as c}from"@better-typed/hyper-fetch";function u(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function l(e,r,t,n,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void t(e)}s.done?r(c):Promise.resolve(c).then(n,o)}function d(e){return function(){var r=this,t=arguments;return new Promise((function(n,o){var i=e.apply(r,t);function a(e){l(i,n,o,a,s,"next",e)}function s(e){l(i,n,o,a,s,"throw",e)}a(void 0)}))}}function p(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function f(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,o,i=[],a=!0,s=!1;try{for(t=t.call(e);!(a=(n=t.next()).done)&&(i.push(n.value),!r||i.length!==r);a=!0);}catch(e){s=!0,o=e}finally{try{a||null==t.return||t.return()}finally{if(s)throw o}}return i}}(e,r)||function(e,r){if(e){if("string"==typeof e)return p(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?p(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var h=Object.prototype,v=h.hasOwnProperty,g="function"==typeof Symbol?Symbol:{},y=g.iterator||"@@iterator",m=g.asyncIterator||"@@asyncIterator",b=g.toStringTag||"@@toStringTag";function O(e,r,t,n){var o=r&&r.prototype instanceof x?r:x,i=Object.create(o.prototype),a=new M(n||[]);return i._invoke=function(e,r,t){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return A()}for(t.method=o,t.arg=i;;){var a=t.delegate;if(a){var s=L(a,t);if(s){if(s===j)continue;return s}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var c=w(e,r,t);if("normal"===c.type){if(n=t.done?"completed":"suspendedYield",c.arg===j)continue;return{value:c.arg,done:t.done}}"throw"===c.type&&(n="completed",t.method="throw",t.arg=c.arg)}}}(e,t,a),i}function w(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}var j={};function x(){}function E(){}function k(){}var P={};P[y]=function(){return this};var S=Object.getPrototypeOf,R=S&&S(S(N([])));R&&R!==h&&v.call(R,y)&&(P=R);var D=k.prototype=x.prototype=Object.create(P);function T(e){["next","throw","return"].forEach((function(r){e[r]=function(e){return this._invoke(r,e)}}))}function q(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===E||"GeneratorFunction"===(r.displayName||r.name))}function F(e,r){function t(n,o,i,a){var s=w(e[n],e,o);if("throw"!==s.type){var c=s.arg,u=c.value;return u&&"object"==typeof u&&v.call(u,"__await")?r.resolve(u.__await).then((function(e){t("next",e,i,a)}),(function(e){t("throw",e,i,a)})):r.resolve(u).then((function(e){c.value=e,i(c)}),(function(e){return t("throw",e,i,a)}))}a(s.arg)}var n;this._invoke=function(e,o){function i(){return new r((function(r,n){t(e,o,r,n)}))}return n=n?n.then(i,i):i()}}function L(e,r){var t=e.iterator[r.method];if(undefined===t){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=undefined,L(e,r),"throw"===r.method))return j;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return j}var n=w(t,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,j;var o=n.arg;return o?o.done?(r[e.resultName]=o.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=undefined),r.delegate=null,j):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,j)}function C(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function K(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function M(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(C,this),this.reset(!0)}function N(e){if(e){var r=e[y];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var t=-1,n=function r(){for(;++t<e.length;)if(v.call(e,t))return r.value=e[t],r.done=!1,r;return r.value=undefined,r.done=!0,r};return n.next=n}}return{next:A}}function A(){return{value:undefined,done:!0}}E.prototype=D.constructor=k,k.constructor=E,k[b]=E.displayName="GeneratorFunction",T(F.prototype),F.prototype[m]=function(){return this},T(D),D[b]="Generator",D[y]=function(){return this},D.toString=function(){return"[object Generator]"},M.prototype={constructor:M,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=undefined,this.done=!1,this.delegate=null,this.method="next",this.arg=undefined,this.tryEntries.forEach(K),!e)for(var r in this)"t"===r.charAt(0)&&v.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=undefined)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function t(t,n){return i.type="throw",i.arg=e,r.next=t,n&&(r.method="next",r.arg=undefined),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n],i=o.completion;if("root"===o.tryLoc)return t("end");if(o.tryLoc<=this.prev){var a=v.call(o,"catchLoc"),s=v.call(o,"finallyLoc");if(a&&s){if(this.prev<o.catchLoc)return t(o.catchLoc,!0);if(this.prev<o.finallyLoc)return t(o.finallyLoc)}else if(a){if(this.prev<o.catchLoc)return t(o.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return t(o.finallyLoc)}}}},abrupt:function(e,r){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc<=this.prev&&v.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===e||"continue"===e)&&o.tryLoc<=r&&r<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=e,i.arg=r,o?(this.method="next",this.next=o.finallyLoc,j):this.complete(i)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),j},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),K(t),j}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var o=n.arg;K(t)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:N(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=undefined),j}};var I={wrap:O,isGeneratorFunction:q,AsyncIterator:F,mark:function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,k):(e.__proto__=k,b in e||(e[b]="GeneratorFunction")),e.prototype=Object.create(D),e},awrap:function(e){return{__await:e}},async:function(e,r,t,n,o){void 0===o&&(o=Promise);var i=new F(O(e,r,t,n),o);return q(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},keys:function(e){var r=[];for(var t in e)r.push(t);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},values:N},_=function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e({time:r,timer:null});n.current.time=r;var o=()=>{n.current.timer&&clearTimeout(n.current.timer),n.current.timer=null},i=e=>{o(),n.current.timer=setTimeout((()=>{n.current.timer=null,e()}),n.current.time)};return t(o),{debounce:i,resetDebounce:o,active:!!n.current.timer}},Q=function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e({time:r,timer:null});n.current.time=r;var o=()=>{n.current.timer&&clearInterval(n.current.timer),n.current.timer=null},i=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;o(),n.current.timer=setInterval((()=>{e()}),r<=0?n.current.time:r)};return t(o),{interval:i,resetInterval:o,active:!!n.current.timer}},B=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,G=(e,r)=>!r||!!e&&+new Date>+r+e,J=e=>{var r=Object.prototype.toString.call(e);return Array.isArray(e)?!e.length:"object"==typeof e&&null!==e&&"[object Object]"===r&&!Object.keys(e).length},U=(e,r)=>{var t,n=Object.prototype.toString.call(e),o=Object.prototype.toString.call(r),i=typeof e,a=typeof r,s=e=>i===e&&a===e;return n===o&&(null===e&&null===r||(!!(s("number")&&Number.isNaN(e)&&Number.isNaN(r))||(!(!J(e)||!J(r))||(Array.isArray(e)&&Array.isArray(r)?e.length===r.length&&!e.some(((e,t)=>!U(e,r[t]))):s("object")&&(n===(t="[object Object]")&&o===t)?Object.keys(e).length===Object.keys(r).length&&!Object.entries(e).some((e=>{var t=f(e,2),n=t[0],o=t[1];return!U(o,r[n])})):e instanceof Date&&r instanceof Date?+e==+r:e===r))))},z={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function Y(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function $(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?Y(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):Y(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var H=(e,r,t)=>{var n,o,i,a;return $($({},z),{},{data:(null==r||null===(n=r.response)||void 0===n?void 0:n[0])||z.data,error:(null==r||null===(o=r.response)||void 0===o?void 0:o[1])||z.error,status:(null==r||null===(i=r.response)||void 0===i?void 0:i[2])||z.status,retries:(null==r?void 0:r.retries)||z.retries,timestamp:(a=(null==r?void 0:r.timestamp)||z.timestamp,a?new Date(a):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:null!=t?t:z.loading})};function V(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function W(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?V(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):V(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var X=(t,i,a,s)=>{var c,u=t.builder,l=t.cacheKey,p=t.queueKey,h=u.appManager,v=u.fetchQueue,g=u.cache,y=f(r(!1),2),m=y[0],b=y[1],O=f(r(+new Date),2)[1],w=e(H(t,(c=i)?{response:c,retries:0,timestamp:+new Date,isRefreshed:!1}:null)),j=e([]),x=e=>{j.current.find((r=>e.includes(r)))&&O(+new Date)};n((()=>{(function(){var e=d(I.mark((function e(){var r,n,o,s;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,u.cache.get(l);case 2:return r=e.sent,n=r||B(i),e.next=6,a.get(p);case 6:o=e.sent,s=w.current.loading||!!o.requests.length,w.current=H(t,n,s),O(+new Date),b(!0);case 11:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}})()()}),[...s],!0),o((()=>{var e=h.events.onFocus((()=>{w.current.isFocused=!0,x(["isFocused"])})),r=h.events.onBlur((()=>{w.current.isFocused=!1,x(["isFocused"])})),t=h.events.onOnline((()=>{w.current.isOnline=!0,x(["isOnline"])})),n=h.events.onOffline((()=>{w.current.isOnline=!1,x(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var E,k,P,S,R,D,T,q,F,L,C={setCacheData:(L=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=6;break}return e.next=4,g.set(W({cache:t.cache,cacheKey:l},r));case 4:e.next=9;break;case 6:n={data:r.response[0],error:r.response[1],status:r.response[2],retries:r.retries,timestamp:new Date(r.timestamp),retryError:r.retryError,refreshError:r.refreshError,isRefreshed:r.isRefreshed,loading:!1},w.current=W(W({},w.current),n),x(Object.keys(n));case 9:case"end":return e.stop()}}),e)}))),function(e){return L.apply(this,arguments)}),setData:(F=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[r,n.error,n.status],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:w.current.data=r,x(["data"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return F.apply(this,arguments)}),setError:(q=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:w.current.error=r,x(["error"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return q.apply(this,arguments)}),setLoading:(T=d(I.mark((function e(r){var t=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:t.length>1&&void 0!==t[1]&&!t[1]?(w.current.loading=r,x(["loading"])):v.events.setLoading(l,{isLoading:r,isRetry:!1});case 2:case"end":return e.stop()}}),e)}))),function(e){return T.apply(this,arguments)}),setStatus:(D=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,r],retries:n.retries,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:w.current.status=r,x(["status"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return D.apply(this,arguments)}),setRefreshed:(R=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:n.retries,isRefreshed:r});case 5:e.next=9;break;case 7:w.current.isRefreshed=r,x(["isRefreshed"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return R.apply(this,arguments)}),setRefreshError:(S=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:w.current.refreshError=r,x(["refreshError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return S.apply(this,arguments)}),setRetryError:(P=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,r,n.status],retries:n.retries,isRefreshed:!0});case 5:e.next=9;break;case 7:w.current.retryError=r,x(["retryError"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return P.apply(this,arguments)}),setRetries:(k=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:r,isRefreshed:n.isRefreshed});case 5:e.next=9;break;case 7:w.current.retries=r,x(["retries"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return k.apply(this,arguments)}),setTimestamp:(E=d(I.mark((function e(r){var n,o=arguments;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(o.length>1&&void 0!==o[1]&&!o[1]){e.next=7;break}return n=w.current,e.next=5,g.set({cache:t.cache,cacheKey:l,response:[n.data,n.error,n.status],retries:n.retries,isRefreshed:n.isRefreshed,timestamp:r?+r:void 0});case 5:e.next=9;break;case 7:w.current.timestamp=r,x(["timestamp"]);case 9:case"end":return e.stop()}}),e)}))),function(e){return E.apply(this,arguments)})};return[w.current,C,e=>{j.current.push(e)},m]},Z=t=>{var o=t.command,i=t.queue,a=t.dependencyTracking,s=t.initialData,c=t.logger,u=t.deepCompare,l=o.builder,p=o.cacheKey,h=o.queueKey,v=l.commandManager,g=l.cache,y=o.dump(),m=f(r(!1),2),b=m[0],O=m[1],w=f(X(o,s,i,[JSON.stringify(y)]),4),j=w[0],x=w[1],E=w[2],k=w[3],P=e(null),S=e(null),R=e(null),D=e(null),T=e(null),q=e(null),F=e(null),L=e(null),C=e(null),K=e=>{if(e){var r,t,n,o=e[2]||0,i=!(!e[0]||e[1]),a=!!(!e[1]&&o>=200&&o<=400);if(i||a)c.debug("Performing success callback",{status:o,hasSuccessState:i,hasSuccessStatus:a,response:e,hasCallback:!(null==S||!S.current)}),null===(t=S.current)||void 0===t||t.call(S,e[0]);else c.debug("Performing error callback",{status:o,hasSuccessState:i,hasSuccessStatus:a,response:e,hasCallback:!(null==R||!R.current)}),null===(n=R.current)||void 0===n||n.call(R,e[1]);null===(r=D.current)||void 0===r||r.call(D,e)}else c.debug("No response to perform callbacks")},M=function(){var e=d(I.mark((function e(r){var t;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(c.debug("Received new data"),K(r.response),t="function"==typeof u?u:U,!(!!u&&t(r.response,[j.data,j.error,j.status]))){e.next=9;break}return e.next=7,x.setTimestamp(r.timestamp?new Date(r.timestamp):null);case 7:e.next=11;break;case 9:return e.next=11,x.setCacheData(r,!1);case 11:return e.next=13,x.setLoading(!1,!1);case 13:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),N=e=>{var r,t=e.isLoading,n=e.isRetry;x.setLoading(t,!1),null===(r=P.current)||void 0===r||r.call(P,{isRetry:n})},A=e=>{var r;null==F||null===(r=F.current)||void 0===r||r.call(F,e)},_=e=>{var r;null==L||null===(r=L.current)||void 0===r||r.call(L,e)},Q=e=>{var r;null==T||null===(r=T.current)||void 0===r||r.call(T,e)},B=e=>{var r;null==T||null===(r=T.current)||void 0===r||r.call(T,e)};return n((()=>{return a||Object.keys(j).forEach((e=>E(e))),r=v.events.onDownloadProgress(h,A),t=v.events.onUploadProgress(h,_),n=v.events.onRequestStart(h,Q),o=v.events.onResponseStart(h,B),s=i.events.onLoading(h,N),c=g.events.get(p,M),u=()=>{r(),t(),n(),o(),s(),c()},null===(e=C.current)||void 0===e||e.call(C),C.current=u,u;var e,r,t,n,o,s,c,u}),[JSON.stringify(y)],!0),n((()=>{k&&!b&&(K([j.data,j.error,j.status]),O(!0))}),[JSON.stringify(y),k],!0),[j,{actions:x,onRequest:e=>{P.current=e},onSuccess:e=>{S.current=e},onError:e=>{R.current=e},onFinished:e=>{D.current=e},onRequestStart:e=>{T.current=e},onResponseStart:e=>{q.current=e},onDownloadProgress:e=>{F.current=e},onUploadProgress:e=>{L.current=e}},{setRenderKey:E,initialized:k}]};function ee(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function re(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ee(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ee(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}i.hour;var te=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:ne,o=t.dependencies,i=void 0===o?ne.dependencies:o,c=t.disabled,u=void 0===c?ne.disabled:c,l=t.dependencyTracking,p=void 0===l?ne.dependencyTracking:l,h=t.revalidateOnMount,v=void 0===h?ne.revalidateOnMount:h,g=t.initialData,y=void 0===g?ne.initialData:g,m=t.refresh,b=void 0===m?ne.refresh:m,O=t.refreshTime,w=void 0===O?ne.refreshTime:O,j=t.refreshBlurred,x=void 0===j?ne.refreshBlurred:j,E=t.refreshOnTabBlur,k=void 0===E?ne.refreshOnTabBlur:E,P=t.refreshOnTabFocus,S=void 0===P?ne.refreshOnTabFocus:P,R=t.refreshOnReconnect,D=void 0===R?ne.refreshOnReconnect:R,T=t.debounce,q=void 0===T?ne.debounce:T,F=t.debounceTime,L=void 0===F?ne.debounceTime:F,C=t.deepCompare,K=void 0===C?ne.deepCompare:C,M=r.cacheTime,N=r.cacheKey,A=r.queueKey,B=r.builder,J=r.dump(),U=_(L),z=Q(w),Y=e(null),$=B.cache,H=B.fetchQueue,V=B.appManager,W=B.loggerManager,X=e(W.init("useFetch")).current,ee=Z({command:r,queue:H,dependencyTracking:p,initialData:y,logger:X,deepCompare:K}),te=f(ee,3),oe=te[0],ie=te[1],ae=te[2],se=ae.setRenderKey,ce=ae.initialized,ue=()=>{u?X.debug("Cannot add to fetch queue",{disabled:u}):(X.debug("Adding request to fetch queue"),H.add(r))},le=()=>{z.resetInterval();var e=oe.timestamp,r=w;if(e){var t=+new Date-+e;r=t>=0&&t<w?w-t:0}b&&(X.debug("Starting refresh counter, request will be send in ".concat(r,"ms")),z.interval(d(I.mark((function e(){var r,t,n,o;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,H.get(A);case 2:r=e.sent,t=!V.isFocused,n=t&&x||V.isFocused,!(o=!!r.requests.length)&&n?(X.debug("Performing refresh request",{hasQueueElements:o,canRefresh:n,isFocused:V.isFocused,timestamp:oe.timestamp}),ue(),z.resetInterval()):X.debug("Cannot trigger refresh request",{hasQueueElements:o,canRefresh:n,isFocused:V.isFocused,timestamp:oe.timestamp});case 8:case"end":return e.stop()}}),e)}))),r))},de=()=>{ue()},pe=e=>{e&&e instanceof a?$.events.revalidate("/".concat(s(e,!0),"/")):e?$.events.revalidate(e):de()},fe=()=>{var e,r=V.events.onFocus((()=>{S&&ue(),le()})),t=V.events.onBlur((()=>{k&&ue(),le()})),n=V.events.onOnline((()=>{D&&ue(),le()})),o=B.appManager.events.onOffline((()=>{le()})),i=$.events.onRevalidate(N,de),a=()=>{r(),t(),n(),o(),i()};return null===(e=Y.current)||void 0===e||e.call(Y),Y.current=a,a};return n((()=>{var e=ce&&G(M,oe.timestamp);(v||e)&&ue()}),[ce]),n(fe,[JSON.stringify(J)],!0),n((()=>{!H.getRequestCount(A)&&q?(X.debug("Debouncing request",{queueKey:A,command:r}),U.debounce((()=>ue()))):ue()}),[JSON.stringify(J),...i,u]),n((()=>{le()}),[JSON.stringify(J),...i,u,b,w],!0),re(re({get data(){return se("data"),oe.data},get error(){return se("error"),oe.error},get loading(){return se("loading"),oe.loading},get status(){return se("status"),oe.status},get retryError(){return se("retryError"),oe.retryError},get refreshError(){return se("refreshError"),oe.refreshError},get isRefreshed(){return se("isRefreshed"),oe.isRefreshed},get retries(){return se("retries"),oe.retries},get timestamp(){return se("timestamp"),oe.timestamp},get isOnline(){return se("isOnline"),oe.isOnline},get isFocused(){return se("isFocused"),oe.isFocused},get isRefreshingError(){return se("error"),se("isRefreshed"),!!oe.error&&oe.isRefreshed},get isStale(){return se("timestamp"),G(M,oe.timestamp)}},ie),{},{isDebouncing:U.active,refresh:pe})},ne={dependencies:[],disabled:!1,dependencyTracking:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:i.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,debounce:!1,debounceTime:400,deepCompare:!0};function oe(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function ie(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?oe(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):oe(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var ae=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:se,n=t.disabled,o=void 0===n?se.disabled:n,i=t.dependencyTracking,c=void 0===i?se.dependencyTracking:i,u=t.initialData,l=void 0===u?se.initialData:u,d=t.debounce,p=void 0===d?se.debounce:d,h=t.debounceTime,v=void 0===h?se.debounceTime:h,g=t.deepCompare,y=void 0===g?se.deepCompare:g,m=r.cacheTime,b=r.builder,O=_(v),w=b.cache,j=b.submitQueue,x=b.loggerManager,E=e(x.init("useSubmit")).current,k=Z({command:r,queue:j,dependencyTracking:c,initialData:l,logger:E,deepCompare:y}),P=f(k,3),S=P[0],R=P[1],D=P[2].setRenderKey,T=function(){var e=arguments.length<=0?void 0:arguments[0];if(o)E.debug("Cannot add to submit queue",{disabled:o,options:e});else{if(E.debug("Adding request to submit queue",{disabled:o,options:e}),!p)return r.send(ie(ie({},e),{},{queueType:"submit"}));O.debounce((()=>{r.send(ie(ie({},e),{},{queueType:"submit"}))}))}},q=e=>{e&&(e&&e instanceof a?w.events.revalidate("/".concat(s(e,!0),"/")):w.events.revalidate(e))},F={actions:R.actions,onSubmitRequest:R.onRequest,onSubmitSuccess:R.onSuccess,onSubmitError:R.onError,onSubmitFinished:R.onFinished,onSubmitRequestStart:R.onRequestStart,onSubmitResponseStart:R.onResponseStart,onSubmitDownloadProgress:R.onDownloadProgress,onSubmitUploadProgress:R.onUploadProgress};return ie(ie({submit:T,get data(){return D("data"),S.data},get error(){return D("error"),S.error},get submitting(){return D("loading"),S.loading},get status(){return D("status"),S.status},get retryError(){return D("retryError"),S.retryError},get retries(){return D("retries"),S.retries},get timestamp(){return D("timestamp"),S.timestamp},get isOnline(){return D("isOnline"),S.isOnline},get isFocused(){return D("isFocused"),S.isFocused},get isStale(){return G(m,S.timestamp)}},F),{},{isDebouncing:!1,isRefreshed:!1,invalidate:q})},se={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,invalidate:[],deepCompare:!0};function ce(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function ue(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ce(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ce(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var le=function(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:de,a=i.queueType,s=void 0===a?de.queueType:a,u=t.queueKey,l=t.builder,p=l.commandManager,h=e(c(t,s)),v=e(null),g=r(!0),y=f(g,2),m=y[0],b=y[1],O=r(!1),w=f(O,2),j=w[0],x=w[1],E=r([]),k=f(E,2),P=k[0],S=k[1],R=function(){var e=d(I.mark((function e(){var r,t;return I.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=h.current,e.next=3,r.get(u);case 3:t=e.sent,x(t.stopped),S(t.requests),b(!1);case 7:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),D=()=>{var e,r=h.current,t=r.events.onQueueChange(u,(e=>{x(e.stopped),S([...e.requests])})),n=p.events.onDownloadProgress(u,((e,r)=>{var t=r.requestId;S((r=>r.map((r=>r.requestId===t?ue(ue({},r),{},{downloading:e}):r))))})),o=p.events.onDownloadProgress(u,((e,r)=>{var t=r.requestId;S((r=>r.map((r=>r.requestId===t?ue(ue({},r),{},{uploading:e}):r))))})),i=r.events.onQueueStatus(u,(e=>{x(e.stopped),S([...e.requests])})),a=()=>{i(),t(),n(),o()};return null===(e=v.current)||void 0===e||e.call(v),v.current=a,a};return o((()=>{R()})),n((()=>D()),[j,P,S,x],!0),{connecting:m,stopped:j,requests:P,stopQueue:()=>h.current.stopQueue(u),pauseQueue:()=>h.current.pauseQueue(u),startQueue:()=>h.current.startQueue(u)}},de={queueType:"auto"};function pe(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function fe(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?pe(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):pe(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var he=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:ve,n=t.dependencyTracking,o=void 0===n?ve.dependencyTracking:n,i=t.initialData,c=void 0===i?ve.initialData:i,u=t.deepCompare,l=void 0===u?ve.deepCompare:u,d=r.cacheTime,p=r.cacheKey,h=r.builder,v=h.cache,g=h.fetchQueue,y=h.loggerManager,m=e(y.init("useCache")).current,b=Z({command:r,queue:g,dependencyTracking:o,initialData:c,logger:m,deepCompare:l}),O=f(b,3),w=O[0],j=O[1],x=O[2].setRenderKey,E=e=>{e&&e instanceof a?v.events.revalidate("/".concat(s(e,!0),"/")):e?v.events.revalidate(e):v.events.revalidate(p)},k={actions:j.actions,onCacheError:j.onError,onCacheSuccess:j.onSuccess,onCacheChange:j.onFinished};return fe(fe({get data(){return x("data"),w.data},get error(){return x("error"),w.error},get loading(){return x("loading"),w.loading},get status(){return x("status"),w.status},get retryError(){return x("retryError"),w.retryError},get refreshError(){return x("refreshError"),w.refreshError},get isRefreshed(){return x("isRefreshed"),w.isRefreshed},get retries(){return x("retries"),w.retries},get timestamp(){return x("timestamp"),w.timestamp},get isOnline(){return x("isOnline"),w.isOnline},get isFocused(){return x("isFocused"),w.isFocused},get isRefreshingError(){return x("error"),x("isRefreshed"),!!w.error&&w.isRefreshed},get isStale(){return x("timestamp"),G(d,w.timestamp)}},k),{},{invalidate:E})},ve={dependencyTracking:!0,initialData:null,deepCompare:!0},ge=e=>{var t=f(r(e.appManager.isOnline),2),n=t[0],i=t[1],a=f(r(e.appManager.isFocused),2),s=a[0],c=a[1];return o((()=>{var r=e.appManager.events.onOnline((()=>i(!0))),t=e.appManager.events.onOffline((()=>i(!1))),n=e.appManager.events.onFocus((()=>c(!0))),o=e.appManager.events.onBlur((()=>c(!1)));return()=>{r(),t(),n(),o()}})),{isOnline:n,isFocused:s}};export{J as isEmpty,U as isEqual,ge as useAppManager,he as useCache,ve as useCacheDefaultOptions,te as useFetch,ne as useFetchDefaultOptions,le as useQueue,de as useQueueDefaultOptions,ae as useSubmit,se as useSubmitDefaultOptions}; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -6,1 +6,2 @@ export * from "./use-fetch"; | ||
export * from "./use-app-manager"; | ||
export * from "./utils/deep-equal.utils"; |
export const useCacheDefaultOptions = { | ||
dependencyTracking: true, | ||
initialData: null, | ||
deepCompare: true, | ||
}; |
import { useRef } from "react"; | ||
import { useDidUpdate } from "@better-typed/react-lifecycle-hooks"; | ||
import { | ||
QueueLoadingEventType, | ||
FetchCommandInstance, | ||
FetchCommand, | ||
getCommandKey, | ||
CacheValueType, | ||
ExtractResponse, | ||
ExtractError, | ||
ExtractFetchReturn, | ||
} from "@better-typed/hyper-fetch"; | ||
import { FetchCommandInstance, FetchCommand, getCommandKey } from "@better-typed/hyper-fetch"; | ||
import { useDependentState } from "use-dependent-state"; | ||
import { isStaleCacheData } from "utils"; | ||
import { | ||
OnCacheErrorCallbackType, | ||
OnCacheFinishedCallbackType, | ||
OnCacheSuccessCallbackType, | ||
UseCacheOptionsType, | ||
UseCacheReturnType, | ||
OnCacheChangeCallbackType, | ||
useCacheDefaultOptions, | ||
} from "use-cache"; | ||
import { UseCacheOptionsType, UseCacheReturnType, useCacheDefaultOptions } from "use-cache"; | ||
import { useCommandState } from "utils/use-command-state"; | ||
@@ -32,54 +14,19 @@ export const useCache = <T extends FetchCommandInstance>( | ||
initialData = useCacheDefaultOptions.initialData, | ||
deepCompare = useCacheDefaultOptions.deepCompare, | ||
}: UseCacheOptionsType<T> = useCacheDefaultOptions, | ||
): UseCacheReturnType<T> => { | ||
const { cacheTime, cacheKey, queueKey, builder } = command; | ||
const commandDump = command.dump(); | ||
const { cacheTime, cacheKey, builder } = command; | ||
const { cache, fetchQueue, loggerManager } = builder; | ||
const logger = useRef(loggerManager.init("useCache")).current; | ||
const [state, actions, setRenderKey, initialized] = useDependentState<T>(command, initialData, fetchQueue, [ | ||
JSON.stringify(commandDump), | ||
]); | ||
const [state, actions, { setRenderKey }] = useCommandState({ | ||
command, | ||
queue: fetchQueue, | ||
dependencyTracking, | ||
initialData, | ||
logger, | ||
deepCompare, | ||
}); | ||
const onSuccessCallback = useRef<null | OnCacheSuccessCallbackType<ExtractResponse<T>>>(null); | ||
const onErrorCallback = useRef<null | OnCacheErrorCallbackType<ExtractError<T>>>(null); | ||
const onFinishedCallback = useRef<null | OnCacheFinishedCallbackType<ExtractFetchReturn<T>>>(null); | ||
const onChangeCallback = useRef<null | OnCacheChangeCallbackType<ExtractFetchReturn<T>>>(null); | ||
const handleCallbacks = (response: ExtractFetchReturn<T> | undefined) => { | ||
if (response) { | ||
const status = response[2] || 0; | ||
const hasSuccessState = !!(response[0] && !response[1]); | ||
const hasSuccessStatus = !!(!response[1] && status >= 200 && status <= 400); | ||
if (hasSuccessState || hasSuccessStatus) { | ||
onSuccessCallback?.current?.(response[0] as ExtractResponse<T>); | ||
} else { | ||
onErrorCallback?.current?.(response[1] as ExtractError<T>); | ||
} | ||
onFinishedCallback?.current?.(response); | ||
} else { | ||
logger.debug("No response to perform callbacks"); | ||
} | ||
}; | ||
const handleGetCacheData = async (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => { | ||
logger.debug("Received new data"); | ||
handleCallbacks(cacheData.response); // Must be first | ||
await actions.setCacheData(cacheData, false); | ||
await actions.setLoading(false, false); | ||
}; | ||
const handleGetEqualCacheUpdate = async ( | ||
cacheData: CacheValueType<ExtractResponse<T>>, | ||
isRefreshed: boolean, | ||
timestamp: number, | ||
) => { | ||
logger.debug("Received equal data event"); | ||
handleCallbacks(cacheData.response); // Must be first | ||
await actions.setRefreshed(isRefreshed, false); | ||
await actions.setTimestamp(new Date(timestamp), false); | ||
await actions.setLoading(false, false); | ||
}; | ||
const revalidate = (invalidateKey?: string | FetchCommandInstance | RegExp) => { | ||
const invalidate = (invalidateKey?: string | FetchCommandInstance | RegExp) => { | ||
if (invalidateKey && invalidateKey instanceof FetchCommand) { | ||
@@ -94,50 +41,9 @@ cache.events.revalidate(`/${getCommandKey(invalidateKey, true)}/`); | ||
const handleGetLoadingEvent = ({ isLoading }: QueueLoadingEventType) => { | ||
actions.setLoading(isLoading, false); | ||
const handlers = { | ||
actions: actions.actions, | ||
onCacheError: actions.onError, | ||
onCacheSuccess: actions.onSuccess, | ||
onCacheChange: actions.onFinished, | ||
}; | ||
const handleMountEvents = () => { | ||
const loadingUnmount = fetchQueue.events.onLoading(queueKey, handleGetLoadingEvent); | ||
const getUnmount = cache.events.get<T>(cacheKey, handleGetCacheData); | ||
const getEqualDataUnmount = cache.events.getEqualData<T>(cacheKey, handleGetEqualCacheUpdate); | ||
return () => { | ||
loadingUnmount(); | ||
getUnmount(); | ||
getEqualDataUnmount(); | ||
}; | ||
}; | ||
const handleDependencyTracking = () => { | ||
if (!dependencyTracking) { | ||
Object.keys(state).forEach((key) => setRenderKey(key as Parameters<typeof setRenderKey>[0])); | ||
} | ||
}; | ||
/** | ||
* Initialization of the events related to data exchange with cache and queue | ||
* This allow to share the state with other hooks and keep it related | ||
*/ | ||
useDidUpdate( | ||
() => { | ||
handleDependencyTracking(); | ||
return handleMountEvents(); | ||
}, | ||
[JSON.stringify(commandDump)], | ||
true, | ||
); | ||
/** | ||
* On the init if we have data we should call the callback functions | ||
*/ | ||
useDidUpdate( | ||
() => { | ||
if (initialized) { | ||
handleCallbacks([state.data, state.error, state.status]); | ||
} | ||
}, | ||
[JSON.stringify(commandDump), initialized], | ||
true, | ||
); | ||
return { | ||
@@ -197,17 +103,5 @@ get data() { | ||
}, | ||
onSuccess: (callback: OnCacheSuccessCallbackType<ExtractResponse<T>>) => { | ||
onSuccessCallback.current = callback; | ||
}, | ||
onError: (callback: OnCacheErrorCallbackType<ExtractError<T>>) => { | ||
onErrorCallback.current = callback; | ||
}, | ||
onFinished: (callback: OnCacheFinishedCallbackType<ExtractFetchReturn<T>>) => { | ||
onFinishedCallback.current = callback; | ||
}, | ||
onChange: (callback: OnCacheChangeCallbackType<ExtractFetchReturn<T>>) => { | ||
onChangeCallback.current = callback; | ||
}, | ||
...actions, | ||
revalidate, | ||
...handlers, | ||
invalidate, | ||
}; | ||
}; |
@@ -7,6 +7,7 @@ import { | ||
CacheValueType, | ||
QueueLoadingEventType, | ||
} from "@better-typed/hyper-fetch"; | ||
import { UseDependentStateActions, UseDependentStateType } from "use-dependent-state"; | ||
import { OnErrorCallbackType, OnFinishedCallbackType, OnSuccessCallbackType } from "utils/use-command-state"; | ||
import { UseDependentStateActions, UseDependentStateType } from "utils/use-dependent-state"; | ||
import { isEqual } from "utils"; | ||
@@ -16,2 +17,3 @@ export type UseCacheOptionsType<T extends FetchCommandInstance> = { | ||
initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["response"] | null; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
@@ -22,17 +24,10 @@ | ||
ExtractError<T> | ||
> & | ||
UseDependentStateActions<ExtractResponse<T>, ExtractError<T>> & { | ||
onSuccess: (callback: OnCacheSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnCacheErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnCacheFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onChange: (callback: OnCacheChangeCallbackType<ExtractFetchReturn<T>>) => void; | ||
isStale: boolean; | ||
isRefreshingError: boolean; | ||
revalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
}; | ||
export type OnCacheRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
export type OnCacheSuccessCallbackType<DataType> = (data: DataType) => void; | ||
export type OnCacheErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
export type OnCacheFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
export type OnCacheChangeCallbackType<ResponseType> = (response: ResponseType) => void; | ||
> & { | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onCacheSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onCacheError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onCacheChange: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
isStale: boolean; | ||
isRefreshingError: boolean; | ||
invalidate: (invalidateKey?: string | FetchCommandInstance) => void; | ||
}; |
@@ -17,3 +17,3 @@ import { DateInterval } from "@better-typed/hyper-fetch"; | ||
debounceTime: 400, // milliseconds | ||
shouldThrow: false, | ||
deepCompare: true, | ||
}; |
import { useRef } from "react"; | ||
import { useDidUpdate } from "@better-typed/react-lifecycle-hooks"; | ||
import { | ||
FetchProgressType, | ||
QueueLoadingEventType, | ||
FetchCommandInstance, | ||
FetchCommand, | ||
getCommandKey, | ||
CacheValueType, | ||
ExtractResponse, | ||
ExtractError, | ||
ExtractFetchReturn, | ||
} from "@better-typed/hyper-fetch"; | ||
import { FetchCommandInstance, FetchCommand, getCommandKey } from "@better-typed/hyper-fetch"; | ||
import { useDependentState } from "use-dependent-state"; | ||
import { useDebounce } from "use-debounce"; | ||
import { useInterval } from "use-interval"; | ||
import { useDebounce } from "utils/use-debounce"; | ||
import { useInterval } from "utils/use-interval"; | ||
import { isStaleCacheData } from "utils"; | ||
import { | ||
OnFetchProgressCallbackType, | ||
OnFetchStartCallbackType, | ||
OnFetchRequestCallbackType, | ||
OnFetchErrorCallbackType, | ||
OnFetchFinishedCallbackType, | ||
OnFetchSuccessCallbackType, | ||
UseFetchOptionsType, | ||
UseFetchReturnType, | ||
useFetchDefaultOptions, | ||
} from "use-fetch"; | ||
import { UseFetchOptionsType, UseFetchReturnType, useFetchDefaultOptions } from "use-fetch"; | ||
import { useCommandState } from "utils/use-command-state"; | ||
// TBD - suspense | ||
export const useFetch = <T extends FetchCommandInstance>( | ||
@@ -49,5 +28,5 @@ command: T, | ||
debounceTime = useFetchDefaultOptions.debounceTime, | ||
// suspense = useFetchDefaultOptions.suspense | ||
shouldThrow = useFetchDefaultOptions.shouldThrow, | ||
}: UseFetchOptionsType<T> = useFetchDefaultOptions, | ||
deepCompare = useFetchDefaultOptions.deepCompare, | ||
}: // suspense = useFetchDefaultOptions.suspense | ||
UseFetchOptionsType<T> = useFetchDefaultOptions, | ||
): UseFetchReturnType<T> => { | ||
@@ -60,17 +39,15 @@ const { cacheTime, cacheKey, queueKey, builder } = command; | ||
const { cache, fetchQueue, appManager, commandManager, loggerManager } = builder; | ||
const unmountCallbacks = useRef<null | VoidFunction>(null); | ||
const { cache, fetchQueue, appManager, loggerManager } = builder; | ||
const logger = useRef(loggerManager.init("useFetch")).current; | ||
const [state, actions, setRenderKey, initialized] = useDependentState<T>(command, initialData, fetchQueue, [ | ||
JSON.stringify(commandDump), | ||
]); | ||
const [state, actions, { setRenderKey, initialized }] = useCommandState({ | ||
command, | ||
queue: fetchQueue, | ||
dependencyTracking, | ||
initialData, | ||
logger, | ||
deepCompare, | ||
}); | ||
const onRequestCallback = useRef<null | OnFetchRequestCallbackType>(null); | ||
const onSuccessCallback = useRef<null | OnFetchSuccessCallbackType<ExtractResponse<T>>>(null); | ||
const onErrorCallback = useRef<null | OnFetchErrorCallbackType<ExtractError<T>>>(null); | ||
const onFinishedCallback = useRef<null | OnFetchFinishedCallbackType<ExtractFetchReturn<T>>>(null); | ||
const onRequestStartCallback = useRef<null | OnFetchStartCallbackType<T>>(null); | ||
const onResponseStartCallback = useRef<null | OnFetchStartCallbackType<T>>(null); | ||
const onDownloadProgressCallback = useRef<null | OnFetchProgressCallbackType>(null); | ||
const onUploadProgressCallback = useRef<null | OnFetchProgressCallbackType>(null); | ||
const handleFetch = () => { | ||
@@ -138,50 +115,2 @@ /** | ||
const handleCallbacks = (response: ExtractFetchReturn<T> | undefined) => { | ||
if (response) { | ||
const status = response[2] || 0; | ||
const hasSuccessState = !!(response[0] && !response[1]); | ||
const hasSuccessStatus = !!(!response[1] && status >= 200 && status <= 400); | ||
if (hasSuccessState || hasSuccessStatus) { | ||
onSuccessCallback?.current?.(response[0] as ExtractResponse<T>); | ||
} else { | ||
onErrorCallback?.current?.(response[1] as ExtractError<T>); | ||
if (shouldThrow) { | ||
throw { | ||
message: "Fetching Error.", | ||
error: response[1], | ||
}; | ||
} | ||
} | ||
onFinishedCallback?.current?.(response); | ||
} else { | ||
logger.debug("No response to perform callbacks"); | ||
} | ||
}; | ||
const handleGetCacheData = async (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => { | ||
logger.debug("Received new data"); | ||
handleCallbacks(cacheData.response); // Must be first | ||
await actions.setCacheData(cacheData, false); | ||
await actions.setLoading(false, false); | ||
handleRefresh(); | ||
}; | ||
const handleGetEqualCacheUpdate = async ( | ||
cacheData: CacheValueType<ExtractResponse<T>>, | ||
isRefreshed: boolean, | ||
timestamp: number, | ||
) => { | ||
logger.debug("Received equal data event"); | ||
handleCallbacks(cacheData.response); // Must be first | ||
await actions.setRefreshed(isRefreshed, false); | ||
await actions.setTimestamp(new Date(timestamp), false); | ||
await actions.setLoading(false, false); | ||
handleRefresh(); | ||
}; | ||
const handleGetLoadingEvent = ({ isLoading, isRetry }: QueueLoadingEventType) => { | ||
actions.setLoading(isLoading, false); | ||
onRequestCallback.current?.({ isRetry }); | ||
}; | ||
const handleRevalidate = () => { | ||
@@ -201,18 +130,2 @@ handleFetch(); | ||
const handleDownloadProgress = (progress: FetchProgressType) => { | ||
onDownloadProgressCallback?.current?.(progress); | ||
}; | ||
const handleUploadProgress = (progress: FetchProgressType) => { | ||
onUploadProgressCallback?.current?.(progress); | ||
}; | ||
const handleRequestStart = (middleware: T) => { | ||
onRequestStartCallback?.current?.(middleware); | ||
}; | ||
const handleResponseStart = (middleware: T) => { | ||
onRequestStartCallback?.current?.(middleware); | ||
}; | ||
const handleMountEvents = () => { | ||
@@ -235,13 +148,5 @@ const focusUnmount = appManager.events.onFocus(() => { | ||
const downloadUnmount = commandManager.events.onDownloadProgress(queueKey, handleDownloadProgress); | ||
const uploadUnmount = commandManager.events.onUploadProgress(queueKey, handleUploadProgress); | ||
const requestStartUnmount = commandManager.events.onRequestStart(queueKey, handleRequestStart); | ||
const responseStartUnmount = commandManager.events.onResponseStart(queueKey, handleResponseStart); | ||
const loadingUnmount = fetchQueue.events.onLoading(queueKey, handleGetLoadingEvent); | ||
const getUnmount = cache.events.get<T>(cacheKey, handleGetCacheData); | ||
const getEqualDataUnmount = cache.events.getEqualData<T>(cacheKey, handleGetEqualCacheUpdate); | ||
const revalidateUnmount = cache.events.onRevalidate(cacheKey, handleRevalidate); | ||
return () => { | ||
const unmount = () => { | ||
focusUnmount(); | ||
@@ -251,19 +156,9 @@ blurUnmount(); | ||
offlineUnmount(); | ||
downloadUnmount(); | ||
uploadUnmount(); | ||
requestStartUnmount(); | ||
responseStartUnmount(); | ||
loadingUnmount(); | ||
getUnmount(); | ||
getEqualDataUnmount(); | ||
revalidateUnmount(); | ||
}; | ||
}; | ||
const handleDependencyTracking = () => { | ||
if (!dependencyTracking) { | ||
Object.keys(state).forEach((key) => setRenderKey(key as Parameters<typeof setRenderKey>[0])); | ||
} | ||
unmountCallbacks.current?.(); | ||
unmountCallbacks.current = unmount; | ||
return unmount; | ||
}; | ||
@@ -285,25 +180,5 @@ | ||
*/ | ||
useDidUpdate( | ||
() => { | ||
handleDependencyTracking(); | ||
return handleMountEvents(); | ||
}, | ||
[JSON.stringify(commandDump)], | ||
true, | ||
); | ||
useDidUpdate(handleMountEvents, [JSON.stringify(commandDump)], true); | ||
/** | ||
* On the init if we have data we should call the callback functions | ||
*/ | ||
useDidUpdate( | ||
() => { | ||
if (initialized) { | ||
handleCallbacks([state.data, state.error, state.status]); | ||
} | ||
}, | ||
[JSON.stringify(commandDump), initialized], | ||
true, | ||
); | ||
/** | ||
* Fetching logic for updates handling | ||
@@ -386,27 +261,3 @@ */ | ||
}, | ||
onRequest: (callback: OnFetchRequestCallbackType) => { | ||
onRequestCallback.current = callback; | ||
}, | ||
onSuccess: (callback: OnFetchSuccessCallbackType<ExtractResponse<T>>) => { | ||
onSuccessCallback.current = callback; | ||
}, | ||
onError: (callback: OnFetchErrorCallbackType<ExtractError<T>>) => { | ||
onErrorCallback.current = callback; | ||
}, | ||
onFinished: (callback: OnFetchFinishedCallbackType<ExtractFetchReturn<T>>) => { | ||
onFinishedCallback.current = callback; | ||
}, | ||
onRequestStart: (callback: OnFetchStartCallbackType<T>) => { | ||
onRequestStartCallback.current = callback; | ||
}, | ||
onResponseStart: (callback: OnFetchStartCallbackType<T>) => { | ||
onResponseStartCallback.current = callback; | ||
}, | ||
onDownloadProgress: (callback: OnFetchProgressCallbackType) => { | ||
onDownloadProgressCallback.current = callback; | ||
}, | ||
onUploadProgress: (callback: OnFetchProgressCallbackType) => { | ||
onUploadProgressCallback.current = callback; | ||
}, | ||
actions, | ||
...actions, | ||
isDebouncing: requestDebounce.active, | ||
@@ -413,0 +264,0 @@ refresh: refreshFn, |
@@ -7,7 +7,14 @@ import { | ||
CacheValueType, | ||
QueueLoadingEventType, | ||
FetchProgressType, | ||
} from "@better-typed/hyper-fetch"; | ||
import { UseDependentStateActions, UseDependentStateType } from "use-dependent-state"; | ||
import { | ||
OnErrorCallbackType, | ||
OnFinishedCallbackType, | ||
OnProgressCallbackType, | ||
OnRequestCallbackType, | ||
OnStartCallbackType, | ||
OnSuccessCallbackType, | ||
} from "utils/use-command-state"; | ||
import { UseDependentStateActions, UseDependentStateType } from "utils/use-dependent-state"; | ||
import { isEqual } from "utils"; | ||
@@ -29,19 +36,18 @@ export type UseFetchOptionsType<T extends FetchCommandInstance> = { | ||
suspense?: boolean; | ||
shouldThrow?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
export type UseFetchReturnType<T extends FetchCommandInstance> = Omit< | ||
UseDependentStateType<ExtractResponse<T>, ExtractError<T>>, | ||
"data" | ||
export type UseFetchReturnType<T extends FetchCommandInstance> = UseDependentStateType< | ||
ExtractResponse<T>, | ||
ExtractError<T> | ||
> & { | ||
data: null | ExtractResponse<T>; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onRequest: (callback: OnFetchRequestCallbackType) => void; | ||
onSuccess: (callback: OnFetchSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnFetchErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFetchFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnFetchStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnFetchProgressCallbackType) => void; | ||
onRequest: (callback: OnRequestCallbackType) => void; | ||
onSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onUploadProgress: (callback: OnProgressCallbackType) => void; | ||
isRefreshed: boolean; | ||
@@ -53,8 +59,1 @@ isRefreshingError: boolean; | ||
}; | ||
export type OnFetchRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
export type OnFetchSuccessCallbackType<DataType> = (data: DataType) => void; | ||
export type OnFetchErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
export type OnFetchFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
export type OnFetchStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
export type OnFetchProgressCallbackType = (progress: FetchProgressType) => void; |
@@ -17,2 +17,4 @@ import { useState, useRef } from "react"; | ||
const unmountCallbacks = useRef<null | VoidFunction>(null); | ||
const [connecting, setConnected] = useState(true); | ||
@@ -51,3 +53,3 @@ const [stopped, setStopped] = useState(false); | ||
return () => { | ||
const unmount = () => { | ||
unmountStatus(); | ||
@@ -58,2 +60,7 @@ unmountChange(); | ||
}; | ||
unmountCallbacks.current?.(); | ||
unmountCallbacks.current = unmount; | ||
return unmount; | ||
}; | ||
@@ -60,0 +67,0 @@ |
@@ -11,2 +11,3 @@ export const useSubmitDefaultOptions = { | ||
invalidate: [], | ||
deepCompare: true, | ||
}; |
import { useRef } from "react"; | ||
import { | ||
ExtractResponse, | ||
FetchCommandInstance, | ||
FetchProgressType, | ||
ExtractError, | ||
ExtractFetchReturn, | ||
QueueLoadingEventType, | ||
CacheValueType, | ||
getCommandKey, | ||
FetchCommand, | ||
} from "@better-typed/hyper-fetch"; | ||
import { useDidUpdate } from "@better-typed/react-lifecycle-hooks"; | ||
import { FetchCommandInstance, getCommandKey, FetchCommand } from "@better-typed/hyper-fetch"; | ||
import { isStaleCacheData } from "utils"; | ||
import { useDependentState } from "use-dependent-state"; | ||
import { useDebounce } from "use-debounce"; | ||
import { | ||
UseSubmitOptionsType, | ||
UseSubmitReturnType, | ||
OnSubmitSuccessCallbackType, | ||
OnSubmitErrorCallbackType, | ||
OnSubmitRequestCallbackType, | ||
OnSubmitFinishedCallbackType, | ||
OnSubmitStartCallbackType, | ||
OnSubmitProgressCallbackType, | ||
useSubmitDefaultOptions, | ||
} from "use-submit"; | ||
import { useDebounce } from "utils/use-debounce"; | ||
import { UseSubmitOptionsType, UseSubmitReturnType, useSubmitDefaultOptions } from "use-submit"; | ||
import { useCommandState } from "utils/use-command-state"; | ||
@@ -38,24 +17,19 @@ export const useSubmit = <T extends FetchCommandInstance>( | ||
debounceTime = useSubmitDefaultOptions.debounceTime, | ||
// suspense = useSubmitDefaultOptions.suspense, | ||
shouldThrow = useSubmitDefaultOptions.shouldThrow, | ||
}: UseSubmitOptionsType<T> = useSubmitDefaultOptions, | ||
deepCompare = useSubmitDefaultOptions.deepCompare, | ||
}: // suspense = useSubmitDefaultOptions.suspense, | ||
UseSubmitOptionsType<T> = useSubmitDefaultOptions, | ||
): UseSubmitReturnType<T> => { | ||
const { cacheTime, cacheKey, queueKey, builder, invalidate } = command; | ||
const { cacheTime, builder } = command; | ||
const requestDebounce = useDebounce(debounceTime); | ||
const { cache, submitQueue, commandManager, loggerManager } = builder; | ||
const { cache, submitQueue, loggerManager } = builder; | ||
const logger = useRef(loggerManager.init("useSubmit")).current; | ||
const commandDump = command.dump(); | ||
const [state, actions, setRenderKey] = useDependentState<T>(command, initialData, submitQueue, [ | ||
JSON.stringify(commandDump), | ||
]); | ||
const [state, actions, { setRenderKey }] = useCommandState({ | ||
command, | ||
queue: submitQueue, | ||
dependencyTracking, | ||
initialData, | ||
logger, | ||
deepCompare, | ||
}); | ||
const onRequestCallback = useRef<null | OnSubmitRequestCallbackType>(null); | ||
const onSuccessCallback = useRef<null | OnSubmitSuccessCallbackType<ExtractResponse<T>>>(null); | ||
const onErrorCallback = useRef<null | OnSubmitErrorCallbackType<ExtractError<T>>>(null); | ||
const onFinishedCallback = useRef<null | OnSubmitFinishedCallbackType<ExtractFetchReturn<T>>>(null); | ||
const onRequestStartCallback = useRef<null | OnSubmitStartCallbackType<T>>(null); | ||
const onResponseStartCallback = useRef<null | OnSubmitStartCallbackType<T>>(null); | ||
const onDownloadProgressCallback = useRef<null | OnSubmitProgressCallbackType>(null); | ||
const onUploadProgressCallback = useRef<null | OnSubmitProgressCallbackType>(null); | ||
const handleSubmit = (...parameters: Parameters<T["send"]>) => { | ||
@@ -79,3 +53,3 @@ const options = parameters[0]; | ||
const invalidateFn = (invalidateKey: string | FetchCommandInstance | RegExp) => { | ||
const invalidate = (invalidateKey: string | FetchCommandInstance | RegExp) => { | ||
if (!invalidateKey) return; | ||
@@ -90,104 +64,14 @@ | ||
const handleCallbacks = (response: ExtractFetchReturn<T> | undefined) => { | ||
if (response) { | ||
const status = response[2] || 0; | ||
const hasSuccessState = !!(response[0] && !response[1]); | ||
const hasSuccessStatus = !!(!response[1] && status >= 200 && status <= 400); | ||
if (hasSuccessState || hasSuccessStatus) { | ||
onSuccessCallback?.current?.(response[0] as ExtractResponse<T>); | ||
invalidate?.forEach((key) => invalidateFn(key)); | ||
} else { | ||
onErrorCallback?.current?.(response[1] as ExtractError<T>); | ||
if (shouldThrow) { | ||
throw { | ||
message: "Fetching Error.", | ||
error: response[1], | ||
}; | ||
} | ||
} | ||
onFinishedCallback?.current?.(response); | ||
} else { | ||
logger.debug("No response to perform callbacks"); | ||
} | ||
const handlers = { | ||
actions: actions.actions, | ||
onSubmitRequest: actions.onRequest, | ||
onSubmitSuccess: actions.onSuccess, | ||
onSubmitError: actions.onError, | ||
onSubmitFinished: actions.onFinished, | ||
onSubmitRequestStart: actions.onRequestStart, | ||
onSubmitResponseStart: actions.onResponseStart, | ||
onSubmitDownloadProgress: actions.onDownloadProgress, | ||
onSubmitUploadProgress: actions.onUploadProgress, | ||
}; | ||
const handleGetCacheData = (cacheData: CacheValueType<ExtractResponse<T>, ExtractError<T>>) => { | ||
handleCallbacks(cacheData.response); // Must be first | ||
actions.setLoading(false, false); | ||
actions.setCacheData(cacheData, false); | ||
}; | ||
const handleGetEqualCacheUpdate = ( | ||
cacheData: CacheValueType<ExtractResponse<T>>, | ||
isRefreshed: boolean, | ||
timestamp: number, | ||
) => { | ||
handleCallbacks(cacheData.response); // Must be first | ||
actions.setRefreshed(isRefreshed, false); | ||
actions.setTimestamp(new Date(timestamp), false); | ||
actions.setLoading(false, false); | ||
}; | ||
const handleGetLoadingEvent = ({ isLoading, isRetry }: QueueLoadingEventType) => { | ||
actions.setLoading(isLoading, false); | ||
onRequestCallback.current?.({ isRetry }); | ||
}; | ||
const handleDownloadProgress = (progress: FetchProgressType) => { | ||
onDownloadProgressCallback?.current?.(progress); | ||
}; | ||
const handleUploadProgress = (progress: FetchProgressType) => { | ||
onUploadProgressCallback?.current?.(progress); | ||
}; | ||
const handleRequestStart = (middleware: T) => { | ||
onRequestStartCallback?.current?.(middleware); | ||
}; | ||
const handleResponseStart = (middleware: T) => { | ||
onRequestStartCallback?.current?.(middleware); | ||
}; | ||
const handleMountEvents = () => { | ||
const downloadUnmount = commandManager.events.onDownloadProgress(queueKey, handleDownloadProgress); | ||
const uploadUnmount = commandManager.events.onUploadProgress(queueKey, handleUploadProgress); | ||
const requestStartUnmount = commandManager.events.onRequestStart(queueKey, handleRequestStart); | ||
const responseStartUnmount = commandManager.events.onResponseStart(queueKey, handleResponseStart); | ||
const loadingUnmount = submitQueue.events.onLoading(queueKey, handleGetLoadingEvent); | ||
const getUnmount = cache.events.get<T>(cacheKey, handleGetCacheData); | ||
const getEqualDataUnmount = cache.events.getEqualData<T>(cacheKey, handleGetEqualCacheUpdate); | ||
return () => { | ||
downloadUnmount(); | ||
uploadUnmount(); | ||
requestStartUnmount(); | ||
responseStartUnmount(); | ||
loadingUnmount(); | ||
getUnmount(); | ||
getEqualDataUnmount(); | ||
}; | ||
}; | ||
const handleDependencyTracking = () => { | ||
if (!dependencyTracking) { | ||
Object.keys(state).forEach((key) => setRenderKey(key as Parameters<typeof setRenderKey>[0])); | ||
} | ||
}; | ||
/** | ||
* Initialization of the events related to data exchange with cache and queue | ||
* This allow to share the state with other hooks and keep it related | ||
*/ | ||
useDidUpdate( | ||
() => { | ||
handleDependencyTracking(); | ||
return handleMountEvents(); | ||
}, | ||
[JSON.stringify(commandDump)], | ||
true, | ||
); | ||
return { | ||
@@ -234,31 +118,7 @@ submit: handleSubmit, | ||
}, | ||
onSubmitRequest: (callback: OnSubmitRequestCallbackType) => { | ||
onRequestCallback.current = callback; | ||
}, | ||
onSubmitSuccess: (callback: OnSubmitSuccessCallbackType<ExtractResponse<T>>) => { | ||
onSuccessCallback.current = callback; | ||
}, | ||
onSubmitError: (callback: OnSubmitErrorCallbackType<ExtractError<T>>) => { | ||
onErrorCallback.current = callback; | ||
}, | ||
onSubmitFinished: (callback: OnSubmitFinishedCallbackType<ExtractFetchReturn<T>>) => { | ||
onFinishedCallback.current = callback; | ||
}, | ||
onSubmitRequestStart: (callback: OnSubmitStartCallbackType<T>) => { | ||
onRequestStartCallback.current = callback; | ||
}, | ||
onSubmitResponseStart: (callback: OnSubmitStartCallbackType<T>) => { | ||
onResponseStartCallback.current = callback; | ||
}, | ||
onSubmitDownloadProgress: (callback: OnSubmitProgressCallbackType) => { | ||
onDownloadProgressCallback.current = callback; | ||
}, | ||
onSubmitUploadProgress: (callback: OnSubmitProgressCallbackType) => { | ||
onUploadProgressCallback.current = callback; | ||
}, | ||
actions, | ||
...handlers, | ||
isDebouncing: false, | ||
isRefreshed: false, | ||
revalidate: invalidateFn, | ||
invalidate, | ||
}; | ||
}; |
import { | ||
FetchProgressType, | ||
QueueLoadingEventType, | ||
CacheValueType, | ||
@@ -11,4 +9,14 @@ ExtractError, | ||
import { UseDependentStateType, UseDependentStateActions } from "use-dependent-state"; | ||
import { isEqual } from "utils"; | ||
import { | ||
OnErrorCallbackType, | ||
OnFinishedCallbackType, | ||
OnProgressCallbackType, | ||
OnRequestCallbackType, | ||
OnStartCallbackType, | ||
OnSuccessCallbackType, | ||
} from "utils/use-command-state"; | ||
import { UseDependentStateType, UseDependentStateActions } from "utils/use-dependent-state"; | ||
export type UseSubmitOptionsType<T extends FetchCommandInstance> = { | ||
@@ -24,2 +32,3 @@ disabled?: boolean; | ||
dependencyTracking?: boolean; | ||
deepCompare?: boolean | typeof isEqual; | ||
}; | ||
@@ -31,23 +40,16 @@ | ||
> & { | ||
onSubmitRequest: (callback: OnSubmitRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSubmitSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnSubmitErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnSubmitFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnSubmitStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnSubmitProgressCallbackType) => void; | ||
revalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
onSubmitRequest: (callback: OnRequestCallbackType) => void; | ||
onSubmitSuccess: (callback: OnSuccessCallbackType<ExtractResponse<T>>) => void; | ||
onSubmitError: (callback: OnErrorCallbackType<ExtractError<T>>) => void; | ||
onSubmitFinished: (callback: OnFinishedCallbackType<ExtractFetchReturn<T>>) => void; | ||
onSubmitRequestStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitResponseStart: (callback: OnStartCallbackType<T>) => void; | ||
onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void; | ||
onSubmitUploadProgress: (callback: OnProgressCallbackType) => void; | ||
submit: (...parameters: Parameters<T["send"]>) => void; | ||
actions: UseDependentStateActions<ExtractResponse<T>, ExtractError<T>>; | ||
submitting: boolean; | ||
isStale: boolean; | ||
isDebouncing: boolean; | ||
invalidate: (invalidateKey: string | FetchCommandInstance | RegExp) => void; | ||
}; | ||
export type OnSubmitRequestCallbackType = (options: Omit<QueueLoadingEventType, "isLoading">) => void; | ||
export type OnSubmitSuccessCallbackType<DataType> = (data: DataType) => void; | ||
export type OnSubmitErrorCallbackType<ErrorType> = (error: ErrorType) => void; | ||
export type OnSubmitFinishedCallbackType<ResponseType> = (response: ResponseType) => void; | ||
export type OnSubmitStartCallbackType<T extends FetchCommandInstance> = (middleware: T) => void; | ||
export type OnSubmitProgressCallbackType = (progress: FetchProgressType) => void; |
export * from "./cache.utils"; | ||
export * from "./deep-equal.utils"; |
{ | ||
"name": "@better-typed/react-hyper-fetch", | ||
"version": "0.0.34", | ||
"version": "0.0.35", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "React hooks and utils for the hyper-fetch", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44
347716
2249
1