@hyper-fetch/react 1.6.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 +7 -0
- package/README.md +82 -0
- package/dist/config-provider/config-provider.d.ts +13 -0
- package/dist/config-provider/config-provider.types.d.ts +17 -0
- package/dist/config-provider/index.d.ts +2 -0
- package/dist/helpers/index.d.ts +2 -0
- package/dist/helpers/use-command-events/index.d.ts +2 -0
- package/dist/helpers/use-command-events/use-command-events.hooks.d.ts +9 -0
- package/dist/helpers/use-command-events/use-command-events.types.d.ts +80 -0
- package/dist/helpers/use-tracked-state/index.d.ts +4 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +2 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +11 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +72 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +7 -0
- package/dist/hooks/use-app-manager/index.d.ts +2 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +3 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +18 -0
- package/dist/hooks/use-cache/index.d.ts +3 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts +5 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +3 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts +35 -0
- package/dist/hooks/use-fetch/index.d.ts +4 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts +7 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +9 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts +85 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts +1 -0
- package/dist/hooks/use-queue/index.d.ts +3 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts +3 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +9 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts +48 -0
- package/dist/hooks/use-submit/index.d.ts +3 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts +7 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +9 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts +101 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +7 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/invalidation.types.d.ts +2 -0
- package/dist/utils/bounce.utils.d.ts +7 -0
- package/dist/utils/deep-equal.utils.d.ts +13 -0
- package/dist/utils/index.d.ts +2 -0
- package/package.json +74 -0
- package/src/config-provider/config-provider.tsx +30 -0
- package/src/config-provider/config-provider.types.ts +21 -0
- package/src/config-provider/index.ts +2 -0
- package/src/helpers/index.ts +2 -0
- package/src/helpers/use-command-events/index.ts +2 -0
- package/src/helpers/use-command-events/use-command-events.hooks.ts +283 -0
- package/src/helpers/use-command-events/use-command-events.types.ts +108 -0
- package/src/helpers/use-tracked-state/index.ts +4 -0
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +10 -0
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +227 -0
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +85 -0
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +82 -0
- package/src/hooks/use-app-manager/index.ts +2 -0
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +36 -0
- package/src/hooks/use-app-manager/use-app-manager.types.ts +18 -0
- package/src/hooks/use-cache/index.ts +3 -0
- package/src/hooks/use-cache/use-cache.constants.ts +5 -0
- package/src/hooks/use-cache/use-cache.hooks.ts +90 -0
- package/src/hooks/use-cache/use-cache.types.ts +45 -0
- package/src/hooks/use-fetch/index.ts +4 -0
- package/src/hooks/use-fetch/use-fetch.constants.ts +25 -0
- package/src/hooks/use-fetch/use-fetch.hooks.ts +265 -0
- package/src/hooks/use-fetch/use-fetch.types.ts +90 -0
- package/src/hooks/use-fetch/use-fetch.utils.ts +7 -0
- package/src/hooks/use-queue/index.ts +3 -0
- package/src/hooks/use-queue/use-queue.constants.ts +7 -0
- package/src/hooks/use-queue/use-queue.hooks.ts +112 -0
- package/src/hooks/use-queue/use-queue.types.ts +51 -0
- package/src/hooks/use-submit/index.ts +3 -0
- package/src/hooks/use-submit/use-submit.constants.ts +17 -0
- package/src/hooks/use-submit/use-submit.hooks.ts +219 -0
- package/src/hooks/use-submit/use-submit.types.ts +119 -0
- package/src/index.ts +8 -0
- package/src/types/index.ts +1 -0
- package/src/types/invalidation.types.ts +3 -0
- package/src/utils/bounce.utils.ts +9 -0
- package/src/utils/deep-equal.utils.ts +64 -0
- package/src/utils/index.ts +2 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
import { useDidUpdate, useDidMount } from "@better-typed/react-lifecycle-hooks";
|
|
3
|
+
import { useDebounce, useThrottle } from "@better-typed/react-performance-hooks";
|
|
4
|
+
import { CommandInstance, Command, getCommandKey } from "@hyper-fetch/core";
|
|
5
|
+
|
|
6
|
+
import { useCommandEvents, useTrackedState } from "helpers";
|
|
7
|
+
import { UseFetchOptionsType, useFetchDefaultOptions, UseFetchReturnType, getRefreshTime } from "hooks/use-fetch";
|
|
8
|
+
import { useConfigProvider } from "config-provider";
|
|
9
|
+
import { getBounceData } from "utils";
|
|
10
|
+
import { InvalidationKeyType } from "types";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This hooks aims to retrieve data from server.
|
|
14
|
+
* @param command Command instance
|
|
15
|
+
* @param options Hook options
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export const useFetch = <T extends CommandInstance>(
|
|
19
|
+
command: T,
|
|
20
|
+
options: UseFetchOptionsType<T> = useFetchDefaultOptions,
|
|
21
|
+
): UseFetchReturnType<T> => {
|
|
22
|
+
// Build the configuration options
|
|
23
|
+
const [globalConfig] = useConfigProvider();
|
|
24
|
+
const {
|
|
25
|
+
dependencies = useFetchDefaultOptions.dependencies,
|
|
26
|
+
disabled = useFetchDefaultOptions.disabled,
|
|
27
|
+
dependencyTracking = useFetchDefaultOptions.dependencyTracking,
|
|
28
|
+
revalidateOnMount = useFetchDefaultOptions.revalidateOnMount,
|
|
29
|
+
initialData = useFetchDefaultOptions.initialData,
|
|
30
|
+
refresh = useFetchDefaultOptions.refresh,
|
|
31
|
+
refreshTime = useFetchDefaultOptions.refreshTime,
|
|
32
|
+
refreshBlurred = useFetchDefaultOptions.refreshBlurred,
|
|
33
|
+
refreshOnBlur = useFetchDefaultOptions.refreshOnBlur,
|
|
34
|
+
refreshOnFocus = useFetchDefaultOptions.refreshOnFocus,
|
|
35
|
+
refreshOnReconnect = useFetchDefaultOptions.refreshOnReconnect,
|
|
36
|
+
bounce = useFetchDefaultOptions.bounce,
|
|
37
|
+
bounceType = useFetchDefaultOptions.bounceType,
|
|
38
|
+
bounceTime = useFetchDefaultOptions.bounceTime,
|
|
39
|
+
deepCompare = useFetchDefaultOptions.deepCompare,
|
|
40
|
+
} = {
|
|
41
|
+
...useFetchDefaultOptions,
|
|
42
|
+
...globalConfig.useFetchConfig,
|
|
43
|
+
...options,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const updateKey = JSON.stringify(command.dump());
|
|
47
|
+
const requestDebounce = useDebounce(bounceTime);
|
|
48
|
+
const requestThrottle = useThrottle(bounceTime);
|
|
49
|
+
const refreshDebounce = useDebounce(refreshTime);
|
|
50
|
+
|
|
51
|
+
const { cacheKey, queueKey, builder } = command;
|
|
52
|
+
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = builder;
|
|
53
|
+
|
|
54
|
+
const logger = useRef(loggerManager.init("useFetch")).current;
|
|
55
|
+
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
56
|
+
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
60
|
+
*/
|
|
61
|
+
const [state, actions, { setRenderKey, setCacheData, getStaleStatus }] = useTrackedState<T>({
|
|
62
|
+
logger,
|
|
63
|
+
command,
|
|
64
|
+
dispatcher,
|
|
65
|
+
initialData,
|
|
66
|
+
deepCompare,
|
|
67
|
+
dependencyTracking,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
72
|
+
*/
|
|
73
|
+
const [callbacks, listeners] = useCommandEvents({
|
|
74
|
+
logger,
|
|
75
|
+
actions,
|
|
76
|
+
command,
|
|
77
|
+
dispatcher,
|
|
78
|
+
setCacheData,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const { addDataListener, addLifecycleListeners, clearDataListener, clearLifecycleListeners } = listeners;
|
|
82
|
+
|
|
83
|
+
// ******************
|
|
84
|
+
// Fetching
|
|
85
|
+
// ******************
|
|
86
|
+
const handleFetch = () => {
|
|
87
|
+
if (!disabled) {
|
|
88
|
+
logger.debug(`Fetching data`);
|
|
89
|
+
dispatcher.add(command);
|
|
90
|
+
} else {
|
|
91
|
+
logger.debug(`Cannot add to fetch queue`, { disabled });
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// ******************
|
|
96
|
+
// Refreshing
|
|
97
|
+
// ******************
|
|
98
|
+
|
|
99
|
+
function handleRefresh() {
|
|
100
|
+
if (!refresh) return;
|
|
101
|
+
const time = getRefreshTime(refreshTime, state.timestamp);
|
|
102
|
+
|
|
103
|
+
logger.debug(`Starting refresh counter, request will be send in ${time}ms`);
|
|
104
|
+
|
|
105
|
+
refreshDebounce.debounce(() => {
|
|
106
|
+
const isBlurred = !appManager.isFocused;
|
|
107
|
+
|
|
108
|
+
// If window tab is not active should we refresh the cache
|
|
109
|
+
const isFetching = dispatcher.hasRunningRequests(command.queueKey);
|
|
110
|
+
const isQueued = dispatcher.getIsActiveQueue(command.queueKey);
|
|
111
|
+
const isActive = isFetching || isQueued;
|
|
112
|
+
const canRefreshBlurred = isBlurred && refreshBlurred && !isActive;
|
|
113
|
+
const canRefreshFocused = !isBlurred && !isActive;
|
|
114
|
+
|
|
115
|
+
if (canRefreshBlurred || canRefreshFocused) {
|
|
116
|
+
handleFetch();
|
|
117
|
+
logger.debug(`Performing refresh request`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Start new refresh counter
|
|
121
|
+
handleRefresh();
|
|
122
|
+
}, time);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const handleRevalidation = (invalidateKey: InvalidationKeyType) => {
|
|
126
|
+
if (invalidateKey && invalidateKey instanceof Command) {
|
|
127
|
+
cache.revalidate(getCommandKey(invalidateKey));
|
|
128
|
+
} else if (invalidateKey && !(invalidateKey instanceof Command)) {
|
|
129
|
+
cache.revalidate(invalidateKey);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const revalidate = (invalidateKey?: InvalidationKeyType | InvalidationKeyType[]) => {
|
|
134
|
+
if (invalidateKey && Array.isArray(invalidateKey)) {
|
|
135
|
+
invalidateKey.forEach(handleRevalidation);
|
|
136
|
+
} else if (invalidateKey && !Array.isArray(invalidateKey)) {
|
|
137
|
+
handleRevalidation(invalidateKey);
|
|
138
|
+
} else {
|
|
139
|
+
handleFetch();
|
|
140
|
+
handleRefresh();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// ******************
|
|
145
|
+
// Fetching lifecycle
|
|
146
|
+
// ******************
|
|
147
|
+
|
|
148
|
+
const initialFetchData = () => {
|
|
149
|
+
const hasStaleData = getStaleStatus();
|
|
150
|
+
const isFetching = dispatcher.getIsActiveQueue(queueKey);
|
|
151
|
+
if (revalidateOnMount || (hasStaleData && !isFetching)) {
|
|
152
|
+
handleFetch();
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const updateFetchData = () => {
|
|
157
|
+
/**
|
|
158
|
+
* While debouncing we need to make sure that first request is not debounced when the cache is not available
|
|
159
|
+
* This way it will not wait for debouncing but fetch data right away
|
|
160
|
+
*/
|
|
161
|
+
if (bounce) {
|
|
162
|
+
logger.debug(`Bounce request with ${bounceType}`, { queueKey, command });
|
|
163
|
+
bounceFunction(() => handleFetch());
|
|
164
|
+
} else {
|
|
165
|
+
handleFetch();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// ******************
|
|
170
|
+
// Events
|
|
171
|
+
// ******************
|
|
172
|
+
|
|
173
|
+
const handleMountEvents = () => {
|
|
174
|
+
addDataListener(command);
|
|
175
|
+
addLifecycleListeners(command);
|
|
176
|
+
|
|
177
|
+
const focusUnmount = appManager.events.onFocus(() => {
|
|
178
|
+
if (refreshOnFocus) {
|
|
179
|
+
handleFetch();
|
|
180
|
+
handleRefresh();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const blurUnmount = appManager.events.onBlur(() => {
|
|
184
|
+
if (refreshOnBlur) {
|
|
185
|
+
handleFetch();
|
|
186
|
+
handleRefresh();
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const onlineUnmount = appManager.events.onOnline(() => {
|
|
190
|
+
if (refreshOnReconnect) {
|
|
191
|
+
handleFetch();
|
|
192
|
+
handleRefresh();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const revalidateUnmount = cache.events.onRevalidate(cacheKey, handleFetch);
|
|
197
|
+
|
|
198
|
+
const unmount = () => {
|
|
199
|
+
clearDataListener();
|
|
200
|
+
clearLifecycleListeners();
|
|
201
|
+
focusUnmount();
|
|
202
|
+
blurUnmount();
|
|
203
|
+
onlineUnmount();
|
|
204
|
+
revalidateUnmount();
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return unmount;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// ******************
|
|
211
|
+
// Lifecycle
|
|
212
|
+
// ******************
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Initialization of the events related to data exchange with cache and queue
|
|
216
|
+
* This allow to share the state with other hooks and keep it related
|
|
217
|
+
*/
|
|
218
|
+
useDidUpdate(handleMountEvents, [updateKey], true);
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Initial fetch triggered once data is stale or we use the revalidate strategy
|
|
222
|
+
*/
|
|
223
|
+
useDidMount(initialFetchData);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Fetching logic for updates handling
|
|
227
|
+
*/
|
|
228
|
+
useDidUpdate(updateFetchData, [updateKey, ...dependencies]);
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Refresh lifecycle handler
|
|
232
|
+
*/
|
|
233
|
+
useDidUpdate(handleRefresh, [updateKey, ...dependencies, disabled, refresh, refreshTime], true);
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
get data() {
|
|
237
|
+
setRenderKey("data");
|
|
238
|
+
return state.data;
|
|
239
|
+
},
|
|
240
|
+
get error() {
|
|
241
|
+
setRenderKey("error");
|
|
242
|
+
return state.error;
|
|
243
|
+
},
|
|
244
|
+
get loading() {
|
|
245
|
+
setRenderKey("loading");
|
|
246
|
+
return state.loading;
|
|
247
|
+
},
|
|
248
|
+
get status() {
|
|
249
|
+
setRenderKey("status");
|
|
250
|
+
return state.status;
|
|
251
|
+
},
|
|
252
|
+
get retries() {
|
|
253
|
+
setRenderKey("retries");
|
|
254
|
+
return state.retries;
|
|
255
|
+
},
|
|
256
|
+
get timestamp() {
|
|
257
|
+
setRenderKey("timestamp");
|
|
258
|
+
return state.timestamp;
|
|
259
|
+
},
|
|
260
|
+
bounce: getBounceData(bounceData),
|
|
261
|
+
...actions,
|
|
262
|
+
...callbacks,
|
|
263
|
+
revalidate,
|
|
264
|
+
};
|
|
265
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CommandInstance, ExtractResponse, ExtractError, CacheValueType } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
import { UseCommandEventsActionsType, UseTrackedStateActions, UseTrackedStateType } from "helpers";
|
|
4
|
+
import { InvalidationKeyType } from "types";
|
|
5
|
+
import { isEqual } from "utils";
|
|
6
|
+
|
|
7
|
+
export type UseFetchOptionsType<T extends CommandInstance> = {
|
|
8
|
+
/**
|
|
9
|
+
* Refetch dependencies
|
|
10
|
+
*/
|
|
11
|
+
dependencies?: any[];
|
|
12
|
+
/**
|
|
13
|
+
* Disable fetching
|
|
14
|
+
*/
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* If `true` it will rerender only when values used by our component gets changed. Otherwise it will rerender on any change.
|
|
18
|
+
*/
|
|
19
|
+
dependencyTracking?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* If `true` it will refetch data in background no matter if we have it from cache.
|
|
22
|
+
*/
|
|
23
|
+
revalidateOnMount?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* If cache is empty we can use placeholder data.
|
|
26
|
+
*/
|
|
27
|
+
initialData?: CacheValueType<ExtractResponse<T>, ExtractError<T>>["data"] | null;
|
|
28
|
+
/**
|
|
29
|
+
* Enable/disable refresh data
|
|
30
|
+
*/
|
|
31
|
+
refresh?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Refresh data interval time
|
|
34
|
+
*/
|
|
35
|
+
refreshTime?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Enable/disable data refresh if our tab is not focused(used by user at given time).
|
|
38
|
+
*/
|
|
39
|
+
refreshBlurred?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Enable/disable data refresh if user leaves current tab.
|
|
42
|
+
*/
|
|
43
|
+
refreshOnBlur?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Enable/disable data refresh if user enters current tab.
|
|
46
|
+
*/
|
|
47
|
+
refreshOnFocus?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Enable/disable data refresh if network is restored.
|
|
50
|
+
*/
|
|
51
|
+
refreshOnReconnect?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
|
|
54
|
+
*/
|
|
55
|
+
bounce?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Possibility to choose between debounce and throttle approaches
|
|
58
|
+
*/
|
|
59
|
+
bounceType?: "debounce" | "throttle";
|
|
60
|
+
/**
|
|
61
|
+
* How long it should bounce requests.
|
|
62
|
+
*/
|
|
63
|
+
bounceTime?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
66
|
+
*/
|
|
67
|
+
deepCompare?: boolean | typeof isEqual;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type UseFetchReturnType<T extends CommandInstance> = UseTrackedStateType<T> &
|
|
71
|
+
UseTrackedStateActions<T> &
|
|
72
|
+
UseCommandEventsActionsType<T> & {
|
|
73
|
+
/**
|
|
74
|
+
* Data related to current state of the bounce usage
|
|
75
|
+
*/
|
|
76
|
+
bounce: {
|
|
77
|
+
/**
|
|
78
|
+
* Active state of the bounce method
|
|
79
|
+
*/
|
|
80
|
+
active: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Method to stop the active bounce method execution
|
|
83
|
+
*/
|
|
84
|
+
reset: () => void;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Revalidate current command resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
88
|
+
*/
|
|
89
|
+
revalidate: (invalidateKey?: InvalidationKeyType | InvalidationKeyType[]) => void;
|
|
90
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useState, useRef } from "react";
|
|
2
|
+
import { CommandInstance, getCommandDispatcher, DispatcherDumpValueType } from "@hyper-fetch/core";
|
|
3
|
+
import { useDidMount, useDidUpdate } from "@better-typed/react-lifecycle-hooks";
|
|
4
|
+
|
|
5
|
+
import { UseQueueOptionsType, useQueueDefaultOptions, QueueRequest, UseQueueReturnType } from "hooks/use-queue";
|
|
6
|
+
import { useConfigProvider } from "config-provider";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This hook allows to control dispatchers request queues
|
|
10
|
+
* @param command
|
|
11
|
+
* @param options
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export const useQueue = <Command extends CommandInstance>(
|
|
15
|
+
command: Command,
|
|
16
|
+
options: UseQueueOptionsType = useQueueDefaultOptions,
|
|
17
|
+
): UseQueueReturnType<Command> => {
|
|
18
|
+
// Build the configuration options
|
|
19
|
+
const [globalConfig] = useConfigProvider();
|
|
20
|
+
const { queueType = "auto" } = {
|
|
21
|
+
...useQueueDefaultOptions,
|
|
22
|
+
...globalConfig.useQueueConfig,
|
|
23
|
+
...options,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const { abortKey, queueKey, builder } = command;
|
|
27
|
+
const { commandManager } = builder;
|
|
28
|
+
|
|
29
|
+
const [dispatcher] = getCommandDispatcher(command, queueType);
|
|
30
|
+
|
|
31
|
+
const unmountCallbacks = useRef<null | VoidFunction>(null);
|
|
32
|
+
|
|
33
|
+
const [stopped, setStopped] = useState(false);
|
|
34
|
+
const [requests, setRequests] = useState<QueueRequest<Command>[]>([]);
|
|
35
|
+
|
|
36
|
+
// ******************
|
|
37
|
+
// Mapping
|
|
38
|
+
// ******************
|
|
39
|
+
|
|
40
|
+
const createRequestsArray = (queueElements: DispatcherDumpValueType<Command>[]): QueueRequest<Command>[] => {
|
|
41
|
+
return queueElements.map<QueueRequest<Command>>((req) => ({
|
|
42
|
+
...req,
|
|
43
|
+
stopRequest: () => dispatcher.stopRequest(queueKey, req.requestId),
|
|
44
|
+
startRequest: () => dispatcher.startRequest(queueKey, req.requestId),
|
|
45
|
+
deleteRequest: () => dispatcher.delete(queueKey, req.requestId, abortKey),
|
|
46
|
+
}));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const mergeRequestData = (requestId: string, data: Partial<QueueRequest<Command>>) => {
|
|
50
|
+
setRequests((prev) => prev.map((el) => (el.requestId === requestId ? { ...el, ...data } : el)));
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// ******************
|
|
54
|
+
// State
|
|
55
|
+
// ******************
|
|
56
|
+
|
|
57
|
+
const getInitialState = () => {
|
|
58
|
+
const commandQueue = dispatcher.getQueue<Command>(queueKey);
|
|
59
|
+
|
|
60
|
+
setStopped(commandQueue.stopped);
|
|
61
|
+
setRequests(createRequestsArray(commandQueue.requests));
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const updateQueueState = (values: { requests: QueueRequest<Command>[]; stopped: boolean }) => {
|
|
65
|
+
setStopped(values.stopped);
|
|
66
|
+
setRequests(createRequestsArray(values.requests));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// ******************
|
|
70
|
+
// Events
|
|
71
|
+
// ******************
|
|
72
|
+
|
|
73
|
+
const mountEvents = () => {
|
|
74
|
+
const unmountChange = dispatcher.events.onQueueChange<Command>(queueKey, updateQueueState);
|
|
75
|
+
const unmountStatus = dispatcher.events.onQueueStatus<Command>(queueKey, updateQueueState);
|
|
76
|
+
|
|
77
|
+
const unmountDownload = commandManager.events.onDownloadProgress(queueKey, (progress, { requestId }) => {
|
|
78
|
+
mergeRequestData(requestId, { downloading: progress });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const unmountUpload = commandManager.events.onUploadProgress(queueKey, (progress, { requestId }) => {
|
|
82
|
+
mergeRequestData(requestId, { uploading: progress });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const unmount = () => {
|
|
86
|
+
unmountStatus();
|
|
87
|
+
unmountChange();
|
|
88
|
+
unmountDownload();
|
|
89
|
+
unmountUpload();
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
unmountCallbacks.current?.();
|
|
93
|
+
unmountCallbacks.current = unmount;
|
|
94
|
+
return unmount;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// ******************
|
|
98
|
+
// Lifecycle
|
|
99
|
+
// ******************
|
|
100
|
+
|
|
101
|
+
useDidMount(getInitialState);
|
|
102
|
+
|
|
103
|
+
useDidUpdate(mountEvents, [stopped, requests, setRequests, setStopped], true);
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
stopped,
|
|
107
|
+
requests,
|
|
108
|
+
stop: () => dispatcher.stop(queueKey),
|
|
109
|
+
pause: () => dispatcher.pause(queueKey),
|
|
110
|
+
start: () => dispatcher.start(queueKey),
|
|
111
|
+
};
|
|
112
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { FetchProgressType, CommandInstance, DispatcherDumpValueType } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
export type UseQueueOptionsType = {
|
|
4
|
+
queueType?: "auto" | "fetch" | "submit";
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type QueueRequest<Command extends CommandInstance> = DispatcherDumpValueType<Command> & {
|
|
8
|
+
/**
|
|
9
|
+
* Uploading progress for given request
|
|
10
|
+
*/
|
|
11
|
+
uploading?: FetchProgressType;
|
|
12
|
+
/**
|
|
13
|
+
* Downloading progress for given request
|
|
14
|
+
*/
|
|
15
|
+
downloading?: FetchProgressType;
|
|
16
|
+
/**
|
|
17
|
+
* Callback which allow to start previously stopped request.
|
|
18
|
+
*/
|
|
19
|
+
startRequest: () => void;
|
|
20
|
+
/**
|
|
21
|
+
* Callback which allow to stop request and cancel it if it's ongoing.
|
|
22
|
+
*/
|
|
23
|
+
stopRequest: () => void;
|
|
24
|
+
/**
|
|
25
|
+
* Removes request from the queue
|
|
26
|
+
*/
|
|
27
|
+
deleteRequest: () => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type UseQueueReturnType<T extends CommandInstance> = {
|
|
31
|
+
/**
|
|
32
|
+
* Queue status for provided command
|
|
33
|
+
*/
|
|
34
|
+
stopped: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* List of requests for provided command
|
|
37
|
+
*/
|
|
38
|
+
requests: QueueRequest<T>[];
|
|
39
|
+
/**
|
|
40
|
+
* Callback which allow to stop queue, it will cancel ongoing requests.
|
|
41
|
+
*/
|
|
42
|
+
stop: () => void;
|
|
43
|
+
/**
|
|
44
|
+
* Callback which allow to pause queue. It will allow ongoing requests to be finished, but will stop next from being send.
|
|
45
|
+
*/
|
|
46
|
+
pause: () => void;
|
|
47
|
+
/**
|
|
48
|
+
* Callback which allow to start queue.
|
|
49
|
+
*/
|
|
50
|
+
start: () => void;
|
|
51
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CommandInstance, RequiredKeys } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
import { UseSubmitOptionsType } from "./use-submit.types";
|
|
4
|
+
|
|
5
|
+
type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<CommandInstance>, "initialData">> & {
|
|
6
|
+
initialData: null;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const useSubmitDefaultOptions: DefaultOptionsType = {
|
|
10
|
+
disabled: false,
|
|
11
|
+
dependencyTracking: true,
|
|
12
|
+
initialData: null,
|
|
13
|
+
bounce: false,
|
|
14
|
+
bounceType: "debounce",
|
|
15
|
+
bounceTime: 400,
|
|
16
|
+
deepCompare: true,
|
|
17
|
+
};
|