@hyper-fetch/react 4.0.1 → 5.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/README.md +0 -9
- package/dist/index.cjs.js +1451 -1
- package/dist/index.cjs.js.map +3 -3
- package/dist/index.d.ts +19 -16
- package/dist/index.esm.js +1430 -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 +7 -7
- package/src/hooks/use-cache/use-cache.hooks.ts +5 -5
- package/src/hooks/use-cache/use-cache.types.ts +2 -2
- package/src/hooks/use-emitter/use-emitter.hooks.ts +2 -2
- package/src/hooks/use-event-messages/use-event-messages.hooks.ts +4 -4
- package/src/hooks/use-event-messages/use-event-messages.types.ts +1 -1
- package/src/hooks/use-fetch/use-fetch.constants.ts +5 -5
- package/src/hooks/use-fetch/use-fetch.hooks.ts +20 -20
- package/src/hooks/use-fetch/use-fetch.types.ts +7 -7
- package/src/hooks/use-listener/use-listener.hooks.ts +17 -10
- package/src/hooks/use-submit/use-submit.hooks.ts +8 -8
- package/src/hooks/use-submit/use-submit.types.ts +2 -2
|
@@ -12,7 +12,7 @@ import { useConfigProvider } from "config-provider";
|
|
|
12
12
|
* @param options
|
|
13
13
|
* @returns
|
|
14
14
|
*/
|
|
15
|
-
export const useEventMessages = <ResponsesType extends {
|
|
15
|
+
export const useEventMessages = <ResponsesType extends { endpoint: string }>(
|
|
16
16
|
socket: SocketInstance,
|
|
17
17
|
options: UseEventMessagesOptionsType<ResponsesType>,
|
|
18
18
|
) => {
|
|
@@ -24,11 +24,11 @@ export const useEventMessages = <ResponsesType extends { name: string }>(
|
|
|
24
24
|
|
|
25
25
|
useDidUpdate(
|
|
26
26
|
() => {
|
|
27
|
-
const unmountListener = socket.events.onListenerEvent<ResponsesType>((data,
|
|
28
|
-
const filterFn = typeof filter === "function" ? () => filter(
|
|
27
|
+
const unmountListener = socket.events.onListenerEvent<ResponsesType>(({ endpoint, data, extra }) => {
|
|
28
|
+
const filterFn = typeof filter === "function" ? () => filter(endpoint, data) : () => filter.includes(endpoint);
|
|
29
29
|
const isFiltered = filter ? filterFn() : false;
|
|
30
30
|
if (!isFiltered) {
|
|
31
|
-
onEventCallback.current?.(data,
|
|
31
|
+
onEventCallback.current?.(data, extra);
|
|
32
32
|
actions.setData(data);
|
|
33
33
|
actions.setTimestamp(+new Date());
|
|
34
34
|
}
|
|
@@ -10,14 +10,14 @@ export const useFetchDefaultOptions: DefaultOptionsType = {
|
|
|
10
10
|
dependencies: [],
|
|
11
11
|
disabled: false,
|
|
12
12
|
dependencyTracking: true,
|
|
13
|
-
|
|
13
|
+
fetchOnMount: true,
|
|
14
14
|
initialData: null,
|
|
15
15
|
refresh: false,
|
|
16
16
|
refreshTime: DateInterval.hour,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
refetchBlurred: true,
|
|
18
|
+
refetchOnBlur: false,
|
|
19
|
+
refetchOnFocus: false,
|
|
20
|
+
refetchOnReconnect: false,
|
|
21
21
|
bounce: false,
|
|
22
22
|
bounceType: "debounce",
|
|
23
23
|
bounceTime: 400,
|
|
@@ -25,14 +25,14 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
25
25
|
dependencies = useFetchDefaultOptions.dependencies,
|
|
26
26
|
disabled = useFetchDefaultOptions.disabled,
|
|
27
27
|
dependencyTracking = useFetchDefaultOptions.dependencyTracking,
|
|
28
|
-
|
|
28
|
+
fetchOnMount = useFetchDefaultOptions.fetchOnMount,
|
|
29
29
|
initialData = useFetchDefaultOptions.initialData,
|
|
30
30
|
refresh = useFetchDefaultOptions.refresh,
|
|
31
31
|
refreshTime = useFetchDefaultOptions.refreshTime,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
refetchBlurred = useFetchDefaultOptions.refetchBlurred,
|
|
33
|
+
refetchOnBlur = useFetchDefaultOptions.refetchOnBlur,
|
|
34
|
+
refetchOnFocus = useFetchDefaultOptions.refetchOnFocus,
|
|
35
|
+
refetchOnReconnect = useFetchDefaultOptions.refetchOnReconnect,
|
|
36
36
|
bounce = useFetchDefaultOptions.bounce,
|
|
37
37
|
bounceType = useFetchDefaultOptions.bounceType,
|
|
38
38
|
bounceTime = useFetchDefaultOptions.bounceTime,
|
|
@@ -107,7 +107,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
107
107
|
const isFetching = dispatcher.hasRunningRequests(request.queueKey);
|
|
108
108
|
const isQueued = dispatcher.getIsActiveQueue(request.queueKey);
|
|
109
109
|
const isActive = isFetching || isQueued;
|
|
110
|
-
const canRefreshBlurred = isBlurred &&
|
|
110
|
+
const canRefreshBlurred = isBlurred && refetchBlurred && !isActive;
|
|
111
111
|
const canRefreshFocused = !isBlurred && !isActive;
|
|
112
112
|
|
|
113
113
|
if (canRefreshBlurred || canRefreshFocused) {
|
|
@@ -120,19 +120,19 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
const
|
|
123
|
+
const handleInvalidation = (invalidateKey: InvalidationKeyType) => {
|
|
124
124
|
if (invalidateKey && invalidateKey instanceof Request) {
|
|
125
|
-
cache.
|
|
125
|
+
cache.invalidate(getRequestKey(invalidateKey));
|
|
126
126
|
} else if (invalidateKey && !(invalidateKey instanceof Request)) {
|
|
127
|
-
cache.
|
|
127
|
+
cache.invalidate(invalidateKey);
|
|
128
128
|
}
|
|
129
129
|
};
|
|
130
130
|
|
|
131
|
-
const
|
|
131
|
+
const refetch = (invalidateKey?: InvalidationKeyType | InvalidationKeyType[]) => {
|
|
132
132
|
if (invalidateKey && Array.isArray(invalidateKey)) {
|
|
133
|
-
invalidateKey.forEach(
|
|
133
|
+
invalidateKey.forEach(handleInvalidation);
|
|
134
134
|
} else if (invalidateKey && !Array.isArray(invalidateKey)) {
|
|
135
|
-
|
|
135
|
+
handleInvalidation(invalidateKey);
|
|
136
136
|
} else {
|
|
137
137
|
handleFetch();
|
|
138
138
|
handleRefresh();
|
|
@@ -146,7 +146,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
146
146
|
const initialFetchData = () => {
|
|
147
147
|
const hasStaleData = getStaleStatus();
|
|
148
148
|
const isFetching = dispatcher.getIsActiveQueue(queueKey);
|
|
149
|
-
if (
|
|
149
|
+
if (fetchOnMount || (hasStaleData && !isFetching)) {
|
|
150
150
|
handleFetch();
|
|
151
151
|
}
|
|
152
152
|
};
|
|
@@ -173,32 +173,32 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
173
173
|
addLifecycleListeners(request);
|
|
174
174
|
|
|
175
175
|
const focusUnmount = appManager.events.onFocus(() => {
|
|
176
|
-
if (
|
|
176
|
+
if (refetchOnFocus) {
|
|
177
177
|
handleFetch();
|
|
178
178
|
handleRefresh();
|
|
179
179
|
}
|
|
180
180
|
});
|
|
181
181
|
const blurUnmount = appManager.events.onBlur(() => {
|
|
182
|
-
if (
|
|
182
|
+
if (refetchOnBlur) {
|
|
183
183
|
handleFetch();
|
|
184
184
|
handleRefresh();
|
|
185
185
|
}
|
|
186
186
|
});
|
|
187
187
|
const onlineUnmount = appManager.events.onOnline(() => {
|
|
188
|
-
if (
|
|
188
|
+
if (refetchOnReconnect) {
|
|
189
189
|
handleFetch();
|
|
190
190
|
handleRefresh();
|
|
191
191
|
}
|
|
192
192
|
});
|
|
193
193
|
|
|
194
|
-
const
|
|
194
|
+
const invalidateUnmount = cache.events.onInvalidate(cacheKey, handleFetch);
|
|
195
195
|
|
|
196
196
|
const unmount = () => {
|
|
197
197
|
clearDataListener();
|
|
198
198
|
focusUnmount();
|
|
199
199
|
blurUnmount();
|
|
200
200
|
onlineUnmount();
|
|
201
|
-
|
|
201
|
+
invalidateUnmount();
|
|
202
202
|
};
|
|
203
203
|
|
|
204
204
|
return unmount;
|
|
@@ -215,7 +215,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
215
215
|
useDidUpdate(handleMountEvents, [updateKey], true);
|
|
216
216
|
|
|
217
217
|
/**
|
|
218
|
-
* Initial fetch triggered once data is stale or we use the
|
|
218
|
+
* Initial fetch triggered once data is stale or we use the refetch strategy
|
|
219
219
|
*/
|
|
220
220
|
useDidMount(initialFetchData);
|
|
221
221
|
|
|
@@ -265,6 +265,6 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
265
265
|
bounce: getBounceData(bounceData),
|
|
266
266
|
...actions,
|
|
267
267
|
...callbacks,
|
|
268
|
-
|
|
268
|
+
refetch,
|
|
269
269
|
};
|
|
270
270
|
};
|
|
@@ -20,7 +20,7 @@ export type UseFetchOptionsType<T extends RequestInstance> = {
|
|
|
20
20
|
/**
|
|
21
21
|
* If `true` it will refetch data in background no matter if we have it from cache.
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
fetchOnMount?: boolean;
|
|
24
24
|
/**
|
|
25
25
|
* If cache is empty we can use placeholder data.
|
|
26
26
|
*/
|
|
@@ -36,19 +36,19 @@ export type UseFetchOptionsType<T extends RequestInstance> = {
|
|
|
36
36
|
/**
|
|
37
37
|
* Enable/disable data refresh if our tab is not focused(used by user at given time).
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
refetchBlurred?: boolean;
|
|
40
40
|
/**
|
|
41
41
|
* Enable/disable data refresh if user leaves current tab.
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
refetchOnBlur?: boolean;
|
|
44
44
|
/**
|
|
45
45
|
* Enable/disable data refresh if user enters current tab.
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
refetchOnFocus?: boolean;
|
|
48
48
|
/**
|
|
49
49
|
* Enable/disable data refresh if network is restored.
|
|
50
50
|
*/
|
|
51
|
-
|
|
51
|
+
refetchOnReconnect?: boolean;
|
|
52
52
|
/**
|
|
53
53
|
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
|
|
54
54
|
*/
|
|
@@ -101,7 +101,7 @@ export type UseFetchReturnType<T extends RequestInstance> = UseTrackedStateType<
|
|
|
101
101
|
reset: () => void;
|
|
102
102
|
};
|
|
103
103
|
/**
|
|
104
|
-
*
|
|
104
|
+
* Refetch current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
105
105
|
*/
|
|
106
|
-
|
|
106
|
+
refetch: (invalidateKey?: InvalidationKeyType | InvalidationKeyType[]) => void;
|
|
107
107
|
};
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { useRef } from "react";
|
|
2
2
|
import { useDidUpdate, useWillUnmount } from "@better-hooks/lifecycle";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
ListenerInstance,
|
|
5
|
+
ExtractListenerAdapterType,
|
|
6
|
+
ExtractSocketExtraType,
|
|
7
|
+
ExtractListenerResponseType,
|
|
8
|
+
} from "@hyper-fetch/sockets";
|
|
4
9
|
|
|
5
10
|
import { useSocketState } from "helpers";
|
|
6
11
|
import { UseListenerOptionsType } from "hooks/use-listener";
|
|
@@ -15,24 +20,26 @@ export const useListener = <ListenerType extends ListenerInstance>(
|
|
|
15
20
|
|
|
16
21
|
const onEventCallback = useRef<
|
|
17
22
|
| null
|
|
18
|
-
| ((
|
|
19
|
-
data:
|
|
20
|
-
|
|
21
|
-
) => void)
|
|
23
|
+
| ((response: {
|
|
24
|
+
data: ExtractListenerResponseType<ListenerType>;
|
|
25
|
+
extra: ExtractSocketExtraType<ExtractListenerAdapterType<ListenerType>>;
|
|
26
|
+
}) => void)
|
|
22
27
|
>(null);
|
|
23
28
|
const removeListenerRef = useRef<ReturnType<typeof listener.listen> | null>(null);
|
|
24
29
|
const [state, actions, callbacks, { setRenderKey }] = useSocketState(listener.socket, { dependencyTracking });
|
|
25
30
|
|
|
26
31
|
const stopListener = () => {
|
|
27
|
-
removeListenerRef.current?.
|
|
32
|
+
removeListenerRef.current?.();
|
|
28
33
|
};
|
|
29
34
|
|
|
30
35
|
const listen = () => {
|
|
31
36
|
stopListener();
|
|
32
|
-
removeListenerRef.current = listener.listen(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
removeListenerRef.current = listener.listen({
|
|
38
|
+
callback: ({ data, extra }) => {
|
|
39
|
+
onEventCallback.current?.({ data, extra });
|
|
40
|
+
actions.setData(data);
|
|
41
|
+
actions.setTimestamp(+new Date());
|
|
42
|
+
},
|
|
36
43
|
});
|
|
37
44
|
};
|
|
38
45
|
|
|
@@ -171,24 +171,24 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
171
171
|
};
|
|
172
172
|
|
|
173
173
|
// ******************
|
|
174
|
-
//
|
|
174
|
+
// Invalidation
|
|
175
175
|
// ******************
|
|
176
176
|
|
|
177
|
-
const
|
|
177
|
+
const handleInvalidation = (invalidateKey: InvalidationKeyType) => {
|
|
178
178
|
if (invalidateKey && invalidateKey instanceof Request) {
|
|
179
|
-
cache.
|
|
179
|
+
cache.invalidate(getRequestKey(invalidateKey));
|
|
180
180
|
} else if (invalidateKey && !(invalidateKey instanceof Request)) {
|
|
181
|
-
cache.
|
|
181
|
+
cache.invalidate(invalidateKey);
|
|
182
182
|
}
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
-
const
|
|
185
|
+
const refetch = (invalidateKey: InvalidationKeyType | InvalidationKeyType[]) => {
|
|
186
186
|
if (!invalidateKey) return;
|
|
187
187
|
|
|
188
188
|
if (invalidateKey && Array.isArray(invalidateKey)) {
|
|
189
|
-
invalidateKey.forEach(
|
|
189
|
+
invalidateKey.forEach(handleInvalidation);
|
|
190
190
|
} else if (invalidateKey && !Array.isArray(invalidateKey)) {
|
|
191
|
-
|
|
191
|
+
handleInvalidation(invalidateKey);
|
|
192
192
|
}
|
|
193
193
|
};
|
|
194
194
|
|
|
@@ -254,6 +254,6 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
254
254
|
...actions,
|
|
255
255
|
...handlers,
|
|
256
256
|
bounce: getBounceData(bounceData),
|
|
257
|
-
|
|
257
|
+
refetch,
|
|
258
258
|
};
|
|
259
259
|
};
|
|
@@ -133,7 +133,7 @@ export type UseSubmitReturnType<RequestType extends RequestInstance> = Omit<
|
|
|
133
133
|
reset: () => void;
|
|
134
134
|
};
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
136
|
+
* Refetch current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
refetch: (invalidateKey: InvalidationKeyType | InvalidationKeyType[]) => void;
|
|
139
139
|
};
|