@hyper-fetch/react 5.6.0 → 5.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,6 +37,7 @@ export const useRequestEvents = <T extends RequestInstance>({
37
37
  actions,
38
38
  setCacheData,
39
39
  }: UseRequestEventsPropsType<T>): UseRequestEventsReturnType<T> => {
40
+ const { responseMapper } = request;
40
41
  const { cache, requestManager } = request.client;
41
42
 
42
43
  // ******************
@@ -136,10 +137,18 @@ export const useRequestEvents = <T extends RequestInstance>({
136
137
 
137
138
  const handleResponse = (req: T) => {
138
139
  return (
139
- data: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
140
+ response: ResponseReturnType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
140
141
  details: ResponseDetailsType,
141
142
  ) => {
142
- handleResponseCallbacks(req, data, details);
143
+ const data = responseMapper ? responseMapper(response) : response;
144
+
145
+ if (data instanceof Promise) {
146
+ return (async () => {
147
+ const promiseData = await data;
148
+ handleResponseCallbacks(req, promiseData, details);
149
+ })();
150
+ }
151
+ return handleResponseCallbacks(req, data, details);
143
152
  };
144
153
  };
145
154
 
@@ -47,8 +47,7 @@ export const useTrackedState = <T extends RequestInstance>({
47
47
 
48
48
  const getStaleStatus = (): boolean => {
49
49
  const cacheData = cache.get(cacheKey);
50
-
51
- return isStaleCacheData(cacheTime, cacheData?.timestamp || state.current.timestamp);
50
+ return !cacheData || isStaleCacheData(cacheTime, cacheData?.timestamp);
52
51
  };
53
52
 
54
53
  // ******************
@@ -83,10 +82,14 @@ export const useTrackedState = <T extends RequestInstance>({
83
82
  const shouldLoadInitialCache = !hasState && !!state.current.data;
84
83
  const shouldRemovePreviousData = hasState && !state.current.data;
85
84
 
86
- if (shouldLoadInitialCache || shouldRemovePreviousData) {
85
+ if (newState.data || shouldLoadInitialCache || shouldRemovePreviousData) {
87
86
  // Don't update the state when we are fetching data for new cacheKey
88
87
  // So on paginated page we will have previous page access until the new one will be fetched
88
+ // However: When we have some cached data, we can use it right away
89
89
  state.current = newState;
90
+ if (state.current) {
91
+ renderKeyTrigger(["data"]);
92
+ }
90
93
  }
91
94
  },
92
95
  [cacheKey, queueKey],
@@ -125,26 +128,6 @@ export const useTrackedState = <T extends RequestInstance>({
125
128
  return false;
126
129
  };
127
130
 
128
- const mapResponseData = (
129
- data: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
130
- ):
131
- | Promise<CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>>
132
- | CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>> => {
133
- if (responseMapper) {
134
- const response = responseMapper(data);
135
- if (response instanceof Promise) {
136
- return new Promise((resolve) => {
137
- (async () => {
138
- const newResponse = await response;
139
- resolve({ ...data, ...newResponse });
140
- })();
141
- });
142
- }
143
- return { ...data, ...response };
144
- }
145
- return data;
146
- };
147
-
148
131
  const handleCacheData = (
149
132
  cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
150
133
  ) => {
@@ -178,15 +161,15 @@ export const useTrackedState = <T extends RequestInstance>({
178
161
  const setCacheData = (
179
162
  cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
180
163
  ): Promise<void> | void => {
181
- const data = mapResponseData(cacheData);
164
+ const data = responseMapper ? responseMapper(cacheData) : cacheData;
182
165
 
183
166
  if (data instanceof Promise) {
184
167
  return (async () => {
185
168
  const promiseData = await data;
186
- handleCacheData(promiseData);
169
+ handleCacheData({ ...cacheData, ...promiseData });
187
170
  })();
188
171
  }
189
- return handleCacheData(data);
172
+ return handleCacheData({ ...cacheData, ...data });
190
173
  };
191
174
 
192
175
  // ******************
@@ -99,7 +99,10 @@ export const useFetch = <RequestType extends RequestInstance>(
99
99
  // ******************
100
100
 
101
101
  function handleRefresh() {
102
- if (!refresh) return;
102
+ if (!refresh) {
103
+ refreshDebounce.reset();
104
+ return;
105
+ }
103
106
 
104
107
  refreshDebounce.debounce(() => {
105
108
  const isBlurred = !appManager.isFocused;
@@ -159,12 +162,13 @@ export const useFetch = <RequestType extends RequestInstance>(
159
162
  };
160
163
 
161
164
  const updateFetchData = () => {
165
+ const hasStaleData = getStaleStatus();
166
+ const shouldUpdate = !revalidate ? hasStaleData : true;
162
167
  /**
163
168
  * This is a hack to avoid double rendering in React 18
164
169
  * It renders initial mount event and allow us to consume only hook updates
165
170
  */
166
-
167
- if (!ignoreReact18DoubleRender.current) {
171
+ if (!ignoreReact18DoubleRender.current && shouldUpdate) {
168
172
  /**
169
173
  * While debouncing we need to make sure that first request is not debounced when the cache is not available
170
174
  * This way it will not wait for debouncing but fetch data right away
@@ -40,7 +40,7 @@ export const useSubmit = <RequestType extends RequestInstance>(
40
40
  ...options,
41
41
  }),
42
42
  // eslint-disable-next-line react-hooks/exhaustive-deps
43
- [JSON.stringify(globalConfig.useSubmitConfig), JSON.stringify(options)],
43
+ [globalConfig.useSubmitConfig, JSON.stringify(options), options.deepCompare],
44
44
  );
45
45
  const { disabled, dependencyTracking, initialData, bounce, bounceType, bounceTime, deepCompare } = mergedOptions;
46
46