@hyper-fetch/react 6.0.0 → 7.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/.eslintrc.json +8 -3
- package/README.md +5 -5
- package/dist/index.d.ts +179 -157
- package/dist/{index.esm.js → index.js} +500 -435
- package/dist/index.js.map +7 -0
- package/package.json +12 -13
- package/src/helpers/use-request-events/use-request-events.hooks.ts +73 -80
- package/src/helpers/use-request-events/use-request-events.types.ts +24 -22
- package/src/helpers/use-socket-state/use-socket-state.constants.ts +4 -1
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +39 -29
- package/src/helpers/use-socket-state/use-socket-state.types.ts +4 -1
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +4 -3
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +79 -98
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +44 -25
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +79 -34
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
- package/src/hooks/use-cache/use-cache.constants.ts +1 -1
- package/src/hooks/use-cache/use-cache.hooks.ts +32 -27
- package/src/hooks/use-cache/use-cache.types.ts +4 -22
- package/src/hooks/use-emitter/use-emitter.hooks.ts +50 -18
- package/src/hooks/use-fetch/use-fetch.constants.ts +6 -5
- package/src/hooks/use-fetch/use-fetch.hooks.ts +71 -82
- package/src/hooks/use-fetch/use-fetch.types.ts +28 -9
- package/src/hooks/use-listener/use-listener.hooks.ts +15 -21
- package/src/hooks/use-queue/use-queue.constants.ts +2 -1
- package/src/hooks/use-queue/use-queue.hooks.ts +123 -42
- package/src/hooks/use-queue/use-queue.types.ts +14 -5
- package/src/hooks/use-submit/use-submit.constants.ts +3 -3
- package/src/hooks/use-submit/use-submit.hooks.ts +39 -53
- package/src/hooks/use-submit/use-submit.types.ts +5 -6
- package/src/index.ts +1 -3
- package/src/provider/index.ts +2 -0
- package/src/{config-provider/config-provider.tsx → provider/provider.tsx} +8 -5
- package/src/{config-provider/config-provider.types.ts → provider/provider.types.ts} +15 -8
- package/src/utils/deep-equal.utils.ts +1 -3
- package/tsconfig.json +2 -2
- package/dist/index.cjs.js +0 -1514
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js.map +0 -7
- package/src/config-provider/index.ts +0 -2
- package/src/hooks/use-event-messages/index.ts +0 -2
- package/src/hooks/use-event-messages/use-event-messages.hooks.ts +0 -69
- package/src/hooks/use-event-messages/use-event-messages.types.ts +0 -4
- package/src/types/index.ts +0 -1
- package/src/types/invalidation.types.ts +0 -3
|
@@ -3,12 +3,15 @@ import {
|
|
|
3
3
|
ExtractErrorType,
|
|
4
4
|
CacheValueType,
|
|
5
5
|
ExtractResponseType,
|
|
6
|
-
|
|
7
|
-
ProgressType,
|
|
8
|
-
ExtractAdapterReturnType,
|
|
6
|
+
LoggerMethods,
|
|
9
7
|
RequestEventType,
|
|
10
8
|
RequestInstance,
|
|
11
|
-
|
|
9
|
+
RequestProgressEventType,
|
|
10
|
+
RequestResponseEventType,
|
|
11
|
+
ResponseErrorType,
|
|
12
|
+
ExtractAdapterType,
|
|
13
|
+
ResponseType,
|
|
14
|
+
ResponseSuccessType,
|
|
12
15
|
} from "@hyper-fetch/core";
|
|
13
16
|
|
|
14
17
|
import { UseTrackedStateActions } from "helpers";
|
|
@@ -20,8 +23,8 @@ export type UseRequestEventsLifecycleMap = Map<string, { unmount: VoidFunction }
|
|
|
20
23
|
// Props
|
|
21
24
|
export type UseRequestEventsPropsType<T extends RequestInstance> = {
|
|
22
25
|
request: T;
|
|
23
|
-
dispatcher: Dispatcher
|
|
24
|
-
logger:
|
|
26
|
+
dispatcher: Dispatcher<ExtractAdapterType<T>>;
|
|
27
|
+
logger: LoggerMethods;
|
|
25
28
|
actions: UseTrackedStateActions<T>;
|
|
26
29
|
setCacheData: (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => void;
|
|
27
30
|
getIsDataProcessing: (cacheKey: string) => boolean;
|
|
@@ -29,7 +32,7 @@ export type UseRequestEventsPropsType<T extends RequestInstance> = {
|
|
|
29
32
|
|
|
30
33
|
export type UseRequestEventsActionsType<T extends RequestInstance> = {
|
|
31
34
|
/**
|
|
32
|
-
* Callback that allows canceling ongoing requests from the given
|
|
35
|
+
* Callback that allows canceling ongoing requests from the given queryKey.
|
|
33
36
|
*/
|
|
34
37
|
abort: () => void;
|
|
35
38
|
/**
|
|
@@ -74,9 +77,9 @@ export type UseRequestEventsActionsType<T extends RequestInstance> = {
|
|
|
74
77
|
export type UseRequestEventsReturnType<T extends RequestInstance> = [
|
|
75
78
|
UseRequestEventsActionsType<T>,
|
|
76
79
|
{
|
|
77
|
-
addCacheDataListener: (request:
|
|
80
|
+
addCacheDataListener: (request: T) => VoidFunction;
|
|
78
81
|
clearCacheDataListener: VoidFunction;
|
|
79
|
-
addLifecycleListeners: (request:
|
|
82
|
+
addLifecycleListeners: (request: T, requestId?: string) => VoidFunction;
|
|
80
83
|
removeLifecycleListener: (requestId: string) => void;
|
|
81
84
|
clearLifecycleListeners: () => void;
|
|
82
85
|
},
|
|
@@ -84,26 +87,25 @@ export type UseRequestEventsReturnType<T extends RequestInstance> = [
|
|
|
84
87
|
|
|
85
88
|
// Lifecycle
|
|
86
89
|
|
|
87
|
-
export type CallbackParameters<Request, ResponseType> = {
|
|
90
|
+
export type CallbackParameters<Request extends RequestInstance, ResponseType> = {
|
|
88
91
|
response: ResponseType;
|
|
89
|
-
|
|
90
|
-
request: Request;
|
|
91
|
-
};
|
|
92
|
+
} & Omit<RequestResponseEventType<Request>, "response">;
|
|
92
93
|
|
|
93
94
|
export type OnSuccessCallbackType<Request extends RequestInstance> = (
|
|
94
|
-
params: CallbackParameters<Request, ExtractResponseType<Request
|
|
95
|
+
params: CallbackParameters<Request, ResponseSuccessType<ExtractResponseType<Request>, ExtractAdapterType<Request>>>,
|
|
95
96
|
) => void | Promise<void>;
|
|
96
97
|
export type OnErrorCallbackType<Request extends RequestInstance> = (
|
|
97
|
-
params: CallbackParameters<Request, ExtractErrorType<Request
|
|
98
|
+
params: CallbackParameters<Request, ResponseErrorType<ExtractErrorType<Request>, ExtractAdapterType<Request>>>,
|
|
98
99
|
) => void | Promise<void>;
|
|
99
100
|
export type OnFinishedCallbackType<Request extends RequestInstance> = (
|
|
100
|
-
params: CallbackParameters<
|
|
101
|
+
params: CallbackParameters<
|
|
102
|
+
Request,
|
|
103
|
+
ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>
|
|
104
|
+
>,
|
|
105
|
+
) => void | Promise<void>;
|
|
106
|
+
export type OnStartCallbackType<Request extends RequestInstance> = (
|
|
107
|
+
data: RequestEventType<Request>,
|
|
101
108
|
) => void | Promise<void>;
|
|
102
|
-
export type OnStartCallbackType<Request extends RequestInstance> = (params: {
|
|
103
|
-
details: RequestEventType<Request>;
|
|
104
|
-
request: Request;
|
|
105
|
-
}) => void | Promise<void>;
|
|
106
109
|
export type OnProgressCallbackType = <Request extends RequestInstance>(
|
|
107
|
-
|
|
108
|
-
details: RequestEventType<Request>,
|
|
110
|
+
data: RequestProgressEventType<Request>,
|
|
109
111
|
) => void | Promise<void>;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { SocketInstance } from "@hyper-fetch/sockets";
|
|
2
|
+
|
|
1
3
|
import { UseSocketStateType } from "./use-socket-state.types";
|
|
2
4
|
|
|
3
|
-
export const initialSocketState: UseSocketStateType = {
|
|
5
|
+
export const initialSocketState: UseSocketStateType<SocketInstance> = {
|
|
4
6
|
data: null,
|
|
7
|
+
extra: null,
|
|
5
8
|
connected: false,
|
|
6
9
|
connecting: false,
|
|
7
10
|
timestamp: null,
|
|
@@ -1,32 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SocketInstance } from "@hyper-fetch/sockets";
|
|
1
|
+
import { SocketInstance, ExtractSocketExtraType } from "@hyper-fetch/sockets";
|
|
3
2
|
import { useDidMount, useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
|
|
3
|
+
import { useRef } from "react";
|
|
4
4
|
|
|
5
5
|
import { UseSocketStateType, initialSocketState, UseSocketStateProps } from "helpers";
|
|
6
6
|
|
|
7
|
-
export const useSocketState = <DataType
|
|
7
|
+
export const useSocketState = <DataType, Socket extends SocketInstance>(
|
|
8
|
+
socket: Socket,
|
|
9
|
+
{ dependencyTracking }: UseSocketStateProps,
|
|
10
|
+
) => {
|
|
8
11
|
const forceUpdate = useForceUpdate();
|
|
9
12
|
|
|
10
|
-
const
|
|
11
|
-
const onCloseCallback = useRef<null | VoidFunction>(null);
|
|
13
|
+
const onDisconnectCallback = useRef<null | VoidFunction>(null);
|
|
12
14
|
const onErrorCallback = useRef<null | ((event: any) => void)>(null);
|
|
15
|
+
const onConnectedCallback = useRef<null | VoidFunction>(null);
|
|
13
16
|
const onConnectingCallback = useRef<null | VoidFunction>(null);
|
|
14
|
-
const onReconnectingCallback = useRef<null | ((attempts: number) => void)>(null);
|
|
15
|
-
const
|
|
17
|
+
const onReconnectingCallback = useRef<null | ((data: { attempts: number }) => void)>(null);
|
|
18
|
+
const onReconnectingFailedCallback = useRef<null | ((data: { attempts: number }) => void)>(null);
|
|
16
19
|
|
|
17
|
-
const state = useRef<UseSocketStateType<DataType>>(initialSocketState);
|
|
18
|
-
const renderKeys = useRef<Array<keyof UseSocketStateType<DataType>>>([]);
|
|
20
|
+
const state = useRef<UseSocketStateType<Socket, DataType>>(initialSocketState);
|
|
21
|
+
const renderKeys = useRef<Array<keyof UseSocketStateType<Socket, DataType>>>([]);
|
|
19
22
|
|
|
20
23
|
// ******************
|
|
21
24
|
// Dependency Tracking
|
|
22
25
|
// ******************
|
|
23
26
|
|
|
24
|
-
const renderKeyTrigger = (keys: Array<keyof UseSocketStateType<DataType>>) => {
|
|
27
|
+
const renderKeyTrigger = (keys: Array<keyof UseSocketStateType<Socket, DataType>>) => {
|
|
25
28
|
const shouldRerender = renderKeys.current.some((renderKey) => keys.includes(renderKey));
|
|
26
29
|
if (shouldRerender) forceUpdate();
|
|
27
30
|
};
|
|
28
31
|
|
|
29
|
-
const setRenderKey = (renderKey: keyof UseSocketStateType<DataType>) => {
|
|
32
|
+
const setRenderKey = (renderKey: keyof UseSocketStateType<Socket, DataType>) => {
|
|
30
33
|
if (!renderKeys.current.includes(renderKey)) {
|
|
31
34
|
renderKeys.current.push(renderKey);
|
|
32
35
|
}
|
|
@@ -38,6 +41,9 @@ export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTra
|
|
|
38
41
|
|
|
39
42
|
useDidUpdate(
|
|
40
43
|
() => {
|
|
44
|
+
state.current.connected = socket.adapter.connected;
|
|
45
|
+
state.current.connecting = socket.adapter.connecting;
|
|
46
|
+
|
|
41
47
|
const handleDependencyTracking = () => {
|
|
42
48
|
if (!dependencyTracking) {
|
|
43
49
|
Object.keys(state.current).forEach((key) => setRenderKey(key as Parameters<typeof setRenderKey>[0]));
|
|
@@ -59,6 +65,10 @@ export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTra
|
|
|
59
65
|
state.current.data = data;
|
|
60
66
|
renderKeyTrigger(["data"]);
|
|
61
67
|
},
|
|
68
|
+
setExtra: (extra: ExtractSocketExtraType<Socket> | null) => {
|
|
69
|
+
state.current.extra = extra;
|
|
70
|
+
renderKeyTrigger(["extra"]);
|
|
71
|
+
},
|
|
62
72
|
setConnected: (connected: boolean) => {
|
|
63
73
|
state.current.connected = connected;
|
|
64
74
|
renderKeyTrigger(["data"]);
|
|
@@ -74,11 +84,11 @@ export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTra
|
|
|
74
84
|
};
|
|
75
85
|
|
|
76
86
|
const callbacks = {
|
|
77
|
-
|
|
78
|
-
|
|
87
|
+
onConnected: (callback: VoidFunction) => {
|
|
88
|
+
onConnectedCallback.current = callback;
|
|
79
89
|
},
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
onDisconnected: (callback: VoidFunction) => {
|
|
91
|
+
onDisconnectCallback.current = callback;
|
|
82
92
|
},
|
|
83
93
|
onError: <ErrorType = Event>(callback: (event: ErrorType) => void) => {
|
|
84
94
|
onErrorCallback.current = callback;
|
|
@@ -86,11 +96,11 @@ export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTra
|
|
|
86
96
|
onConnecting: (callback: VoidFunction) => {
|
|
87
97
|
onConnectingCallback.current = callback;
|
|
88
98
|
},
|
|
89
|
-
onReconnecting: (callback: (attempts: number) => void) => {
|
|
99
|
+
onReconnecting: (callback: (data: { attempts: number }) => void) => {
|
|
90
100
|
onReconnectingCallback.current = callback;
|
|
91
101
|
},
|
|
92
|
-
|
|
93
|
-
|
|
102
|
+
onReconnectingFailed: (callback: (data: { attempts: number }) => void) => {
|
|
103
|
+
onReconnectingFailedCallback.current = callback;
|
|
94
104
|
},
|
|
95
105
|
};
|
|
96
106
|
|
|
@@ -102,23 +112,23 @@ export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTra
|
|
|
102
112
|
const umountOnError = socket.events.onError((event) => {
|
|
103
113
|
onErrorCallback.current?.(event);
|
|
104
114
|
});
|
|
105
|
-
const umountOnConnecting = socket.events.onConnecting(() => {
|
|
106
|
-
actions.setConnecting(
|
|
115
|
+
const umountOnConnecting = socket.events.onConnecting(({ connecting }) => {
|
|
116
|
+
actions.setConnecting(connecting);
|
|
107
117
|
onConnectingCallback.current?.();
|
|
108
118
|
});
|
|
109
|
-
const umountOnOpen = socket.events.
|
|
119
|
+
const umountOnOpen = socket.events.onConnected(() => {
|
|
110
120
|
actions.setConnected(true);
|
|
111
|
-
|
|
121
|
+
onConnectedCallback.current?.();
|
|
112
122
|
});
|
|
113
|
-
const umountOnClose = socket.events.
|
|
123
|
+
const umountOnClose = socket.events.onDisconnected(() => {
|
|
114
124
|
actions.setConnected(false);
|
|
115
|
-
|
|
125
|
+
onDisconnectCallback.current?.();
|
|
116
126
|
});
|
|
117
|
-
const umountOnReconnecting = socket.events.onReconnecting((attempts) => {
|
|
118
|
-
onReconnectingCallback.current?.(attempts);
|
|
127
|
+
const umountOnReconnecting = socket.events.onReconnecting(({ attempts }) => {
|
|
128
|
+
onReconnectingCallback.current?.({ attempts });
|
|
119
129
|
});
|
|
120
|
-
const
|
|
121
|
-
|
|
130
|
+
const umountOnReconnectingFailed = socket.events.onReconnectingFailed(({ attempts }) => {
|
|
131
|
+
onReconnectingFailedCallback.current?.({ attempts });
|
|
122
132
|
});
|
|
123
133
|
|
|
124
134
|
return () => {
|
|
@@ -127,7 +137,7 @@ export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTra
|
|
|
127
137
|
umountOnOpen();
|
|
128
138
|
umountOnClose();
|
|
129
139
|
umountOnReconnecting();
|
|
130
|
-
|
|
140
|
+
umountOnReconnectingFailed();
|
|
131
141
|
};
|
|
132
142
|
});
|
|
133
143
|
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { ExtractSocketExtraType, SocketInstance } from "@hyper-fetch/sockets";
|
|
2
|
+
|
|
3
|
+
export type UseSocketStateType<Socket extends SocketInstance, DataType = any> = {
|
|
2
4
|
data: DataType | null;
|
|
5
|
+
extra: ExtractSocketExtraType<Socket> | null;
|
|
3
6
|
connected: boolean;
|
|
4
7
|
connecting: boolean;
|
|
5
8
|
timestamp: number | null;
|
|
@@ -4,9 +4,10 @@ export const initialState: UseTrackedStateType = {
|
|
|
4
4
|
data: null,
|
|
5
5
|
error: null,
|
|
6
6
|
status: null,
|
|
7
|
-
extra:
|
|
8
|
-
success:
|
|
7
|
+
extra: {},
|
|
8
|
+
success: false,
|
|
9
9
|
loading: false,
|
|
10
10
|
retries: 0,
|
|
11
|
-
|
|
11
|
+
responseTimestamp: null,
|
|
12
|
+
requestTimestamp: null,
|
|
12
13
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
2
|
import { useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
|
|
3
3
|
import {
|
|
4
4
|
ExtractErrorType,
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
ExtractAdapterType,
|
|
9
9
|
ExtractAdapterExtraType,
|
|
10
10
|
} from "@hyper-fetch/core";
|
|
11
|
+
import { useRef } from "react";
|
|
11
12
|
|
|
12
13
|
import { isEqual } from "utils";
|
|
13
14
|
import {
|
|
@@ -16,12 +17,12 @@ import {
|
|
|
16
17
|
UseTrackedStateProps,
|
|
17
18
|
UseTrackedStateReturn,
|
|
18
19
|
} from "./use-tracked-state.types";
|
|
19
|
-
import {
|
|
20
|
+
import { getInitialState, getIsInitiallyLoading, getValidCacheData, isStaleCacheData } from "./use-tracked-state.utils";
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
*
|
|
23
24
|
* @param request
|
|
24
|
-
* @param
|
|
25
|
+
* @param initialResponse
|
|
25
26
|
* @param dispatcher
|
|
26
27
|
* @param dependencies
|
|
27
28
|
* @internal
|
|
@@ -29,17 +30,18 @@ import { getDetailsState, getInitialState, getValidCacheData, isStaleCacheData }
|
|
|
29
30
|
export const useTrackedState = <T extends RequestInstance>({
|
|
30
31
|
request,
|
|
31
32
|
dispatcher,
|
|
32
|
-
|
|
33
|
+
initialResponse,
|
|
33
34
|
deepCompare,
|
|
34
35
|
dependencyTracking,
|
|
35
|
-
|
|
36
|
+
disabled,
|
|
37
|
+
revalidate,
|
|
36
38
|
}: UseTrackedStateProps<T>): UseTrackedStateReturn<T> => {
|
|
37
|
-
const { client, cacheKey,
|
|
38
|
-
const { cache
|
|
39
|
+
const { client, cacheKey, queryKey, staleTime, unstable_responseMapper } = request;
|
|
40
|
+
const { cache } = client;
|
|
39
41
|
|
|
40
42
|
const forceUpdate = useForceUpdate();
|
|
41
43
|
|
|
42
|
-
const state = useRef<UseTrackedStateType<T>>(getInitialState(
|
|
44
|
+
const state = useRef<UseTrackedStateType<T>>(getInitialState({ initialResponse, dispatcher, request, disabled }));
|
|
43
45
|
const renderKeys = useRef<Array<keyof UseTrackedStateType<T>>>([]);
|
|
44
46
|
const isProcessingData = useRef("");
|
|
45
47
|
|
|
@@ -49,7 +51,7 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
49
51
|
|
|
50
52
|
const getStaleStatus = (): boolean => {
|
|
51
53
|
const cacheData = cache.get(cacheKey);
|
|
52
|
-
return !cacheData || isStaleCacheData(
|
|
54
|
+
return !cacheData || isStaleCacheData(staleTime, cacheData?.responseTimestamp);
|
|
53
55
|
};
|
|
54
56
|
|
|
55
57
|
// ******************
|
|
@@ -73,23 +75,20 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
73
75
|
|
|
74
76
|
useDidUpdate(
|
|
75
77
|
() => {
|
|
78
|
+
// Get cache state
|
|
79
|
+
const cacheData = cache.get<ExtractResponseType<T>, ExtractErrorType<T>>(cacheKey);
|
|
80
|
+
const cacheState = getValidCacheData<T>(request, initialResponse, cacheData);
|
|
81
|
+
|
|
76
82
|
// Handle initial loading state
|
|
77
|
-
state.current.loading =
|
|
83
|
+
state.current.loading = getIsInitiallyLoading({
|
|
84
|
+
queryKey: request.queryKey,
|
|
85
|
+
dispatcher,
|
|
86
|
+
disabled,
|
|
87
|
+
revalidate,
|
|
88
|
+
hasState: !!cacheState,
|
|
89
|
+
});
|
|
78
90
|
|
|
79
|
-
|
|
80
|
-
const cacheData = cache.get<
|
|
81
|
-
ExtractResponseType<T>,
|
|
82
|
-
ExtractErrorType<T>,
|
|
83
|
-
ExtractAdapterExtraType<ExtractAdapterType<T>>
|
|
84
|
-
>(cacheKey);
|
|
85
|
-
const cacheState = getValidCacheData<T>(request, initialData, cacheData);
|
|
86
|
-
|
|
87
|
-
const hasInitialState = isEqual(initialData?.data, state.current.data);
|
|
88
|
-
const hasState = !!(state.current.data || state.current.error) && !hasInitialState;
|
|
89
|
-
const shouldLoadInitialCache = !hasState && !!state.current.data;
|
|
90
|
-
const shouldRemovePreviousData = hasState && !state.current.data;
|
|
91
|
-
|
|
92
|
-
if (cacheState || shouldLoadInitialCache || shouldRemovePreviousData) {
|
|
91
|
+
if (cacheState) {
|
|
93
92
|
// Don't update the state when we are fetching data for new cacheKey
|
|
94
93
|
// So on paginated page we will have previous page access until the new one will be fetched
|
|
95
94
|
// However: When we have some cached data, we can use it right away
|
|
@@ -97,7 +96,7 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
97
96
|
setCacheData(cacheState);
|
|
98
97
|
}
|
|
99
98
|
},
|
|
100
|
-
[cacheKey,
|
|
99
|
+
[cacheKey, queryKey],
|
|
101
100
|
true,
|
|
102
101
|
);
|
|
103
102
|
|
|
@@ -141,10 +140,11 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
141
140
|
error: cacheData.error,
|
|
142
141
|
status: cacheData.status,
|
|
143
142
|
success: cacheData.success,
|
|
144
|
-
extra: cacheData.extra
|
|
143
|
+
extra: cacheData.extra as ExtractAdapterExtraType<ExtractAdapterType<T>>,
|
|
145
144
|
retries: cacheData.retries,
|
|
146
|
-
|
|
147
|
-
|
|
145
|
+
responseTimestamp: new Date(cacheData.responseTimestamp),
|
|
146
|
+
requestTimestamp: new Date(cacheData.requestTimestamp),
|
|
147
|
+
loading: dispatcher.hasRunningRequests(queryKey),
|
|
148
148
|
};
|
|
149
149
|
|
|
150
150
|
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
@@ -187,7 +187,7 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
187
187
|
cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
|
|
188
188
|
): Promise<void> | void => {
|
|
189
189
|
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: true });
|
|
190
|
-
const data =
|
|
190
|
+
const data = unstable_responseMapper ? unstable_responseMapper(cacheData) : cacheData;
|
|
191
191
|
|
|
192
192
|
if (data instanceof Promise) {
|
|
193
193
|
return (async () => {
|
|
@@ -205,82 +205,63 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
205
205
|
// ******************
|
|
206
206
|
|
|
207
207
|
const actions: UseTrackedStateActions<T> = {
|
|
208
|
-
setData: (data
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
cache.set(request, { ...currentState, ...getDetailsState(state.current), success: true, data });
|
|
212
|
-
} else {
|
|
213
|
-
state.current.data = data;
|
|
214
|
-
renderKeyTrigger(["data"]);
|
|
215
|
-
}
|
|
208
|
+
setData: (data) => {
|
|
209
|
+
state.current.data = data instanceof Function ? data(state.current.data || null) : data;
|
|
210
|
+
renderKeyTrigger(["data"]);
|
|
216
211
|
},
|
|
217
|
-
setError: (error
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
cache.set(request, { ...currentState, ...getDetailsState(state.current), success: false, error });
|
|
221
|
-
} else {
|
|
222
|
-
state.current.error = error;
|
|
223
|
-
renderKeyTrigger(["error"]);
|
|
224
|
-
}
|
|
212
|
+
setError: (error) => {
|
|
213
|
+
state.current.error = error instanceof Function ? error(state.current.error || null) : error;
|
|
214
|
+
renderKeyTrigger(["error"]);
|
|
225
215
|
},
|
|
226
|
-
setLoading: (loading
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
loading,
|
|
232
|
-
isRetry: false,
|
|
233
|
-
isOffline: false,
|
|
234
|
-
});
|
|
235
|
-
} else {
|
|
236
|
-
state.current.loading = loading;
|
|
237
|
-
renderKeyTrigger(["loading"]);
|
|
238
|
-
}
|
|
216
|
+
setLoading: (loading) => {
|
|
217
|
+
const value = loading instanceof Function ? loading(state.current.loading) : loading;
|
|
218
|
+
if (value === state.current.loading) return;
|
|
219
|
+
state.current.loading = value;
|
|
220
|
+
renderKeyTrigger(["loading"]);
|
|
239
221
|
},
|
|
240
|
-
setStatus: (status
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
state.current.status = status;
|
|
246
|
-
renderKeyTrigger(["status"]);
|
|
247
|
-
}
|
|
222
|
+
setStatus: (status) => {
|
|
223
|
+
const value = status instanceof Function ? status(state.current.status) : status;
|
|
224
|
+
if (value === state.current.status) return;
|
|
225
|
+
state.current.status = status instanceof Function ? status(state.current.status || null) : status;
|
|
226
|
+
renderKeyTrigger(["status"]);
|
|
248
227
|
},
|
|
249
|
-
setSuccess: (success
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
state.current.success = success;
|
|
255
|
-
renderKeyTrigger(["success"]);
|
|
256
|
-
}
|
|
228
|
+
setSuccess: (success) => {
|
|
229
|
+
const value = success instanceof Function ? success(state.current.success || false) : success;
|
|
230
|
+
if (value === state.current.success) return;
|
|
231
|
+
state.current.success = success instanceof Function ? success(state.current.success || false) : success;
|
|
232
|
+
renderKeyTrigger(["success"]);
|
|
257
233
|
},
|
|
258
|
-
setExtra: (extra
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
state.current.extra = extra;
|
|
264
|
-
renderKeyTrigger(["extra"]);
|
|
265
|
-
}
|
|
234
|
+
setExtra: (extra) => {
|
|
235
|
+
const value = extra instanceof Function ? extra(state.current.extra) : extra;
|
|
236
|
+
if (value === state.current.extra) return;
|
|
237
|
+
state.current.extra = extra instanceof Function ? extra(state.current.extra) : extra;
|
|
238
|
+
renderKeyTrigger(["extra"]);
|
|
266
239
|
},
|
|
267
|
-
setRetries: (retries
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
state.current.retries = retries;
|
|
273
|
-
renderKeyTrigger(["retries"]);
|
|
274
|
-
}
|
|
240
|
+
setRetries: (retries) => {
|
|
241
|
+
const value = retries instanceof Function ? retries(state.current.retries || 0) : retries;
|
|
242
|
+
if (value === state.current.retries) return;
|
|
243
|
+
state.current.retries = retries instanceof Function ? retries(state.current.retries || 0) : retries;
|
|
244
|
+
renderKeyTrigger(["retries"]);
|
|
275
245
|
},
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
246
|
+
setResponseTimestamp: (timestamp) => {
|
|
247
|
+
const value = timestamp instanceof Function ? timestamp(state.current.responseTimestamp) : timestamp;
|
|
248
|
+
if (value === state.current.responseTimestamp) return;
|
|
249
|
+
const getTimestamp = (prev: Date | null) => {
|
|
250
|
+
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
state.current.responseTimestamp = getTimestamp(state.current.responseTimestamp);
|
|
254
|
+
renderKeyTrigger(["responseTimestamp"]);
|
|
255
|
+
},
|
|
256
|
+
setRequestTimestamp: (timestamp) => {
|
|
257
|
+
const value = timestamp instanceof Function ? timestamp(state.current.requestTimestamp) : timestamp;
|
|
258
|
+
if (value === state.current.requestTimestamp) return;
|
|
259
|
+
const getTimestamp = (prev: Date | null) => {
|
|
260
|
+
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
state.current.requestTimestamp = getTimestamp(state.current.requestTimestamp);
|
|
264
|
+
renderKeyTrigger(["requestTimestamp"]);
|
|
284
265
|
},
|
|
285
266
|
};
|
|
286
267
|
|
|
@@ -4,24 +4,26 @@ import {
|
|
|
4
4
|
CacheValueType,
|
|
5
5
|
ExtractResponseType,
|
|
6
6
|
RequestInstance,
|
|
7
|
-
LoggerType,
|
|
8
7
|
ExtractAdapterType,
|
|
9
8
|
ExtractAdapterStatusType,
|
|
10
9
|
ExtractAdapterExtraType,
|
|
11
|
-
|
|
10
|
+
ExtractAdapterResolvedType,
|
|
12
11
|
NullableType,
|
|
12
|
+
CacheSetState,
|
|
13
|
+
LoggerMethods,
|
|
13
14
|
} from "@hyper-fetch/core";
|
|
14
15
|
|
|
15
16
|
import { isEqual } from "utils";
|
|
16
17
|
|
|
17
18
|
export type UseTrackedStateProps<T extends RequestInstance> = {
|
|
18
19
|
request: T;
|
|
19
|
-
logger:
|
|
20
|
-
|
|
21
|
-
dispatcher: Dispatcher
|
|
20
|
+
logger: LoggerMethods;
|
|
21
|
+
initialResponse: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
|
|
22
|
+
dispatcher: Dispatcher<ExtractAdapterType<T>>;
|
|
22
23
|
dependencyTracking: boolean;
|
|
23
|
-
defaultCacheEmitting?: boolean;
|
|
24
24
|
deepCompare: boolean | typeof isEqual;
|
|
25
|
+
disabled?: boolean;
|
|
26
|
+
revalidate?: boolean;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
export type UseTrackedStateReturn<T extends RequestInstance> = [
|
|
@@ -51,11 +53,11 @@ export type UseTrackedStateType<T extends RequestInstance = RequestInstance> = {
|
|
|
51
53
|
/**
|
|
52
54
|
* Request status
|
|
53
55
|
*/
|
|
54
|
-
status: ExtractAdapterStatusType<ExtractAdapterType<T
|
|
56
|
+
status: null | ExtractAdapterStatusType<ExtractAdapterType<T>> | null;
|
|
55
57
|
/**
|
|
56
58
|
* Request additional response data
|
|
57
59
|
*/
|
|
58
|
-
extra: ExtractAdapterExtraType<ExtractAdapterType<T>>;
|
|
60
|
+
extra: null | ExtractAdapterExtraType<ExtractAdapterType<T>>;
|
|
59
61
|
/**
|
|
60
62
|
* Information whether request succeeded
|
|
61
63
|
*/
|
|
@@ -67,40 +69,57 @@ export type UseTrackedStateType<T extends RequestInstance = RequestInstance> = {
|
|
|
67
69
|
/**
|
|
68
70
|
* Request response timestamp
|
|
69
71
|
*/
|
|
70
|
-
|
|
72
|
+
responseTimestamp: null | Date;
|
|
73
|
+
/**
|
|
74
|
+
* Request response timestamp
|
|
75
|
+
*/
|
|
76
|
+
requestTimestamp: null | Date;
|
|
71
77
|
};
|
|
72
78
|
|
|
73
79
|
export type UseTrackedStateActions<T extends RequestInstance> = {
|
|
74
80
|
/**
|
|
75
|
-
* Action to set custom data. We can do it locally(inside hook state)
|
|
81
|
+
* Action to set custom data. We can do it locally(inside hook state).
|
|
82
|
+
* If you need to update cache data use client.cache.update(). method.
|
|
83
|
+
*/
|
|
84
|
+
setData: (data: CacheSetState<ExtractResponseType<T> | null>) => void;
|
|
85
|
+
/**
|
|
86
|
+
* Action to set custom error. We can do it locally(inside hook state).
|
|
87
|
+
* If you need to update cache data use client.cache.update() method.
|
|
76
88
|
*/
|
|
77
|
-
|
|
89
|
+
setError: (error: CacheSetState<ExtractErrorType<T> | null>) => void;
|
|
78
90
|
/**
|
|
79
|
-
* Action to set custom
|
|
91
|
+
* Action to set custom loading. We can do it locally(inside hook state).
|
|
92
|
+
* If you need to update cache data use client.cache.update() method.
|
|
80
93
|
*/
|
|
81
|
-
|
|
94
|
+
setLoading: (loading: CacheSetState<boolean>) => void;
|
|
82
95
|
/**
|
|
83
|
-
* Action to set custom
|
|
96
|
+
* Action to set custom status. We can do it locally(inside hook state).
|
|
97
|
+
* If you need to turn on loading for all listening hooks use client.requestManager.events.emitLoading() method.
|
|
84
98
|
*/
|
|
85
|
-
|
|
99
|
+
setStatus: (status: CacheSetState<ExtractAdapterStatusType<ExtractAdapterType<T>>>) => void;
|
|
86
100
|
/**
|
|
87
|
-
* Action to set custom
|
|
101
|
+
* Action to set custom success. We can do it locally(inside hook state).
|
|
102
|
+
* If you need to update cache data use client.cache.update() method.
|
|
88
103
|
*/
|
|
89
|
-
|
|
104
|
+
setSuccess: (success: CacheSetState<boolean>) => void;
|
|
90
105
|
/**
|
|
91
|
-
* Action to set custom
|
|
106
|
+
* Action to set custom additional data. We can do it locally(inside hook state).
|
|
107
|
+
* If you need to update cache data use client.cache.update() method.
|
|
92
108
|
*/
|
|
93
|
-
|
|
109
|
+
setExtra: (extra: CacheSetState<ExtractAdapterExtraType<ExtractAdapterType<T>> | null>) => void;
|
|
94
110
|
/**
|
|
95
|
-
* Action to set custom
|
|
111
|
+
* Action to set custom retries count. We can do it locally(inside hook state).
|
|
112
|
+
* If you need to update cache data use client.cache.update() method.
|
|
96
113
|
*/
|
|
97
|
-
|
|
114
|
+
setRetries: (retries: CacheSetState<number>) => void;
|
|
98
115
|
/**
|
|
99
|
-
* Action to set custom
|
|
116
|
+
* Action to set custom timestamp. We can do it locally(inside hook state).
|
|
117
|
+
* If you need to update cache data use client.cache.update() method.
|
|
100
118
|
*/
|
|
101
|
-
|
|
119
|
+
setResponseTimestamp: (timestamp: CacheSetState<Date>) => void;
|
|
102
120
|
/**
|
|
103
|
-
* Action to set custom timestamp. We can do it locally(inside hook state)
|
|
121
|
+
* Action to set custom timestamp. We can do it locally(inside hook state).
|
|
122
|
+
* If you need to update cache data use client.cache.update() method.
|
|
104
123
|
*/
|
|
105
|
-
|
|
124
|
+
setRequestTimestamp: (timestamp: CacheSetState<Date>) => void;
|
|
106
125
|
};
|