@hyper-fetch/react 2.1.0 → 3.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.
- package/README.md +1 -1
- package/dist/config-provider/config-provider.types.d.ts +11 -5
- package/dist/helpers/index.d.ts +1 -1
- package/dist/helpers/use-request-events/index.d.ts +2 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +8 -0
- package/dist/helpers/{use-command-events/use-command-events.types.d.ts → use-request-events/use-request-events.types.d.ts} +24 -24
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +2 -2
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +12 -12
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +4 -4
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +1 -1
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +1 -1
- package/dist/hooks/use-cache/use-cache.types.d.ts +6 -6
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +1 -1
- package/dist/hooks/use-event-messages/use-event-messages.hooks.d.ts +1 -1
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts +2 -2
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +2 -2
- package/dist/hooks/use-fetch/use-fetch.types.d.ts +10 -6
- package/dist/hooks/use-listener/use-listener.hooks.d.ts +3 -3
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +2 -2
- package/dist/hooks/use-queue/use-queue.types.d.ts +7 -7
- package/dist/hooks/use-submit/use-submit.constants.d.ts +2 -2
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +2 -2
- package/dist/hooks/use-submit/use-submit.types.d.ts +20 -16
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/types/invalidation.types.d.ts +2 -2
- package/package.json +3 -3
- package/src/config-provider/config-provider.types.ts +11 -5
- package/src/helpers/index.ts +1 -1
- package/src/helpers/use-request-events/index.ts +2 -0
- package/src/helpers/{use-command-events/use-command-events.hooks.ts → use-request-events/use-request-events.hooks.ts} +56 -53
- package/src/helpers/{use-command-events/use-command-events.types.ts → use-request-events/use-request-events.types.ts} +35 -35
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +16 -16
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +15 -15
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +22 -22
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +11 -11
- package/src/hooks/use-cache/use-cache.hooks.ts +13 -13
- package/src/hooks/use-cache/use-cache.types.ts +6 -6
- package/src/hooks/use-emitter/use-emitter.hooks.ts +5 -1
- package/src/hooks/use-event-messages/use-event-messages.hooks.ts +6 -2
- package/src/hooks/use-fetch/use-fetch.constants.ts +3 -2
- package/src/hooks/use-fetch/use-fetch.hooks.ts +26 -25
- package/src/hooks/use-fetch/use-fetch.types.ts +11 -7
- package/src/hooks/use-listener/use-listener.hooks.ts +9 -5
- package/src/hooks/use-queue/use-queue.hooks.ts +23 -23
- package/src/hooks/use-queue/use-queue.types.ts +7 -7
- package/src/hooks/use-submit/use-submit.constants.ts +3 -2
- package/src/hooks/use-submit/use-submit.hooks.ts +42 -41
- package/src/hooks/use-submit/use-submit.types.ts +28 -18
- package/src/types/invalidation.types.ts +2 -2
- package/dist/helpers/use-command-events/index.d.ts +0 -2
- package/dist/helpers/use-command-events/use-command-events.hooks.d.ts +0 -8
- package/src/helpers/use-command-events/index.ts +0 -2
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { useState } from "react";
|
|
2
|
-
import { useDidMount } from "@better-
|
|
3
|
-
import {
|
|
2
|
+
import { useDidMount } from "@better-hooks/lifecycle";
|
|
3
|
+
import { ClientInstance } from "@hyper-fetch/core";
|
|
4
4
|
|
|
5
5
|
import { UseAppManagerReturnType } from "hooks/use-app-manager";
|
|
6
6
|
|
|
7
|
-
export const useAppManager = <B extends
|
|
8
|
-
const [online, setIsOnline] = useState(
|
|
9
|
-
const [focused, setIsFocused] = useState(
|
|
7
|
+
export const useAppManager = <B extends ClientInstance>(client: B): UseAppManagerReturnType => {
|
|
8
|
+
const [online, setIsOnline] = useState(client.appManager.isOnline);
|
|
9
|
+
const [focused, setIsFocused] = useState(client.appManager.isFocused);
|
|
10
10
|
|
|
11
11
|
const mountEvents = () => {
|
|
12
|
-
const unmountIsOnline =
|
|
13
|
-
const unmountIsOffline =
|
|
14
|
-
const unmountIsFocus =
|
|
15
|
-
const unmountIsBlur =
|
|
12
|
+
const unmountIsOnline = client.appManager.events.onOnline(() => setIsOnline(true));
|
|
13
|
+
const unmountIsOffline = client.appManager.events.onOffline(() => setIsOnline(false));
|
|
14
|
+
const unmountIsFocus = client.appManager.events.onFocus(() => setIsFocused(true));
|
|
15
|
+
const unmountIsBlur = client.appManager.events.onBlur(() => setIsFocused(false));
|
|
16
16
|
|
|
17
17
|
return () => {
|
|
18
18
|
unmountIsOnline();
|
|
@@ -23,11 +23,11 @@ export const useAppManager = <B extends BuilderInstance>(builder: B): UseAppMana
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
const setOnline = (isOnline: boolean) => {
|
|
26
|
-
|
|
26
|
+
client.appManager.setOnline(isOnline);
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
const setFocused = (isFocused: boolean) => {
|
|
30
|
-
|
|
30
|
+
client.appManager.setFocused(isFocused);
|
|
31
31
|
};
|
|
32
32
|
|
|
33
33
|
useDidMount(mountEvents);
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { useRef } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { getRequestDispatcher, RequestInstance, Request, getRequestKey } from "@hyper-fetch/core";
|
|
3
3
|
|
|
4
4
|
import { UseCacheOptionsType, useCacheDefaultOptions, UseCacheReturnType } from "hooks/use-cache";
|
|
5
|
-
import {
|
|
5
|
+
import { useRequestEvents, useTrackedState } from "helpers";
|
|
6
6
|
import { useConfigProvider } from "config-provider";
|
|
7
7
|
|
|
8
|
-
export const useCache = <T extends
|
|
9
|
-
|
|
8
|
+
export const useCache = <T extends RequestInstance>(
|
|
9
|
+
request: T,
|
|
10
10
|
options: UseCacheOptionsType<T> = useCacheDefaultOptions,
|
|
11
11
|
): UseCacheReturnType<T> => {
|
|
12
|
-
const { cacheKey,
|
|
12
|
+
const { cacheKey, client } = request;
|
|
13
13
|
|
|
14
|
-
const { cache, loggerManager } =
|
|
14
|
+
const { cache, loggerManager } = client;
|
|
15
15
|
const logger = useRef(loggerManager.init("useCache")).current;
|
|
16
|
-
const [dispatcher] =
|
|
16
|
+
const [dispatcher] = getRequestDispatcher(request);
|
|
17
17
|
|
|
18
18
|
// Build the configuration options
|
|
19
19
|
const [globalConfig] = useConfigProvider();
|
|
@@ -28,7 +28,7 @@ export const useCache = <T extends CommandInstance>(
|
|
|
28
28
|
*/
|
|
29
29
|
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState<T>({
|
|
30
30
|
logger,
|
|
31
|
-
|
|
31
|
+
request,
|
|
32
32
|
dispatcher,
|
|
33
33
|
initialData,
|
|
34
34
|
deepCompare,
|
|
@@ -38,17 +38,17 @@ export const useCache = <T extends CommandInstance>(
|
|
|
38
38
|
/**
|
|
39
39
|
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
40
40
|
*/
|
|
41
|
-
const [callbacks] =
|
|
41
|
+
const [callbacks] = useRequestEvents({
|
|
42
42
|
logger,
|
|
43
43
|
actions,
|
|
44
|
-
|
|
44
|
+
request,
|
|
45
45
|
dispatcher,
|
|
46
46
|
setCacheData,
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
const revalidate = (invalidateKey?: string |
|
|
50
|
-
if (invalidateKey instanceof
|
|
51
|
-
cache.revalidate(
|
|
49
|
+
const revalidate = (invalidateKey?: string | RequestInstance | RegExp) => {
|
|
50
|
+
if (invalidateKey instanceof Request) {
|
|
51
|
+
cache.revalidate(getRequestKey(invalidateKey, true));
|
|
52
52
|
} else if (invalidateKey) {
|
|
53
53
|
cache.revalidate(invalidateKey);
|
|
54
54
|
} else {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestInstance, ExtractResponseType, ExtractErrorType, CacheValueType } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { isEqual } from "utils";
|
|
4
4
|
import {
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
UseTrackedStateType,
|
|
10
10
|
} from "helpers";
|
|
11
11
|
|
|
12
|
-
export type UseCacheOptionsType<T extends
|
|
12
|
+
export type UseCacheOptionsType<T extends RequestInstance> = {
|
|
13
13
|
/**
|
|
14
14
|
* If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
|
|
15
15
|
*/
|
|
@@ -17,14 +17,14 @@ export type UseCacheOptionsType<T extends CommandInstance> = {
|
|
|
17
17
|
/**
|
|
18
18
|
* If cache is empty we can use placeholder data.
|
|
19
19
|
*/
|
|
20
|
-
initialData?: CacheValueType<
|
|
20
|
+
initialData?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
|
|
21
21
|
/**
|
|
22
22
|
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
23
23
|
*/
|
|
24
24
|
deepCompare?: boolean | typeof isEqual;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
export type UseCacheReturnType<T extends
|
|
27
|
+
export type UseCacheReturnType<T extends RequestInstance> = UseTrackedStateType<T> &
|
|
28
28
|
UseTrackedStateActions<T> & {
|
|
29
29
|
/**
|
|
30
30
|
* Helper hook listener for success response
|
|
@@ -39,7 +39,7 @@ export type UseCacheReturnType<T extends CommandInstance> = UseTrackedStateType<
|
|
|
39
39
|
*/
|
|
40
40
|
onCacheChange: (callback: OnFinishedCallbackType<T>) => void;
|
|
41
41
|
/**
|
|
42
|
-
* Revalidate current
|
|
42
|
+
* Revalidate current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
43
43
|
*/
|
|
44
|
-
revalidate: (invalidateKey?: string | RegExp |
|
|
44
|
+
revalidate: (invalidateKey?: string | RegExp | RequestInstance) => void;
|
|
45
45
|
};
|
|
@@ -3,13 +3,17 @@ import { EmitterInstance } from "@hyper-fetch/sockets";
|
|
|
3
3
|
|
|
4
4
|
import { UseEmitterOptionsType } from "hooks/use-emitter";
|
|
5
5
|
import { useSocketState } from "helpers";
|
|
6
|
+
import { useConfigProvider } from "config-provider";
|
|
6
7
|
|
|
7
8
|
// Todo bounce
|
|
8
9
|
|
|
9
10
|
export const useEmitter = <EmitterType extends EmitterInstance>(
|
|
10
11
|
emitter: EmitterType,
|
|
11
|
-
|
|
12
|
+
options: UseEmitterOptionsType,
|
|
12
13
|
) => {
|
|
14
|
+
const [globalConfig] = useConfigProvider();
|
|
15
|
+
const { dependencyTracking } = { ...globalConfig.useEmitter, ...options };
|
|
16
|
+
|
|
13
17
|
const onEventCallback = useRef<null | ((emitter: EmitterType) => void)>(null);
|
|
14
18
|
const [state, actions, callbacks, { setRenderKey }] = useSocketState(emitter.socket, { dependencyTracking });
|
|
15
19
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useRef } from "react";
|
|
2
|
-
import { useDidUpdate } from "@better-
|
|
2
|
+
import { useDidUpdate } from "@better-hooks/lifecycle";
|
|
3
3
|
import { SocketInstance } from "@hyper-fetch/sockets";
|
|
4
4
|
|
|
5
5
|
import { useSocketState } from "helpers";
|
|
6
6
|
import { UseEventMessagesOptionsType } from "hooks/use-event-messages";
|
|
7
|
+
import { useConfigProvider } from "config-provider";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Allow to listen to all event messages received with sockets
|
|
@@ -13,8 +14,11 @@ import { UseEventMessagesOptionsType } from "hooks/use-event-messages";
|
|
|
13
14
|
*/
|
|
14
15
|
export const useEventMessages = <ResponsesType>(
|
|
15
16
|
socket: SocketInstance,
|
|
16
|
-
|
|
17
|
+
options: UseEventMessagesOptionsType<ResponsesType>,
|
|
17
18
|
) => {
|
|
19
|
+
const [globalConfig] = useConfigProvider();
|
|
20
|
+
const { dependencyTracking = false, filter } = { ...globalConfig.useEventMessages, ...options };
|
|
21
|
+
|
|
18
22
|
const onEventCallback = useRef<null | ((data: ResponsesType, event: MessageEvent<ResponsesType>) => void)>(null);
|
|
19
23
|
const [state, actions, callbacks, { setRenderKey }] = useSocketState(socket, { dependencyTracking });
|
|
20
24
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { DateInterval,
|
|
1
|
+
import { DateInterval, RequestInstance, RequiredKeys } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { UseFetchOptionsType } from "hooks/use-fetch";
|
|
4
4
|
|
|
5
|
-
type DefaultOptionsType = RequiredKeys<Omit<UseFetchOptionsType<
|
|
5
|
+
type DefaultOptionsType = RequiredKeys<Omit<UseFetchOptionsType<RequestInstance>, "initialData">> & {
|
|
6
6
|
initialData: null;
|
|
7
7
|
};
|
|
8
8
|
|
|
@@ -21,5 +21,6 @@ export const useFetchDefaultOptions: DefaultOptionsType = {
|
|
|
21
21
|
bounce: false,
|
|
22
22
|
bounceType: "debounce",
|
|
23
23
|
bounceTime: 400,
|
|
24
|
+
bounceTimeout: 200,
|
|
24
25
|
deepCompare: true,
|
|
25
26
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useRef } from "react";
|
|
2
|
-
import { useDidUpdate, useDidMount } from "@better-
|
|
3
|
-
import { useDebounce, useThrottle } from "@better-
|
|
4
|
-
import {
|
|
2
|
+
import { useDidUpdate, useDidMount } from "@better-hooks/lifecycle";
|
|
3
|
+
import { useDebounce, useThrottle } from "@better-hooks/performance";
|
|
4
|
+
import { RequestInstance, Request, getRequestKey } from "@hyper-fetch/core";
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { useRequestEvents, useTrackedState } from "helpers";
|
|
7
7
|
import { UseFetchOptionsType, useFetchDefaultOptions, UseFetchReturnType, getRefreshTime } from "hooks/use-fetch";
|
|
8
8
|
import { useConfigProvider } from "config-provider";
|
|
9
9
|
import { getBounceData } from "utils";
|
|
@@ -11,12 +11,12 @@ import { InvalidationKeyType } from "types";
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* This hooks aims to retrieve data from server.
|
|
14
|
-
* @param
|
|
14
|
+
* @param request Request instance
|
|
15
15
|
* @param options Hook options
|
|
16
16
|
* @returns
|
|
17
17
|
*/
|
|
18
|
-
export const useFetch = <T extends
|
|
19
|
-
|
|
18
|
+
export const useFetch = <T extends RequestInstance>(
|
|
19
|
+
request: T,
|
|
20
20
|
options: UseFetchOptionsType<T> = useFetchDefaultOptions,
|
|
21
21
|
): UseFetchReturnType<T> => {
|
|
22
22
|
// Build the configuration options
|
|
@@ -36,6 +36,7 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
36
36
|
bounce = useFetchDefaultOptions.bounce,
|
|
37
37
|
bounceType = useFetchDefaultOptions.bounceType,
|
|
38
38
|
bounceTime = useFetchDefaultOptions.bounceTime,
|
|
39
|
+
bounceTimeout = useFetchDefaultOptions.bounceTimeout,
|
|
39
40
|
deepCompare = useFetchDefaultOptions.deepCompare,
|
|
40
41
|
} = {
|
|
41
42
|
...useFetchDefaultOptions,
|
|
@@ -43,13 +44,13 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
43
44
|
...options,
|
|
44
45
|
};
|
|
45
46
|
|
|
46
|
-
const updateKey = JSON.stringify(
|
|
47
|
-
const requestDebounce = useDebounce(bounceTime);
|
|
48
|
-
const requestThrottle = useThrottle(bounceTime);
|
|
49
|
-
const refreshDebounce = useDebounce(refreshTime);
|
|
47
|
+
const updateKey = JSON.stringify(request.dump());
|
|
48
|
+
const requestDebounce = useDebounce({ delay: bounceTime });
|
|
49
|
+
const requestThrottle = useThrottle({ interval: bounceTime, timeout: bounceTimeout });
|
|
50
|
+
const refreshDebounce = useDebounce({ delay: refreshTime });
|
|
50
51
|
|
|
51
|
-
const { cacheKey, queueKey,
|
|
52
|
-
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } =
|
|
52
|
+
const { cacheKey, queueKey, client } = request;
|
|
53
|
+
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
53
54
|
|
|
54
55
|
const logger = useRef(loggerManager.init("useFetch")).current;
|
|
55
56
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
@@ -60,7 +61,7 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
60
61
|
*/
|
|
61
62
|
const [state, actions, { setRenderKey, setCacheData, getStaleStatus }] = useTrackedState<T>({
|
|
62
63
|
logger,
|
|
63
|
-
|
|
64
|
+
request,
|
|
64
65
|
dispatcher,
|
|
65
66
|
initialData,
|
|
66
67
|
deepCompare,
|
|
@@ -70,10 +71,10 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
70
71
|
/**
|
|
71
72
|
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
72
73
|
*/
|
|
73
|
-
const [callbacks, listeners] =
|
|
74
|
+
const [callbacks, listeners] = useRequestEvents({
|
|
74
75
|
logger,
|
|
75
76
|
actions,
|
|
76
|
-
|
|
77
|
+
request,
|
|
77
78
|
dispatcher,
|
|
78
79
|
setCacheData,
|
|
79
80
|
});
|
|
@@ -86,7 +87,7 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
86
87
|
const handleFetch = () => {
|
|
87
88
|
if (!disabled) {
|
|
88
89
|
logger.debug(`Fetching data`);
|
|
89
|
-
dispatcher.add(
|
|
90
|
+
dispatcher.add(request);
|
|
90
91
|
} else {
|
|
91
92
|
logger.debug(`Cannot add to fetch queue`, { disabled });
|
|
92
93
|
}
|
|
@@ -106,8 +107,8 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
106
107
|
const isBlurred = !appManager.isFocused;
|
|
107
108
|
|
|
108
109
|
// If window tab is not active should we refresh the cache
|
|
109
|
-
const isFetching = dispatcher.hasRunningRequests(
|
|
110
|
-
const isQueued = dispatcher.getIsActiveQueue(
|
|
110
|
+
const isFetching = dispatcher.hasRunningRequests(request.queueKey);
|
|
111
|
+
const isQueued = dispatcher.getIsActiveQueue(request.queueKey);
|
|
111
112
|
const isActive = isFetching || isQueued;
|
|
112
113
|
const canRefreshBlurred = isBlurred && refreshBlurred && !isActive;
|
|
113
114
|
const canRefreshFocused = !isBlurred && !isActive;
|
|
@@ -123,9 +124,9 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
123
124
|
}
|
|
124
125
|
|
|
125
126
|
const handleRevalidation = (invalidateKey: InvalidationKeyType) => {
|
|
126
|
-
if (invalidateKey && invalidateKey instanceof
|
|
127
|
-
cache.revalidate(
|
|
128
|
-
} else if (invalidateKey && !(invalidateKey instanceof
|
|
127
|
+
if (invalidateKey && invalidateKey instanceof Request) {
|
|
128
|
+
cache.revalidate(getRequestKey(invalidateKey));
|
|
129
|
+
} else if (invalidateKey && !(invalidateKey instanceof Request)) {
|
|
129
130
|
cache.revalidate(invalidateKey);
|
|
130
131
|
}
|
|
131
132
|
};
|
|
@@ -159,7 +160,7 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
159
160
|
* This way it will not wait for debouncing but fetch data right away
|
|
160
161
|
*/
|
|
161
162
|
if (bounce) {
|
|
162
|
-
logger.debug(`Bounce request with ${bounceType}`, { queueKey,
|
|
163
|
+
logger.debug(`Bounce request with ${bounceType}`, { queueKey, request });
|
|
163
164
|
bounceFunction(() => handleFetch());
|
|
164
165
|
} else {
|
|
165
166
|
handleFetch();
|
|
@@ -171,8 +172,8 @@ export const useFetch = <T extends CommandInstance>(
|
|
|
171
172
|
// ******************
|
|
172
173
|
|
|
173
174
|
const handleMountEvents = () => {
|
|
174
|
-
addDataListener(
|
|
175
|
-
addLifecycleListeners(
|
|
175
|
+
addDataListener(request);
|
|
176
|
+
addLifecycleListeners(request);
|
|
176
177
|
|
|
177
178
|
const focusUnmount = appManager.events.onFocus(() => {
|
|
178
179
|
if (refreshOnFocus) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestInstance, ExtractResponseType, ExtractErrorType, CacheValueType } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { UseRequestEventsActionsType, UseTrackedStateActions, UseTrackedStateType } from "helpers";
|
|
4
4
|
import { InvalidationKeyType } from "types";
|
|
5
5
|
import { isEqual } from "utils";
|
|
6
6
|
|
|
7
|
-
export type UseFetchOptionsType<T extends
|
|
7
|
+
export type UseFetchOptionsType<T extends RequestInstance> = {
|
|
8
8
|
/**
|
|
9
9
|
* Refetch dependencies
|
|
10
10
|
*/
|
|
@@ -24,7 +24,7 @@ export type UseFetchOptionsType<T extends CommandInstance> = {
|
|
|
24
24
|
/**
|
|
25
25
|
* If cache is empty we can use placeholder data.
|
|
26
26
|
*/
|
|
27
|
-
initialData?: CacheValueType<
|
|
27
|
+
initialData?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
|
|
28
28
|
/**
|
|
29
29
|
* Enable/disable refresh data
|
|
30
30
|
*/
|
|
@@ -61,15 +61,19 @@ export type UseFetchOptionsType<T extends CommandInstance> = {
|
|
|
61
61
|
* How long it should bounce requests.
|
|
62
62
|
*/
|
|
63
63
|
bounceTime?: number;
|
|
64
|
+
/**
|
|
65
|
+
* ONLY in throttle mode - options for handling last bounce event
|
|
66
|
+
*/
|
|
67
|
+
bounceTimeout?: number;
|
|
64
68
|
/**
|
|
65
69
|
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
66
70
|
*/
|
|
67
71
|
deepCompare?: boolean | typeof isEqual;
|
|
68
72
|
};
|
|
69
73
|
|
|
70
|
-
export type UseFetchReturnType<T extends
|
|
74
|
+
export type UseFetchReturnType<T extends RequestInstance> = UseTrackedStateType<T> &
|
|
71
75
|
UseTrackedStateActions<T> &
|
|
72
|
-
|
|
76
|
+
UseRequestEventsActionsType<T> & {
|
|
73
77
|
/**
|
|
74
78
|
* Data related to current state of the bounce usage
|
|
75
79
|
*/
|
|
@@ -84,7 +88,7 @@ export type UseFetchReturnType<T extends CommandInstance> = UseTrackedStateType<
|
|
|
84
88
|
reset: () => void;
|
|
85
89
|
};
|
|
86
90
|
/**
|
|
87
|
-
* Revalidate current
|
|
91
|
+
* Revalidate current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
88
92
|
*/
|
|
89
93
|
revalidate: (invalidateKey?: InvalidationKeyType | InvalidationKeyType[]) => void;
|
|
90
94
|
};
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { useRef } from "react";
|
|
2
|
-
import { useDidUpdate, useWillUnmount } from "@better-
|
|
3
|
-
import { ListenerInstance,
|
|
2
|
+
import { useDidUpdate, useWillUnmount } from "@better-hooks/lifecycle";
|
|
3
|
+
import { ListenerInstance, ExtractListenerAdapterType } from "@hyper-fetch/sockets";
|
|
4
4
|
|
|
5
5
|
import { useSocketState } from "helpers";
|
|
6
6
|
import { UseListenerOptionsType } from "hooks/use-listener";
|
|
7
|
+
import { useConfigProvider } from "config-provider";
|
|
7
8
|
|
|
8
9
|
export const useListener = <ListenerType extends ListenerInstance>(
|
|
9
10
|
listener: ListenerType,
|
|
10
|
-
|
|
11
|
+
options: UseListenerOptionsType,
|
|
11
12
|
) => {
|
|
13
|
+
const [globalConfig] = useConfigProvider();
|
|
14
|
+
const { dependencyTracking } = { ...globalConfig.useListener, ...options };
|
|
15
|
+
|
|
12
16
|
const onEventCallback = useRef<
|
|
13
17
|
| null
|
|
14
18
|
| ((
|
|
15
|
-
data:
|
|
16
|
-
event: MessageEvent<
|
|
19
|
+
data: ExtractListenerAdapterType<ListenerType>,
|
|
20
|
+
event: MessageEvent<ExtractListenerAdapterType<ListenerType>>,
|
|
17
21
|
) => void)
|
|
18
22
|
>(null);
|
|
19
23
|
const removeListenerRef = useRef<ReturnType<typeof listener.listen> | null>(null);
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { useState, useRef } from "react";
|
|
2
|
-
import {
|
|
3
|
-
import { useDidMount, useDidUpdate } from "@better-
|
|
2
|
+
import { RequestInstance, getRequestDispatcher, DispatcherStorageValueType } from "@hyper-fetch/core";
|
|
3
|
+
import { useDidMount, useDidUpdate } from "@better-hooks/lifecycle";
|
|
4
4
|
|
|
5
5
|
import { UseQueueOptionsType, useQueueDefaultOptions, QueueRequest, UseQueueReturnType } from "hooks/use-queue";
|
|
6
6
|
import { useConfigProvider } from "config-provider";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* This hook allows to control dispatchers request queues
|
|
10
|
-
* @param
|
|
10
|
+
* @param request
|
|
11
11
|
* @param options
|
|
12
12
|
* @returns
|
|
13
13
|
*/
|
|
14
|
-
export const useQueue = <
|
|
15
|
-
|
|
14
|
+
export const useQueue = <Request extends RequestInstance>(
|
|
15
|
+
request: Request,
|
|
16
16
|
options: UseQueueOptionsType = useQueueDefaultOptions,
|
|
17
|
-
): UseQueueReturnType<
|
|
17
|
+
): UseQueueReturnType<Request> => {
|
|
18
18
|
// Build the configuration options
|
|
19
19
|
const [globalConfig] = useConfigProvider();
|
|
20
20
|
const { queueType = "auto" } = {
|
|
@@ -23,22 +23,22 @@ export const useQueue = <Command extends CommandInstance>(
|
|
|
23
23
|
...options,
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
const { abortKey, queueKey,
|
|
27
|
-
const {
|
|
26
|
+
const { abortKey, queueKey, client } = request;
|
|
27
|
+
const { requestManager } = client;
|
|
28
28
|
|
|
29
|
-
const [dispatcher] =
|
|
29
|
+
const [dispatcher] = getRequestDispatcher(request, queueType);
|
|
30
30
|
|
|
31
31
|
const unmountCallbacks = useRef<null | VoidFunction>(null);
|
|
32
32
|
|
|
33
33
|
const [stopped, setStopped] = useState(false);
|
|
34
|
-
const [requests, setRequests] = useState<QueueRequest<
|
|
34
|
+
const [requests, setRequests] = useState<QueueRequest<Request>[]>([]);
|
|
35
35
|
|
|
36
36
|
// ******************
|
|
37
37
|
// Mapping
|
|
38
38
|
// ******************
|
|
39
39
|
|
|
40
|
-
const createRequestsArray = (queueElements:
|
|
41
|
-
return queueElements.map<QueueRequest<
|
|
40
|
+
const createRequestsArray = (queueElements: DispatcherStorageValueType<Request>[]): QueueRequest<Request>[] => {
|
|
41
|
+
return queueElements.map<QueueRequest<Request>>((req) => ({
|
|
42
42
|
...req,
|
|
43
43
|
stopRequest: () => dispatcher.stopRequest(queueKey, req.requestId),
|
|
44
44
|
startRequest: () => dispatcher.startRequest(queueKey, req.requestId),
|
|
@@ -46,7 +46,7 @@ export const useQueue = <Command extends CommandInstance>(
|
|
|
46
46
|
}));
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
-
const
|
|
49
|
+
const mergePayloadType = (requestId: string, data: Partial<QueueRequest<Request>>) => {
|
|
50
50
|
setRequests((prev) => prev.map((el) => (el.requestId === requestId ? { ...el, ...data } : el)));
|
|
51
51
|
};
|
|
52
52
|
|
|
@@ -55,13 +55,13 @@ export const useQueue = <Command extends CommandInstance>(
|
|
|
55
55
|
// ******************
|
|
56
56
|
|
|
57
57
|
const getInitialState = () => {
|
|
58
|
-
const
|
|
58
|
+
const requestQueue = dispatcher.getQueue<Request>(queueKey);
|
|
59
59
|
|
|
60
|
-
setStopped(
|
|
61
|
-
setRequests(createRequestsArray(
|
|
60
|
+
setStopped(requestQueue.stopped);
|
|
61
|
+
setRequests(createRequestsArray(requestQueue.requests));
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
-
const updateQueueState = (values: { requests: QueueRequest<
|
|
64
|
+
const updateQueueState = (values: { requests: QueueRequest<Request>[]; stopped: boolean }) => {
|
|
65
65
|
setStopped(values.stopped);
|
|
66
66
|
setRequests(createRequestsArray(values.requests));
|
|
67
67
|
};
|
|
@@ -71,15 +71,15 @@ export const useQueue = <Command extends CommandInstance>(
|
|
|
71
71
|
// ******************
|
|
72
72
|
|
|
73
73
|
const mountEvents = () => {
|
|
74
|
-
const unmountChange = dispatcher.events.onQueueChange<
|
|
75
|
-
const unmountStatus = dispatcher.events.onQueueStatus<
|
|
74
|
+
const unmountChange = dispatcher.events.onQueueChange<Request>(queueKey, updateQueueState);
|
|
75
|
+
const unmountStatus = dispatcher.events.onQueueStatus<Request>(queueKey, updateQueueState);
|
|
76
76
|
|
|
77
|
-
const unmountDownload =
|
|
78
|
-
|
|
77
|
+
const unmountDownload = requestManager.events.onDownloadProgress(queueKey, (progress, { requestId }) => {
|
|
78
|
+
mergePayloadType(requestId, { downloading: progress });
|
|
79
79
|
});
|
|
80
80
|
|
|
81
|
-
const unmountUpload =
|
|
82
|
-
|
|
81
|
+
const unmountUpload = requestManager.events.onUploadProgress(queueKey, (progress, { requestId }) => {
|
|
82
|
+
mergePayloadType(requestId, { uploading: progress });
|
|
83
83
|
});
|
|
84
84
|
|
|
85
85
|
const unmount = () => {
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProgressType, RequestInstance, DispatcherStorageValueType } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
export type UseQueueOptionsType = {
|
|
4
4
|
queueType?: "auto" | "fetch" | "submit";
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
export type QueueRequest<
|
|
7
|
+
export type QueueRequest<Request extends RequestInstance> = DispatcherStorageValueType<Request> & {
|
|
8
8
|
/**
|
|
9
9
|
* Uploading progress for given request
|
|
10
10
|
*/
|
|
11
|
-
uploading?:
|
|
11
|
+
uploading?: ProgressType;
|
|
12
12
|
/**
|
|
13
13
|
* Downloading progress for given request
|
|
14
14
|
*/
|
|
15
|
-
downloading?:
|
|
15
|
+
downloading?: ProgressType;
|
|
16
16
|
/**
|
|
17
17
|
* Callback which allow to start previously stopped request.
|
|
18
18
|
*/
|
|
@@ -27,13 +27,13 @@ export type QueueRequest<Command extends CommandInstance> = DispatcherDumpValueT
|
|
|
27
27
|
deleteRequest: () => void;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
export type UseQueueReturnType<T extends
|
|
30
|
+
export type UseQueueReturnType<T extends RequestInstance> = {
|
|
31
31
|
/**
|
|
32
|
-
* Queue status for provided
|
|
32
|
+
* Queue status for provided request
|
|
33
33
|
*/
|
|
34
34
|
stopped: boolean;
|
|
35
35
|
/**
|
|
36
|
-
* List of requests for provided
|
|
36
|
+
* List of requests for provided request
|
|
37
37
|
*/
|
|
38
38
|
requests: QueueRequest<T>[];
|
|
39
39
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestInstance, RequiredKeys } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { UseSubmitOptionsType } from "./use-submit.types";
|
|
4
4
|
|
|
5
|
-
type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<
|
|
5
|
+
type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<RequestInstance>, "initialData">> & {
|
|
6
6
|
initialData: null;
|
|
7
7
|
};
|
|
8
8
|
|
|
@@ -13,5 +13,6 @@ export const useSubmitDefaultOptions: DefaultOptionsType = {
|
|
|
13
13
|
bounce: false,
|
|
14
14
|
bounceType: "debounce",
|
|
15
15
|
bounceTime: 400,
|
|
16
|
+
bounceTimeout: 200,
|
|
16
17
|
deepCompare: true,
|
|
17
18
|
};
|