@hyper-fetch/react 6.0.0 → 6.0.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.
@@ -64,11 +64,48 @@ export const getTimestamp = (timestamp?: NullableType<number | Date>) => {
64
64
  return timestamp ? new Date(timestamp) : null;
65
65
  };
66
66
 
67
- export const getInitialState = <T extends RequestInstance>(
68
- initialData: NullableType<Partial<ExtractAdapterReturnType<T>>>,
69
- dispatcher: Dispatcher,
70
- request: T,
71
- ): UseTrackedStateType<T> => {
67
+ export const getIsInitiallyLoading = ({
68
+ queryKey,
69
+ dispatcher,
70
+ hasState,
71
+ revalidate,
72
+ disabled,
73
+ }: {
74
+ queryKey: string;
75
+ dispatcher: Dispatcher;
76
+ hasState: boolean;
77
+ revalidate?: boolean;
78
+ disabled?: boolean;
79
+ }) => {
80
+ if (!revalidate && hasState) {
81
+ return false;
82
+ }
83
+
84
+ const queue = dispatcher.getQueue(queryKey);
85
+ const isInitiallyLoading = dispatcher.hasRunningRequests(queryKey) || (!queue.stopped && disabled === false);
86
+
87
+ return isInitiallyLoading;
88
+ };
89
+
90
+ export const getInitialState = <T extends RequestInstance>({
91
+ initialResponse,
92
+ dispatcher,
93
+ request,
94
+ disabled,
95
+ revalidate,
96
+ }: {
97
+ initialResponse: NullableType<Partial<ExtractAdapterReturnType<T>>>;
98
+ dispatcher: Dispatcher;
99
+ request: T;
100
+ /**
101
+ * useFetch only
102
+ */
103
+ disabled?: boolean;
104
+ /**
105
+ * useFetch only
106
+ */
107
+ revalidate?: boolean;
108
+ }): UseTrackedStateType<T> => {
72
109
  const { client, cacheKey, responseMapper } = request;
73
110
  const { cache } = client;
74
111
 
@@ -77,8 +114,15 @@ export const getInitialState = <T extends RequestInstance>(
77
114
  ExtractErrorType<T>,
78
115
  ExtractAdapterExtraType<ExtractAdapterType<T>>
79
116
  >(cacheKey);
80
- const cacheState = getValidCacheData<T>(request, initialData, cacheData);
81
- const initialLoading = dispatcher.hasRunningRequests(request.queueKey);
117
+ const cacheState = getValidCacheData<T>(request, initialResponse, cacheData);
118
+
119
+ const initialLoading = getIsInitiallyLoading({
120
+ queryKey: request.queueKey,
121
+ dispatcher,
122
+ disabled,
123
+ revalidate,
124
+ hasState: !!cacheState,
125
+ });
82
126
 
83
127
  if (cacheState) {
84
128
  const mappedData = responseMapper ? responseMapper(cacheState) : cacheState;
@@ -71,6 +71,8 @@ export const useFetch = <RequestType extends RequestInstance>(
71
71
  initialData,
72
72
  deepCompare,
73
73
  dependencyTracking,
74
+ disabled,
75
+ revalidate,
74
76
  });
75
77
 
76
78
  /**
@@ -94,11 +96,6 @@ export const useFetch = <RequestType extends RequestInstance>(
94
96
  if (!disabled) {
95
97
  logger.debug(`Fetching data`);
96
98
  dispatcher.add(request);
97
- const queue = dispatcher.getQueue(request.queueKey);
98
- // We want fast initial loading state if not paused / disabled
99
- if (!queue.stopped && queue.requests.length) {
100
- actions.setLoading(true);
101
- }
102
99
  } else {
103
100
  logger.debug(`Cannot add to fetch queue`, { disabled });
104
101
  }