Skip to content

Commit 1c8cc9b

Browse files
authored
Merge pull request #195 from atomic-state/enhancemennts/caching
enh(caching):
2 parents 1b37db6 + 0814ccd commit 1c8cc9b

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-react",
3-
"version": "3.6.6",
3+
"version": "3.6.7",
44
"description": "React hooks for data fetching",
55
"main": "dist/index.js",
66
"scripts": {

src/hooks/use-fetch.ts

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,14 +1333,22 @@ export function useFetch<FetchDataType = any, BodyType = any>(
13331333
}
13341334

13351335
useIsomorphicLayoutEffect(() => {
1336-
if (url !== '') {
1337-
if (!jsonCompare(previousProps.get(resolvedKey), optionsConfig)) {
1338-
abortControllers.get(resolvedKey)?.abort()
1339-
if (inDeps('data')) {
1340-
queue(initializeRevalidation)
1336+
const fn = () => {
1337+
if (url !== '') {
1338+
if (!jsonCompare(previousProps.get(resolvedKey), optionsConfig)) {
1339+
abortControllers.get(resolvedKey)?.abort()
1340+
if (inDeps('data')) {
1341+
queue(initializeRevalidation)
1342+
}
13411343
}
13421344
}
13431345
}
1346+
if (debounce) {
1347+
const tm = setTimeout(fn, debounce)
1348+
return () => clearTimeout(tm)
1349+
}
1350+
fn()
1351+
return () => {}
13441352
}, [serialize(optionsConfig), thisDeps, fetchState])
13451353

13461354
if (suspense) {
@@ -1365,26 +1373,36 @@ export function useFetch<FetchDataType = any, BodyType = any>(
13651373
}
13661374

13671375
useIsomorphicLayoutEffect(() => {
1368-
if (!runningRequests.get(resolvedKey) && isExpired) {
1369-
if (windowExists) {
1370-
if (canRevalidate && url !== '') {
1371-
if (
1372-
!jsonCompare(
1373-
JSON.parse(previousConfig.get(resolvedKey) || '{}'),
1374-
optionsConfig
1375-
)
1376-
) {
1377-
if (!isPending(resolvedKey)) {
1378-
if (inDeps('data')) {
1379-
initializeRevalidation()
1376+
const fn = () => {
1377+
if (!runningRequests.get(resolvedKey) && canRevalidate) {
1378+
if (windowExists) {
1379+
if (canRevalidate && url !== '') {
1380+
if (
1381+
!jsonCompare(
1382+
JSON.parse(previousConfig.get(resolvedKey) || '{}'),
1383+
optionsConfig
1384+
)
1385+
) {
1386+
if (!isPending(resolvedKey)) {
1387+
if (inDeps('data')) {
1388+
initializeRevalidation()
1389+
}
1390+
} else {
1391+
setLoading(true)
13801392
}
1381-
} else {
1382-
setLoading(true)
13831393
}
13841394
}
13851395
}
13861396
}
13871397
}
1398+
1399+
if (debounce) {
1400+
const tm = setTimeout(fn, debounce)
1401+
return () => clearTimeout(tm)
1402+
}
1403+
fn()
1404+
1405+
return () => {}
13881406
}, [resolvedKey, serialize(optionsConfig), canRevalidate, thisDeps])
13891407

13901408
useIsomorphicLayoutEffect(() => {
@@ -1403,16 +1421,26 @@ export function useFetch<FetchDataType = any, BodyType = any>(
14031421
}
14041422
}
14051423

1406-
if (revalidateAfterUnmount) {
1407-
if (suspense) {
1408-
if (suspenseInitialized.get(resolvedKey)) {
1424+
const fn = () => {
1425+
if (revalidateAfterUnmount) {
1426+
if (suspense) {
1427+
if (suspenseInitialized.get(resolvedKey)) {
1428+
revalidate()
1429+
}
1430+
} else {
14091431
revalidate()
14101432
}
1411-
} else {
1412-
revalidate()
14131433
}
14141434
}
14151435

1436+
if (debounce) {
1437+
const tm = setTimeout(fn, debounce)
1438+
return () => clearTimeout(tm)
1439+
}
1440+
fn()
1441+
1442+
return () => {}
1443+
14161444
// eslint-disable-next-line react-hooks/exhaustive-deps
14171445
}, [serialize(optionsConfig), thisDeps])
14181446

src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ export type FetchConfigType<FetchDataType = any, BodyType = any> = Omit<
330330
formatBody?: boolean | ((b: BodyType) => any)
331331
/**
332332
* The time to wait before revalidation after props change
333-
* @deprecated Use the `useDebounceFetch` hook instead
334333
*/
335334
debounce?: TimeSpan
336335
/**

0 commit comments

Comments
 (0)