@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
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
1
2
|
import {
|
|
2
3
|
CacheValueType,
|
|
3
4
|
NullableType,
|
|
4
5
|
RequestInstance,
|
|
5
6
|
ExtractResponseType,
|
|
6
7
|
ExtractErrorType,
|
|
7
|
-
|
|
8
|
+
ExtractAdapterResolvedType,
|
|
8
9
|
Dispatcher,
|
|
9
10
|
ExtractAdapterType,
|
|
10
11
|
ExtractAdapterExtraType,
|
|
11
12
|
ResponseDetailsType,
|
|
13
|
+
ExtractAdapterStatusType,
|
|
12
14
|
} from "@hyper-fetch/core";
|
|
13
15
|
|
|
14
16
|
import { initialState, UseTrackedStateType } from "helpers";
|
|
@@ -19,41 +21,48 @@ export const getDetailsState = (
|
|
|
19
21
|
): ResponseDetailsType => {
|
|
20
22
|
return {
|
|
21
23
|
retries: state?.retries || 0,
|
|
22
|
-
timestamp: +new Date(),
|
|
23
24
|
isCanceled: false,
|
|
24
25
|
isOffline: false,
|
|
26
|
+
addedTimestamp: +new Date(),
|
|
27
|
+
triggerTimestamp: +new Date(),
|
|
28
|
+
requestTimestamp: +new Date(),
|
|
29
|
+
responseTimestamp: +new Date(),
|
|
25
30
|
...details,
|
|
26
31
|
};
|
|
27
32
|
};
|
|
28
33
|
|
|
29
|
-
export const isStaleCacheData = (
|
|
30
|
-
if (!
|
|
31
|
-
return +new Date() > +
|
|
34
|
+
export const isStaleCacheData = (staleTime: number, staleTimestamp: NullableType<Date | number>) => {
|
|
35
|
+
if (!staleTimestamp) return true;
|
|
36
|
+
return +new Date() > +staleTimestamp + staleTime;
|
|
32
37
|
};
|
|
33
38
|
|
|
34
39
|
export const getValidCacheData = <T extends RequestInstance>(
|
|
35
40
|
request: T,
|
|
36
|
-
|
|
37
|
-
cacheData: NullableType<CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>>,
|
|
38
|
-
): CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>> | null => {
|
|
39
|
-
const isStale = isStaleCacheData(request.
|
|
41
|
+
initialResponse: NullableType<Partial<ExtractAdapterResolvedType<T>>>,
|
|
42
|
+
cacheData: NullableType<CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>>,
|
|
43
|
+
): CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>> | null => {
|
|
44
|
+
const isStale = isStaleCacheData(request.staleTime, cacheData?.responseTimestamp);
|
|
40
45
|
|
|
41
46
|
if (!isStale && cacheData) {
|
|
42
47
|
return cacheData;
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
if (
|
|
50
|
+
if (initialResponse) {
|
|
46
51
|
return {
|
|
47
52
|
data: null,
|
|
48
53
|
error: null,
|
|
49
54
|
status: null,
|
|
50
|
-
success:
|
|
55
|
+
success: true,
|
|
51
56
|
extra: null,
|
|
52
|
-
|
|
57
|
+
cached: !!request.cache,
|
|
58
|
+
...(initialResponse as Partial<ExtractAdapterResolvedType<T>>),
|
|
53
59
|
...getDetailsState(),
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
staleTime: 1000,
|
|
61
|
+
version: request.client.cache.version,
|
|
62
|
+
cacheKey: request.cacheKey,
|
|
63
|
+
cacheTime: request.cacheTime,
|
|
64
|
+
requestTimestamp: initialResponse?.requestTimestamp ?? +new Date(),
|
|
65
|
+
responseTimestamp: initialResponse?.responseTimestamp ?? +new Date(),
|
|
57
66
|
};
|
|
58
67
|
}
|
|
59
68
|
|
|
@@ -64,24 +73,58 @@ export const getTimestamp = (timestamp?: NullableType<number | Date>) => {
|
|
|
64
73
|
return timestamp ? new Date(timestamp) : null;
|
|
65
74
|
};
|
|
66
75
|
|
|
67
|
-
export const
|
|
68
|
-
|
|
69
|
-
dispatcher
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
export const getIsInitiallyLoading = <T extends RequestInstance>({
|
|
77
|
+
queryKey,
|
|
78
|
+
dispatcher,
|
|
79
|
+
hasState,
|
|
80
|
+
revalidate,
|
|
81
|
+
disabled,
|
|
82
|
+
}: {
|
|
83
|
+
queryKey: string;
|
|
84
|
+
dispatcher: Dispatcher<ExtractAdapterType<T>>;
|
|
85
|
+
hasState: boolean;
|
|
86
|
+
revalidate?: boolean;
|
|
87
|
+
disabled?: boolean;
|
|
88
|
+
}) => {
|
|
89
|
+
if (!revalidate && hasState) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const queue = dispatcher.getQueue(queryKey);
|
|
94
|
+
const isInitiallyLoading = dispatcher.hasRunningRequests(queryKey) || (!queue.stopped && disabled === false);
|
|
95
|
+
|
|
96
|
+
return isInitiallyLoading;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const getInitialState = <T extends RequestInstance>({
|
|
100
|
+
initialResponse,
|
|
101
|
+
dispatcher,
|
|
102
|
+
request,
|
|
103
|
+
disabled,
|
|
104
|
+
revalidate,
|
|
105
|
+
}: {
|
|
106
|
+
initialResponse: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
|
|
107
|
+
dispatcher: Dispatcher<ExtractAdapterType<T>>;
|
|
108
|
+
request: T;
|
|
109
|
+
disabled?: boolean; // useFetch only
|
|
110
|
+
revalidate?: boolean; // useFetch only
|
|
111
|
+
}): UseTrackedStateType<T> => {
|
|
112
|
+
const { client, cacheKey, unstable_responseMapper } = request;
|
|
73
113
|
const { cache } = client;
|
|
74
114
|
|
|
75
|
-
const cacheData = cache.get<
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
115
|
+
const cacheData = cache.get<ExtractResponseType<T>, ExtractErrorType<T>>(cacheKey);
|
|
116
|
+
const cacheState = getValidCacheData<T>(request, initialResponse, cacheData);
|
|
117
|
+
|
|
118
|
+
const initialLoading = getIsInitiallyLoading({
|
|
119
|
+
queryKey: request.queryKey,
|
|
120
|
+
dispatcher,
|
|
121
|
+
disabled,
|
|
122
|
+
revalidate,
|
|
123
|
+
hasState: !!cacheState,
|
|
124
|
+
});
|
|
82
125
|
|
|
83
126
|
if (cacheState) {
|
|
84
|
-
const mappedData =
|
|
127
|
+
const mappedData = unstable_responseMapper ? unstable_responseMapper(cacheState) : cacheState;
|
|
85
128
|
if (mappedData instanceof Promise) {
|
|
86
129
|
// For the async mapper we cannot return async values
|
|
87
130
|
// So we have return the initial state instead
|
|
@@ -92,9 +135,10 @@ export const getInitialState = <T extends RequestInstance>(
|
|
|
92
135
|
error: mappedData.error,
|
|
93
136
|
status: mappedData.status,
|
|
94
137
|
success: mappedData.success,
|
|
95
|
-
extra: mappedData.extra
|
|
138
|
+
extra: (mappedData.extra || client.adapter.defaultExtra) as ExtractAdapterExtraType<ExtractAdapterType<T>>,
|
|
96
139
|
retries: cacheState.retries,
|
|
97
|
-
|
|
140
|
+
requestTimestamp: getTimestamp(cacheState.requestTimestamp),
|
|
141
|
+
responseTimestamp: getTimestamp(cacheState.responseTimestamp),
|
|
98
142
|
loading: initialLoading,
|
|
99
143
|
};
|
|
100
144
|
}
|
|
@@ -102,11 +146,12 @@ export const getInitialState = <T extends RequestInstance>(
|
|
|
102
146
|
return {
|
|
103
147
|
data: initialState.data,
|
|
104
148
|
error: initialState.error,
|
|
105
|
-
status: initialState.status
|
|
149
|
+
status: initialState.status as ExtractAdapterStatusType<ExtractAdapterType<T>>,
|
|
106
150
|
success: initialState.success,
|
|
107
|
-
extra: request.client.defaultExtra
|
|
151
|
+
extra: request.client.adapter.defaultExtra as ExtractAdapterExtraType<ExtractAdapterType<T>>,
|
|
108
152
|
retries: initialState.retries,
|
|
109
|
-
|
|
153
|
+
requestTimestamp: getTimestamp(initialState.requestTimestamp),
|
|
154
|
+
responseTimestamp: getTimestamp(initialState.responseTimestamp),
|
|
110
155
|
loading: initialLoading,
|
|
111
156
|
};
|
|
112
157
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
2
1
|
import { useDidMount } from "@better-hooks/lifecycle";
|
|
3
2
|
import { ClientInstance } from "@hyper-fetch/core";
|
|
3
|
+
import { useState } from "react";
|
|
4
4
|
|
|
5
5
|
import { UseAppManagerReturnType } from "hooks/use-app-manager";
|
|
6
6
|
|
|
7
|
-
export const useAppManager = <
|
|
7
|
+
export const useAppManager = <Client extends ClientInstance>(client: Client): UseAppManagerReturnType => {
|
|
8
8
|
const [online, setIsOnline] = useState(client.appManager.isOnline);
|
|
9
9
|
const [focused, setIsFocused] = useState(client.appManager.isFocused);
|
|
10
10
|
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { useRef } from "react";
|
|
2
1
|
import { useDidUpdate } from "@better-hooks/lifecycle";
|
|
3
|
-
import { getRequestDispatcher, RequestInstance
|
|
2
|
+
import { getRequestDispatcher, RequestInstance } from "@hyper-fetch/core";
|
|
3
|
+
import { useRef } from "react";
|
|
4
4
|
|
|
5
5
|
import { UseCacheOptionsType, useCacheDefaultOptions, UseCacheReturnType } from "hooks/use-cache";
|
|
6
6
|
import { useRequestEvents, useTrackedState } from "helpers";
|
|
7
|
-
import {
|
|
7
|
+
import { useProvider } from "provider";
|
|
8
8
|
|
|
9
9
|
export const useCache = <T extends RequestInstance>(
|
|
10
10
|
request: T,
|
|
11
|
-
options
|
|
11
|
+
options?: UseCacheOptionsType<T>,
|
|
12
12
|
): UseCacheReturnType<T> => {
|
|
13
13
|
const { cacheKey, client } = request;
|
|
14
14
|
|
|
15
15
|
const { cache, loggerManager } = client;
|
|
16
|
-
const logger = useRef(loggerManager.
|
|
16
|
+
const logger = useRef(loggerManager.initialize(client, "useCache")).current;
|
|
17
17
|
const [dispatcher] = getRequestDispatcher(request);
|
|
18
18
|
const updateKey = JSON.stringify(request.toJSON());
|
|
19
19
|
|
|
20
20
|
// Build the configuration options
|
|
21
|
-
const
|
|
22
|
-
const { dependencyTracking,
|
|
21
|
+
const { config: globalConfig } = useProvider();
|
|
22
|
+
const { dependencyTracking, initialResponse, deepCompare } = {
|
|
23
23
|
...useCacheDefaultOptions,
|
|
24
24
|
...globalConfig.useCacheConfig,
|
|
25
25
|
...options,
|
|
@@ -32,7 +32,7 @@ export const useCache = <T extends RequestInstance>(
|
|
|
32
32
|
logger,
|
|
33
33
|
request,
|
|
34
34
|
dispatcher,
|
|
35
|
-
|
|
35
|
+
initialResponse,
|
|
36
36
|
deepCompare,
|
|
37
37
|
dependencyTracking,
|
|
38
38
|
});
|
|
@@ -40,7 +40,7 @@ export const useCache = <T extends RequestInstance>(
|
|
|
40
40
|
/**
|
|
41
41
|
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
42
42
|
*/
|
|
43
|
-
const [
|
|
43
|
+
const [, listeners] = useRequestEvents({
|
|
44
44
|
logger,
|
|
45
45
|
actions,
|
|
46
46
|
request,
|
|
@@ -52,20 +52,24 @@ export const useCache = <T extends RequestInstance>(
|
|
|
52
52
|
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
53
53
|
|
|
54
54
|
const handleMountEvents = () => {
|
|
55
|
-
addCacheDataListener(request);
|
|
56
|
-
addLifecycleListeners(request);
|
|
55
|
+
const unmountDataListener = addCacheDataListener(request);
|
|
56
|
+
const unmountLifecycleListener = addLifecycleListeners(request);
|
|
57
|
+
return () => {
|
|
58
|
+
unmountDataListener();
|
|
59
|
+
unmountLifecycleListener();
|
|
60
|
+
};
|
|
57
61
|
};
|
|
58
62
|
|
|
59
|
-
useDidUpdate(
|
|
63
|
+
useDidUpdate(
|
|
64
|
+
() => {
|
|
65
|
+
return handleMountEvents();
|
|
66
|
+
},
|
|
67
|
+
[updateKey],
|
|
68
|
+
true,
|
|
69
|
+
);
|
|
60
70
|
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
cache.invalidate(getRequestKey(invalidateKey, true));
|
|
64
|
-
} else if (invalidateKey) {
|
|
65
|
-
cache.invalidate(invalidateKey);
|
|
66
|
-
} else {
|
|
67
|
-
cache.invalidate(cacheKey);
|
|
68
|
-
}
|
|
71
|
+
const invalidate = (cacheKeys?: string | RegExp | RequestInstance | Array<string | RegExp | RequestInstance>) => {
|
|
72
|
+
cache.invalidate(cacheKeys ?? cacheKey);
|
|
69
73
|
};
|
|
70
74
|
|
|
71
75
|
return {
|
|
@@ -97,14 +101,15 @@ export const useCache = <T extends RequestInstance>(
|
|
|
97
101
|
setRenderKey("retries");
|
|
98
102
|
return state.retries;
|
|
99
103
|
},
|
|
100
|
-
get
|
|
101
|
-
setRenderKey("
|
|
102
|
-
return state.
|
|
104
|
+
get responseTimestamp() {
|
|
105
|
+
setRenderKey("responseTimestamp");
|
|
106
|
+
return state.responseTimestamp;
|
|
107
|
+
},
|
|
108
|
+
get requestTimestamp() {
|
|
109
|
+
setRenderKey("requestTimestamp");
|
|
110
|
+
return state.requestTimestamp;
|
|
103
111
|
},
|
|
104
|
-
onCacheError: callbacks.onError,
|
|
105
|
-
onCacheSuccess: callbacks.onSuccess,
|
|
106
|
-
onCacheChange: callbacks.onFinished,
|
|
107
112
|
...actions,
|
|
108
|
-
|
|
113
|
+
invalidate,
|
|
109
114
|
};
|
|
110
115
|
};
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { RequestInstance, ExtractResponseType, ExtractErrorType, CacheValueType } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { isEqual } from "utils";
|
|
4
|
-
import {
|
|
5
|
-
OnErrorCallbackType,
|
|
6
|
-
OnFinishedCallbackType,
|
|
7
|
-
OnSuccessCallbackType,
|
|
8
|
-
UseTrackedStateActions,
|
|
9
|
-
UseTrackedStateType,
|
|
10
|
-
} from "helpers";
|
|
4
|
+
import { UseTrackedStateActions, UseTrackedStateType } from "helpers";
|
|
11
5
|
|
|
12
6
|
export type UseCacheOptionsType<T extends RequestInstance> = {
|
|
13
7
|
/**
|
|
@@ -17,7 +11,7 @@ export type UseCacheOptionsType<T extends RequestInstance> = {
|
|
|
17
11
|
/**
|
|
18
12
|
* If cache is empty we can use placeholder data.
|
|
19
13
|
*/
|
|
20
|
-
|
|
14
|
+
initialResponse?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
|
|
21
15
|
/**
|
|
22
16
|
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
|
|
23
17
|
*/
|
|
@@ -27,19 +21,7 @@ export type UseCacheOptionsType<T extends RequestInstance> = {
|
|
|
27
21
|
export type UseCacheReturnType<T extends RequestInstance> = UseTrackedStateType<T> &
|
|
28
22
|
UseTrackedStateActions<T> & {
|
|
29
23
|
/**
|
|
30
|
-
*
|
|
24
|
+
* Invalidate cache for the current request or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
31
25
|
*/
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Helper hook listener for error response
|
|
35
|
-
*/
|
|
36
|
-
onCacheError: (callback: OnErrorCallbackType<T>) => void;
|
|
37
|
-
/**
|
|
38
|
-
* Helper hook listener for response
|
|
39
|
-
*/
|
|
40
|
-
onCacheChange: (callback: OnFinishedCallbackType<T>) => void;
|
|
41
|
-
/**
|
|
42
|
-
* Refetch current request resource or pass custom key to trigger it by invalidationKey(Regex / cacheKey).
|
|
43
|
-
*/
|
|
44
|
-
refetch: (invalidateKey?: string | RegExp | RequestInstance) => void;
|
|
26
|
+
invalidate: (cacheKeys?: string | RegExp | RequestInstance | Array<string | RegExp | RequestInstance>) => void;
|
|
45
27
|
};
|
|
@@ -1,30 +1,66 @@
|
|
|
1
|
+
import { EmitterInstance, EmitterCallbackErrorType, EmitType } from "@hyper-fetch/sockets";
|
|
2
|
+
import { useDidUpdate } from "@better-hooks/lifecycle";
|
|
1
3
|
import { useRef } from "react";
|
|
2
|
-
import { EmitterInstance } from "@hyper-fetch/sockets";
|
|
3
4
|
|
|
4
5
|
import { UseEmitterOptionsType } from "hooks/use-emitter";
|
|
5
6
|
import { useSocketState } from "helpers";
|
|
6
|
-
import {
|
|
7
|
+
import { useProvider } from "provider";
|
|
7
8
|
|
|
8
9
|
export const useEmitter = <EmitterType extends EmitterInstance>(
|
|
9
10
|
emitter: EmitterType,
|
|
10
|
-
options
|
|
11
|
+
options?: UseEmitterOptionsType,
|
|
11
12
|
) => {
|
|
12
|
-
const
|
|
13
|
+
const { config: globalConfig } = useProvider();
|
|
13
14
|
const { dependencyTracking } = { ...globalConfig.useEmitter, ...options };
|
|
14
15
|
|
|
15
|
-
const onEventCallback = useRef<null | ((emitter: EmitterType) => void)>(null);
|
|
16
16
|
const [state, actions, callbacks, { setRenderKey }] = useSocketState(emitter.socket, { dependencyTracking });
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Callbacks
|
|
20
|
+
*/
|
|
21
|
+
const onEventStartCallback = useRef<null | ((emitter: EmitterType) => void)>(null);
|
|
22
|
+
const onEventErrorCallback = useRef<null | EmitterCallbackErrorType>(null);
|
|
23
|
+
|
|
24
|
+
useDidUpdate(
|
|
25
|
+
() => {
|
|
26
|
+
const onEventStart = () => {
|
|
27
|
+
return emitter.socket.events.onEmitterStartEventByTopic(emitter, () => {
|
|
28
|
+
onEventStartCallback.current?.(emitter);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const onEventError = () => {
|
|
33
|
+
return emitter.socket.events.onEmitterErrorByTopic(emitter, ({ error }) => {
|
|
34
|
+
onEventErrorCallback.current?.({ error });
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const unmountEventStart = onEventStart();
|
|
39
|
+
const unmountEventError = onEventError();
|
|
40
|
+
|
|
41
|
+
return () => {
|
|
42
|
+
unmountEventStart();
|
|
43
|
+
unmountEventError();
|
|
44
|
+
};
|
|
21
45
|
},
|
|
46
|
+
[emitter.topic, onEventStartCallback.current, onEventErrorCallback.current],
|
|
47
|
+
true,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Emitter
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
const emit: EmitType<EmitterType> = (emitOptions: Parameters<EmitType<EmitterType>>[0]) => {
|
|
55
|
+
return emitter.emit(emitOptions as any);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const onEmit = (callback: (emitter: EmitterType) => void) => {
|
|
59
|
+
onEventStartCallback.current = callback;
|
|
22
60
|
};
|
|
23
61
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
onEventCallback.current?.(emitter);
|
|
27
|
-
return emitter.emit(emitOptions);
|
|
62
|
+
const onEmitError = (callback: EmitterCallbackErrorType) => {
|
|
63
|
+
onEventErrorCallback.current = callback;
|
|
28
64
|
};
|
|
29
65
|
|
|
30
66
|
return {
|
|
@@ -36,14 +72,10 @@ export const useEmitter = <EmitterType extends EmitterInstance>(
|
|
|
36
72
|
setRenderKey("connecting");
|
|
37
73
|
return state.connecting;
|
|
38
74
|
},
|
|
39
|
-
get timestamp() {
|
|
40
|
-
setRenderKey("timestamp");
|
|
41
|
-
return state.timestamp;
|
|
42
|
-
},
|
|
43
75
|
...actions,
|
|
44
76
|
...callbacks,
|
|
45
|
-
|
|
77
|
+
onEmit,
|
|
78
|
+
onEmitError,
|
|
46
79
|
emit,
|
|
47
|
-
reconnect: emitter.socket.reconnect,
|
|
48
80
|
};
|
|
49
81
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Time, RequestInstance, RequiredKeys } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { UseFetchOptionsType } from "hooks/use-fetch";
|
|
4
4
|
|
|
5
|
-
type DefaultOptionsType = RequiredKeys<Omit<UseFetchOptionsType<RequestInstance>, "
|
|
6
|
-
|
|
5
|
+
type DefaultOptionsType = RequiredKeys<Omit<UseFetchOptionsType<RequestInstance>, "initialResponse">> & {
|
|
6
|
+
initialResponse: null;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
export const useFetchDefaultOptions: DefaultOptionsType = {
|
|
@@ -11,9 +11,9 @@ export const useFetchDefaultOptions: DefaultOptionsType = {
|
|
|
11
11
|
disabled: false,
|
|
12
12
|
dependencyTracking: true,
|
|
13
13
|
revalidate: true,
|
|
14
|
-
|
|
14
|
+
initialResponse: null,
|
|
15
15
|
refresh: false,
|
|
16
|
-
refreshTime:
|
|
16
|
+
refreshTime: Time.HOUR,
|
|
17
17
|
refetchBlurred: true,
|
|
18
18
|
refetchOnBlur: false,
|
|
19
19
|
refetchOnFocus: false,
|
|
@@ -21,5 +21,6 @@ export const useFetchDefaultOptions: DefaultOptionsType = {
|
|
|
21
21
|
bounce: false,
|
|
22
22
|
bounceType: "debounce",
|
|
23
23
|
bounceTime: 400,
|
|
24
|
+
bounceTimeout: 400,
|
|
24
25
|
deepCompare: true,
|
|
25
26
|
};
|