@hyper-fetch/react 3.3.4 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +3 -3
- package/dist/index.d.ts +60 -23
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +3 -3
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +25 -20
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +3 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +78 -28
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +24 -5
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +35 -16
- package/src/hooks/use-cache/use-cache.hooks.ts +8 -0
- package/src/hooks/use-emitter/use-emitter.hooks.ts +0 -2
- package/src/hooks/use-fetch/use-fetch.constants.ts +0 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +12 -4
- package/src/hooks/use-fetch/use-fetch.types.ts +26 -13
- package/src/hooks/use-queue/use-queue.hooks.ts +2 -2
- package/src/hooks/use-queue/use-queue.types.ts +2 -2
- package/src/hooks/use-submit/use-submit.constants.ts +0 -1
- package/src/hooks/use-submit/use-submit.hooks.ts +42 -11
- package/src/hooks/use-submit/use-submit.types.ts +26 -13
- package/tsconfig.json +1 -1
|
@@ -3,13 +3,14 @@ import {
|
|
|
3
3
|
Request,
|
|
4
4
|
getRequestKey,
|
|
5
5
|
ExtractAdapterReturnType,
|
|
6
|
-
requestSendRequest,
|
|
7
6
|
RequestInstance,
|
|
8
|
-
|
|
7
|
+
sendRequest,
|
|
8
|
+
ResponseReturnType,
|
|
9
9
|
ExtractResponseType,
|
|
10
10
|
ExtractErrorType,
|
|
11
11
|
RequestSendOptionsType,
|
|
12
12
|
RequestSendType,
|
|
13
|
+
ExtractAdapterType,
|
|
13
14
|
} from "@hyper-fetch/core";
|
|
14
15
|
import { useDidMount } from "@better-hooks/lifecycle";
|
|
15
16
|
import { useDebounce, useThrottle } from "@better-hooks/performance";
|
|
@@ -32,16 +33,16 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
32
33
|
): UseSubmitReturnType<RequestType> => {
|
|
33
34
|
// Build the configuration options
|
|
34
35
|
const [globalConfig] = useConfigProvider();
|
|
35
|
-
const mergedOptions = useMemo(
|
|
36
|
+
const mergedOptions: UseSubmitOptionsType<RequestType> = useMemo(
|
|
36
37
|
() => ({
|
|
37
38
|
...useSubmitDefaultOptions,
|
|
38
39
|
...globalConfig.useSubmitConfig,
|
|
39
40
|
...options,
|
|
40
41
|
}),
|
|
42
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
41
43
|
[JSON.stringify(globalConfig.useSubmitConfig), JSON.stringify(options)],
|
|
42
44
|
);
|
|
43
|
-
const { disabled, dependencyTracking, initialData, bounce, bounceType, bounceTime,
|
|
44
|
-
mergedOptions;
|
|
45
|
+
const { disabled, dependencyTracking, initialData, bounce, bounceType, bounceTime, deepCompare } = mergedOptions;
|
|
45
46
|
|
|
46
47
|
/**
|
|
47
48
|
* Because of the dynamic cacheKey / queueKey signing within the request we need to store it's latest instance
|
|
@@ -52,9 +53,18 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
52
53
|
|
|
53
54
|
const logger = useRef(loggerManager.init("useSubmit")).current;
|
|
54
55
|
const requestDebounce = useDebounce({ delay: bounceTime });
|
|
55
|
-
const requestThrottle = useThrottle({
|
|
56
|
+
const requestThrottle = useThrottle({
|
|
57
|
+
interval: bounceTime,
|
|
58
|
+
timeout: "bounceTimeout" in mergedOptions ? mergedOptions.bounceTimeout : bounceTime,
|
|
59
|
+
});
|
|
56
60
|
const bounceResolver = useRef<
|
|
57
|
-
(
|
|
61
|
+
(
|
|
62
|
+
value: ResponseReturnType<
|
|
63
|
+
ExtractResponseType<RequestType>,
|
|
64
|
+
ExtractErrorType<RequestType>,
|
|
65
|
+
ExtractAdapterType<RequestType>
|
|
66
|
+
>,
|
|
67
|
+
) => void
|
|
58
68
|
>(() => null);
|
|
59
69
|
|
|
60
70
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
@@ -94,14 +104,23 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
94
104
|
|
|
95
105
|
if (disabled) {
|
|
96
106
|
logger.warning(`Cannot submit request`, { disabled, submitOptions });
|
|
97
|
-
return Promise.resolve(
|
|
98
|
-
|
|
107
|
+
return Promise.resolve({
|
|
108
|
+
data: null,
|
|
109
|
+
error: new Error("Cannot submit request. Option 'disabled' is enabled"),
|
|
110
|
+
status: null,
|
|
111
|
+
extra: request.client.defaultExtra,
|
|
112
|
+
}) as Promise<
|
|
113
|
+
ResponseReturnType<
|
|
114
|
+
ExtractResponseType<RequestType>,
|
|
115
|
+
ExtractErrorType<RequestType>,
|
|
116
|
+
ExtractAdapterType<RequestType>
|
|
117
|
+
>
|
|
99
118
|
>;
|
|
100
119
|
}
|
|
101
120
|
|
|
102
121
|
const triggerRequest = () => {
|
|
103
122
|
addDataListener(requestClone);
|
|
104
|
-
return
|
|
123
|
+
return sendRequest(requestClone, {
|
|
105
124
|
dispatcherType: "submit",
|
|
106
125
|
...submitOptions,
|
|
107
126
|
onSettle: (requestId, cmd) => {
|
|
@@ -120,7 +139,11 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
120
139
|
// By default bounce method will prevent function to be triggered, but returned promise will still await to be resolved.
|
|
121
140
|
// This way we can close previous promise, making sure our logic will not stuck in memory.
|
|
122
141
|
bounceResolver.current = (
|
|
123
|
-
value:
|
|
142
|
+
value: ResponseReturnType<
|
|
143
|
+
ExtractResponseType<RequestType>,
|
|
144
|
+
ExtractErrorType<RequestType>,
|
|
145
|
+
ExtractAdapterType<RequestType>
|
|
146
|
+
>,
|
|
124
147
|
) => {
|
|
125
148
|
// Trigger previous awaiting calls to resolve together in bounced batches
|
|
126
149
|
bouncedResolve(value);
|
|
@@ -211,6 +234,14 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
211
234
|
setRenderKey("status");
|
|
212
235
|
return state.status;
|
|
213
236
|
},
|
|
237
|
+
get success() {
|
|
238
|
+
setRenderKey("success");
|
|
239
|
+
return state.success;
|
|
240
|
+
},
|
|
241
|
+
get extra() {
|
|
242
|
+
setRenderKey("extra");
|
|
243
|
+
return state.extra;
|
|
244
|
+
},
|
|
214
245
|
get retries() {
|
|
215
246
|
setRenderKey("retries");
|
|
216
247
|
return state.retries;
|
|
@@ -31,18 +31,6 @@ export type UseSubmitOptionsType<T extends RequestInstance> = {
|
|
|
31
31
|
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
|
|
32
32
|
*/
|
|
33
33
|
bounce?: boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Possibility to choose between debounce and throttle approaches
|
|
36
|
-
*/
|
|
37
|
-
bounceType?: "debounce" | "throttle";
|
|
38
|
-
/**
|
|
39
|
-
* How long it should bounce requests.
|
|
40
|
-
*/
|
|
41
|
-
bounceTime?: number;
|
|
42
|
-
/**
|
|
43
|
-
* ONLY in throttle mode - options for handling last bounce event
|
|
44
|
-
*/
|
|
45
|
-
bounceTimeout?: number;
|
|
46
34
|
/**
|
|
47
35
|
* If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
|
|
48
36
|
*/
|
|
@@ -51,7 +39,32 @@ export type UseSubmitOptionsType<T extends RequestInstance> = {
|
|
|
51
39
|
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
52
40
|
*/
|
|
53
41
|
deepCompare?: boolean | typeof isEqual;
|
|
54
|
-
}
|
|
42
|
+
} & (
|
|
43
|
+
| {
|
|
44
|
+
/**
|
|
45
|
+
* Possibility to choose between debounce and throttle approaches
|
|
46
|
+
*/
|
|
47
|
+
bounceType?: "debounce";
|
|
48
|
+
/**
|
|
49
|
+
* How long it should bounce requests.
|
|
50
|
+
*/
|
|
51
|
+
bounceTime?: number;
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
/**
|
|
55
|
+
* Possibility to choose between debounce and throttle approaches
|
|
56
|
+
*/
|
|
57
|
+
bounceType: "throttle";
|
|
58
|
+
/**
|
|
59
|
+
* How long it should interval requests.
|
|
60
|
+
*/
|
|
61
|
+
bounceTime?: number;
|
|
62
|
+
/**
|
|
63
|
+
* ONLY in throttle mode - options for handling last bounce event
|
|
64
|
+
*/
|
|
65
|
+
bounceTimeout?: number;
|
|
66
|
+
}
|
|
67
|
+
);
|
|
55
68
|
|
|
56
69
|
export type UseSubmitReturnType<RequestType extends RequestInstance> = Omit<
|
|
57
70
|
UseTrackedStateType<RequestType>,
|