@hyper-fetch/react 5.0.0 → 5.0.2
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/dist/index.cjs.js +59 -35
- package/dist/index.cjs.js.map +2 -2
- package/dist/index.d.ts +7 -7
- package/dist/index.esm.js +62 -38
- package/dist/index.esm.js.map +3 -3
- package/package.json +2 -3
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +3 -2
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +8 -4
- package/src/hooks/use-fetch/use-fetch.hooks.ts +33 -10
- package/src/hooks/use-fetch/use-fetch.types.ts +2 -2
- package/src/hooks/use-submit/use-submit.types.ts +3 -9
- package/src/utils/deep-equal.utils.ts +39 -32
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _hyper_fetch_core from '@hyper-fetch/core';
|
|
2
|
-
import { RequestInstance, Dispatcher, LoggerType, CacheValueType, ExtractResponseType, ExtractErrorType, ResponseDetailsType, ExtractAdapterReturnType, RequestEventType, ProgressType,
|
|
2
|
+
import { RequestInstance, Dispatcher, LoggerType, CacheValueType, ExtractResponseType, ExtractErrorType, ResponseDetailsType, ExtractAdapterReturnType, RequestEventType, ProgressType, NullableType, ExtractAdapterStatusType, ExtractAdapterType, ExtractAdapterExtraType, RequiredKeys, RequestSendType, QueueElementType, ClientInstance } from '@hyper-fetch/core';
|
|
3
3
|
import * as _hyper_fetch_sockets from '@hyper-fetch/sockets';
|
|
4
4
|
import { SocketInstance, ListenerInstance, ExtractListenerResponseType, ExtractSocketExtraType, ExtractListenerAdapterType, EmitterInstance } from '@hyper-fetch/sockets';
|
|
5
5
|
import React from 'react';
|
|
@@ -124,7 +124,7 @@ declare const getBounceData: (bounceData: {
|
|
|
124
124
|
type UseTrackedStateProps<T extends RequestInstance> = {
|
|
125
125
|
request: T;
|
|
126
126
|
logger: LoggerType;
|
|
127
|
-
initialData:
|
|
127
|
+
initialData: NullableType<Partial<ExtractAdapterReturnType<T>>>;
|
|
128
128
|
dispatcher: Dispatcher;
|
|
129
129
|
dependencyTracking: boolean;
|
|
130
130
|
defaultCacheEmitting?: boolean;
|
|
@@ -222,9 +222,9 @@ declare const useTrackedState: <T extends RequestInstance>({ request, dispatcher
|
|
|
222
222
|
|
|
223
223
|
declare const getDetailsState: (state?: UseTrackedStateType<RequestInstance>, details?: Partial<ResponseDetailsType>) => ResponseDetailsType;
|
|
224
224
|
declare const isStaleCacheData: (cacheTime: number, cacheTimestamp: NullableType<Date | number>) => boolean;
|
|
225
|
-
declare const getValidCacheData: <T extends RequestInstance>(request: T, initialData: ExtractAdapterReturnType<T
|
|
225
|
+
declare const getValidCacheData: <T extends RequestInstance>(request: T, initialData: Partial<ExtractAdapterReturnType<T>>, cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, _hyper_fetch_core.AdapterInstance>) => CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, _hyper_fetch_core.AdapterInstance>;
|
|
226
226
|
declare const getTimestamp: (timestamp?: NullableType<number | Date>) => Date;
|
|
227
|
-
declare const getInitialState: <T extends RequestInstance>(initialData:
|
|
227
|
+
declare const getInitialState: <T extends RequestInstance>(initialData: Partial<ExtractAdapterReturnType<T>>, dispatcher: Dispatcher, request: T) => UseTrackedStateType<T>;
|
|
228
228
|
|
|
229
229
|
declare const useSocketState: <DataType>(socket: SocketInstance, { dependencyTracking }: UseSocketStateProps) => readonly [UseSocketStateType<DataType>, {
|
|
230
230
|
setData: (data: DataType) => void;
|
|
@@ -276,7 +276,7 @@ type UseFetchOptionsType<T extends RequestInstance> = {
|
|
|
276
276
|
/**
|
|
277
277
|
* If cache is empty we can use placeholder data.
|
|
278
278
|
*/
|
|
279
|
-
initialData?:
|
|
279
|
+
initialData?: NullableType<Partial<ExtractAdapterReturnType<T>>>;
|
|
280
280
|
/**
|
|
281
281
|
* Enable/disable refresh data
|
|
282
282
|
*/
|
|
@@ -369,13 +369,13 @@ declare const useSubmit: <RequestType extends RequestInstance>(request: RequestT
|
|
|
369
369
|
|
|
370
370
|
type UseSubmitOptionsType<T extends RequestInstance> = {
|
|
371
371
|
/**
|
|
372
|
-
* Disable
|
|
372
|
+
* Disable submitting
|
|
373
373
|
*/
|
|
374
374
|
disabled?: boolean;
|
|
375
375
|
/**
|
|
376
376
|
* If cache is empty we can use placeholder data.
|
|
377
377
|
*/
|
|
378
|
-
initialData?:
|
|
378
|
+
initialData?: NullableType<Partial<ExtractAdapterReturnType<T>>>;
|
|
379
379
|
/**
|
|
380
380
|
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
|
|
381
381
|
*/
|
package/dist/index.esm.js
CHANGED
|
@@ -40,7 +40,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
40
40
|
|
|
41
41
|
// src/hooks/use-fetch/use-fetch.hooks.ts
|
|
42
42
|
import { useRef as useRef4 } from "react";
|
|
43
|
-
import { useDidUpdate as useDidUpdate3, useDidMount as useDidMount2 } from "@better-hooks/lifecycle";
|
|
43
|
+
import { useDidUpdate as useDidUpdate3, useDidMount as useDidMount2, useWillUnmount as useWillUnmount2 } from "@better-hooks/lifecycle";
|
|
44
44
|
import { useDebounce, useThrottle } from "@better-hooks/performance";
|
|
45
45
|
import { Request, getRequestKey } from "@hyper-fetch/core";
|
|
46
46
|
|
|
@@ -269,36 +269,43 @@ var isEmpty = (value) => {
|
|
|
269
269
|
return false;
|
|
270
270
|
};
|
|
271
271
|
var isEqual = (firstValue, secondValue) => {
|
|
272
|
-
|
|
273
|
-
const secondValueType = Object.prototype.toString.call(secondValue);
|
|
274
|
-
const firstType = typeof firstValue;
|
|
275
|
-
const secondType = typeof secondValue;
|
|
276
|
-
const isType = (type) => firstType === type && secondType === type;
|
|
277
|
-
const isTypeValue = (type) => firstValueType === type && secondValueType === type;
|
|
278
|
-
if (firstValueType !== secondValueType)
|
|
279
|
-
return false;
|
|
280
|
-
if (firstValue === null && secondValue === null)
|
|
281
|
-
return true;
|
|
282
|
-
if (isType("number") && Number.isNaN(firstValue) && Number.isNaN(secondValue))
|
|
283
|
-
return true;
|
|
284
|
-
if (isEmpty(firstValue) && isEmpty(secondValue))
|
|
272
|
+
if (firstValue === secondValue)
|
|
285
273
|
return true;
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
274
|
+
try {
|
|
275
|
+
const firstValueType = Object.prototype.toString.call(firstValue);
|
|
276
|
+
const secondValueType = Object.prototype.toString.call(secondValue);
|
|
277
|
+
const firstType = typeof firstValue;
|
|
278
|
+
const secondType = typeof secondValue;
|
|
279
|
+
const isType = (type) => firstType === type && secondType === type;
|
|
280
|
+
const isTypeValue = (type) => firstValueType === type && secondValueType === type;
|
|
281
|
+
if (firstValueType !== secondValueType)
|
|
293
282
|
return false;
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
)
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
283
|
+
if (firstValue === null && secondValue === null)
|
|
284
|
+
return true;
|
|
285
|
+
if (isType("number") && Number.isNaN(firstValue) && Number.isNaN(secondValue))
|
|
286
|
+
return true;
|
|
287
|
+
if (isEmpty(firstValue) && isEmpty(secondValue))
|
|
288
|
+
return true;
|
|
289
|
+
if (Array.isArray(firstValue) && Array.isArray(secondValue)) {
|
|
290
|
+
if (firstValue.length !== secondValue.length)
|
|
291
|
+
return false;
|
|
292
|
+
return !firstValue.some((element, i) => !isEqual(element, secondValue[i]));
|
|
293
|
+
}
|
|
294
|
+
if (isType("object") && isTypeValue("[object Object]")) {
|
|
295
|
+
if (Object.keys(firstValue).length !== Object.keys(secondValue).length)
|
|
296
|
+
return false;
|
|
297
|
+
return !Object.entries(firstValue).some(
|
|
298
|
+
([key, value]) => !isEqual(value, secondValue[key])
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
if (firstValue instanceof Date && secondValue instanceof Date) {
|
|
302
|
+
return +firstValue === +secondValue;
|
|
303
|
+
}
|
|
304
|
+
return firstValue === secondValue;
|
|
305
|
+
} catch (err) {
|
|
306
|
+
console.error(err);
|
|
307
|
+
return false;
|
|
300
308
|
}
|
|
301
|
-
return firstValue === secondValue;
|
|
302
309
|
};
|
|
303
310
|
|
|
304
311
|
// src/utils/bounce.utils.ts
|
|
@@ -326,7 +333,13 @@ var getValidCacheData = (request, initialData, cacheData) => {
|
|
|
326
333
|
return cacheData;
|
|
327
334
|
}
|
|
328
335
|
if (initialData) {
|
|
329
|
-
return __spreadProps(__spreadValues(__spreadValues({
|
|
336
|
+
return __spreadProps(__spreadValues(__spreadValues({
|
|
337
|
+
data: null,
|
|
338
|
+
error: null,
|
|
339
|
+
status: null,
|
|
340
|
+
success: null,
|
|
341
|
+
extra: null
|
|
342
|
+
}, initialData || {}), getDetailsState()), {
|
|
330
343
|
cacheTime: 1e3,
|
|
331
344
|
clearKey: request.client.cache.clearKey,
|
|
332
345
|
garbageCollection: request.garbageCollection
|
|
@@ -723,6 +736,7 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
723
736
|
const refreshDebounce = useDebounce({ delay: refreshTime });
|
|
724
737
|
const { cacheKey, queueKey, client } = request;
|
|
725
738
|
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
739
|
+
const ignoreReact18DoubleRender = useRef4(true);
|
|
726
740
|
const logger = useRef4(loggerManager.init("useFetch")).current;
|
|
727
741
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
728
742
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
@@ -784,19 +798,26 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
784
798
|
handleRefresh();
|
|
785
799
|
}
|
|
786
800
|
};
|
|
801
|
+
const getIsFetchingIdentity = () => {
|
|
802
|
+
return dispatcher.getRunningRequests(queueKey).some((running) => running.request.cacheKey === cacheKey);
|
|
803
|
+
};
|
|
787
804
|
const initialFetchData = () => {
|
|
788
805
|
const hasStaleData = getStaleStatus();
|
|
789
|
-
const isFetching =
|
|
790
|
-
if (fetchOnMount || hasStaleData && !isFetching) {
|
|
806
|
+
const isFetching = getIsFetchingIdentity();
|
|
807
|
+
if ((fetchOnMount || hasStaleData) && !isFetching) {
|
|
791
808
|
handleFetch();
|
|
792
809
|
}
|
|
793
810
|
};
|
|
794
811
|
const updateFetchData = () => {
|
|
795
|
-
if (
|
|
796
|
-
|
|
797
|
-
|
|
812
|
+
if (!ignoreReact18DoubleRender.current) {
|
|
813
|
+
if (bounce) {
|
|
814
|
+
logger.debug(`Bounce request with ${bounceType}`, { queueKey, request });
|
|
815
|
+
bounceFunction(() => handleFetch());
|
|
816
|
+
} else {
|
|
817
|
+
handleFetch();
|
|
818
|
+
}
|
|
798
819
|
} else {
|
|
799
|
-
|
|
820
|
+
ignoreReact18DoubleRender.current = false;
|
|
800
821
|
}
|
|
801
822
|
};
|
|
802
823
|
const handleMountEvents = () => {
|
|
@@ -832,8 +853,11 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
832
853
|
};
|
|
833
854
|
useDidUpdate3(handleMountEvents, [updateKey], true);
|
|
834
855
|
useDidMount2(initialFetchData);
|
|
835
|
-
useDidUpdate3(updateFetchData, [updateKey, disabled, ...dependencies]);
|
|
856
|
+
useDidUpdate3(updateFetchData, [updateKey, disabled, ...dependencies], true);
|
|
836
857
|
useDidUpdate3(handleRefresh, [updateKey, ...dependencies, disabled, refresh, refreshTime], true);
|
|
858
|
+
useWillUnmount2(() => {
|
|
859
|
+
ignoreReact18DoubleRender.current = true;
|
|
860
|
+
});
|
|
837
861
|
return __spreadProps(__spreadValues(__spreadValues({
|
|
838
862
|
get data() {
|
|
839
863
|
setRenderKey("data");
|
|
@@ -1225,7 +1249,7 @@ var useCacheDefaultOptions = {
|
|
|
1225
1249
|
|
|
1226
1250
|
// src/hooks/use-listener/use-listener.hooks.ts
|
|
1227
1251
|
import { useRef as useRef8 } from "react";
|
|
1228
|
-
import { useDidUpdate as useDidUpdate5, useWillUnmount as
|
|
1252
|
+
import { useDidUpdate as useDidUpdate5, useWillUnmount as useWillUnmount3 } from "@better-hooks/lifecycle";
|
|
1229
1253
|
var useListener = (listener, options) => {
|
|
1230
1254
|
const [globalConfig] = useConfigProvider();
|
|
1231
1255
|
const { dependencyTracking } = __spreadValues(__spreadValues({}, globalConfig.useListener), options);
|
|
@@ -1254,7 +1278,7 @@ var useListener = (listener, options) => {
|
|
|
1254
1278
|
[listener],
|
|
1255
1279
|
true
|
|
1256
1280
|
);
|
|
1257
|
-
|
|
1281
|
+
useWillUnmount3(() => {
|
|
1258
1282
|
stopListener();
|
|
1259
1283
|
});
|
|
1260
1284
|
const additionalCallbacks = {
|