@hyper-fetch/react 9.5.0 → 9.5.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.
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +9 -3
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +9 -3
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +625 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +12 -4
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/index.mjs +4 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +9 -27
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/react",
|
|
3
|
-
"version": "9.5.
|
|
3
|
+
"version": "9.5.1",
|
|
4
4
|
"description": "React hooks and utils for the hyper-fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -84,6 +84,7 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
84
84
|
// Optimistic state
|
|
85
85
|
// ******************
|
|
86
86
|
|
|
87
|
+
// Rollback and invalidation are settled by the core - here we only deliver the context to the callbacks
|
|
87
88
|
const optimisticResultsRef = useRef<Map<string, OptimisticCallbackResult<any>>>(new Map());
|
|
88
89
|
|
|
89
90
|
// ******************
|
|
@@ -95,37 +96,24 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
95
96
|
const { isOffline, isCanceled, willRetry } = values.details;
|
|
96
97
|
const opt = optimisticResultsRef.current.get(values.requestId);
|
|
97
98
|
const paramsWithContext = { ...values, mutationContext: opt?.context } as any;
|
|
99
|
+
const isOfflineError = request.offline && isOffline && !success;
|
|
98
100
|
|
|
99
|
-
if (
|
|
101
|
+
if (isOfflineError) {
|
|
100
102
|
logger.debug({ title: "Performing offline error callback", type: "system", extra: values });
|
|
101
103
|
onOfflineErrorCallback.current?.(paramsWithContext);
|
|
102
104
|
} else if (isCanceled) {
|
|
103
105
|
logger.debug({ title: "Performing abort callback", type: "system", extra: values });
|
|
104
|
-
try {
|
|
105
|
-
opt?.rollback?.();
|
|
106
|
-
} catch {
|
|
107
|
-
// rollback threw — onAbort still fires
|
|
108
|
-
}
|
|
109
106
|
onAbortCallback.current?.(paramsWithContext);
|
|
110
107
|
} else if (success) {
|
|
111
108
|
logger.debug({ title: "Performing success callback", type: "system", extra: values });
|
|
112
|
-
if (opt?.invalidate) {
|
|
113
|
-
opt.invalidate.forEach((req) => cache.invalidate(req));
|
|
114
|
-
}
|
|
115
109
|
onSuccessCallback.current?.(paramsWithContext);
|
|
116
110
|
} else {
|
|
117
111
|
logger.debug({ title: "Performing error callback", type: "system", extra: values });
|
|
118
|
-
if (!willRetry) {
|
|
119
|
-
try {
|
|
120
|
-
opt?.rollback?.();
|
|
121
|
-
} catch {
|
|
122
|
-
// rollback threw — onError still fires
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
112
|
onErrorCallback.current?.(paramsWithContext);
|
|
126
113
|
}
|
|
127
114
|
onFinishedCallback.current?.(paramsWithContext);
|
|
128
|
-
|
|
115
|
+
// Retried and offline requests respond again, their callbacks still need the context
|
|
116
|
+
if (!willRetry && (!isOfflineError || isCanceled)) {
|
|
129
117
|
optimisticResultsRef.current.delete(values.requestId);
|
|
130
118
|
}
|
|
131
119
|
};
|
|
@@ -145,7 +133,9 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
145
133
|
|
|
146
134
|
// When we process the cache data, we don't want to change the loading state during it
|
|
147
135
|
// This prevents the UI from flickering with { data: null, loading: false }
|
|
148
|
-
if (isProcessing) {
|
|
136
|
+
if (isProcessing) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
149
139
|
|
|
150
140
|
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(scopeKey(req.queryKey, req.scope));
|
|
151
141
|
if (loading || canDisableLoading) {
|
|
@@ -192,15 +182,7 @@ export const useRequestEvents = <R extends RequestInstance>({
|
|
|
192
182
|
};
|
|
193
183
|
|
|
194
184
|
const handleRemove = ({ requestId }: RequestEventType<R>) => {
|
|
195
|
-
|
|
196
|
-
if (opt) {
|
|
197
|
-
try {
|
|
198
|
-
opt.rollback?.();
|
|
199
|
-
} catch {
|
|
200
|
-
// swallow — queue removal rollback failure
|
|
201
|
-
}
|
|
202
|
-
optimisticResultsRef.current.delete(requestId);
|
|
203
|
-
}
|
|
185
|
+
optimisticResultsRef.current.delete(requestId);
|
|
204
186
|
removeLifecycleListener(requestId);
|
|
205
187
|
};
|
|
206
188
|
|