Skip to content

Commit 7fd19aa

Browse files
authored
Merge pull request #160 from danybeltran/feat-perf-revalidation
feat(performance):
2 parents ed5ac01 + 4a8204a commit 7fd19aa

File tree

3 files changed

+75
-91
lines changed

3 files changed

+75
-91
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.0.2",
3+
"version": "3.0.4",
44
"description": "React hooks for data fetching",
55
"main": "dist/index.js",
66
"scripts": {

src/hooks/others.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,17 @@ export function useFetchCode(id: any) {
126126
* Get the loading state of a request using its id
127127
*/
128128
export function useFetchLoading(id: any): boolean {
129-
const idString = serialize({ idString: serialize(id) })
130-
131-
const { loading } = useFetch({
132-
id: id
129+
const { loading, ...all } = useFetch({
130+
id
133131
})
134-
135-
return !isDefined(runningRequests[idString]) ? true : isPending(idString)
132+
return (
133+
loading &&
134+
isPending(
135+
serialize({
136+
idString: serialize(id)
137+
})
138+
)
139+
)
136140
}
137141

138142
/**
@@ -159,11 +163,11 @@ export function useFetchError(id: any, onError?: (err?: any) => void) {
159163
}
160164
}, [resolvedKey])
161165

162-
const { error } = useFetch({
163-
id: id
164-
})
165-
166-
return error
166+
return (
167+
useFetch({
168+
id: id
169+
}).error || hasErrors[resolvedKey]
170+
)
167171
}
168172

169173
/**

src/hooks/use-fetch.ts

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
322322
loading: auto
323323
? isPending(resolvedKey) ||
324324
(revalidateOnMount
325-
? suspense
326-
? isPending(resolvedKey)
327-
: true
325+
? previousConfig[resolvedKey] !== serialize(optionsConfig)
328326
: previousConfig[resolvedKey] !== serialize(optionsConfig))
329327
: false,
330328
error: (hasErrors[resolvedDataKey] || false) as boolean,
@@ -337,7 +335,11 @@ export function useFetch<FetchDataType = any, BodyType = any>(
337335
loading: false,
338336
error: false,
339337
completedAttempts: false
340-
})
338+
}).current
339+
340+
const inDeps = (k: keyof typeof thisDeps) => {
341+
return thisDeps[k]
342+
}
341343

342344
const { data, loading, online, error, completedAttempts } = fetchState
343345

@@ -833,32 +835,10 @@ export function useFetch<FetchDataType = any, BodyType = any>(
833835
}
834836
}
835837
} finally {
836-
setFetchState(p => {
837-
const n = {
838-
...p,
839-
data: thisDeps.current.data ? $$data ?? p.data : p.data,
840-
online: thisDeps.current.online ? p.online : p.online,
841-
loading: thisDeps.current.loading
842-
? rpc?.loading ?? false
843-
: p.loading,
844-
error: thisDeps.current.error
845-
? isDefined($$error)
846-
? $$error
847-
: p.error
848-
: p.error,
849-
completedAttempts: thisDeps.current.completedAttempts
850-
? $$completedAttempts ?? p.completedAttempts
851-
: p.completedAttempts
852-
}
853-
if (jsonCompare(n, p)) return p
854-
return n
855-
})
856-
857838
runningRequests[resolvedKey] = false
858839
suspenseInitialized[resolvedKey] = true
859840

860841
requestsProvider.emit(resolvedKey, {
861-
requestCallId,
862842
error:
863843
hasErrors[resolvedKey] || hasErrors[resolvedDataKey] || false,
864844
...rpc,
@@ -875,7 +855,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
875855
}
876856
},
877857
[
878-
thisDeps.current,
858+
thisDeps,
879859
canRevalidate,
880860
ctx.auto,
881861
stringDeps,
@@ -960,28 +940,21 @@ export function useFetch<FetchDataType = any, BodyType = any>(
960940
if (v.requestCallId !== requestCallId) {
961941
if (!willSuspend[resolvedKey]) {
962942
queue(() => {
963-
setFetchState(p => {
964-
const n = {
965-
...p,
966-
data: thisDeps.current.data
967-
? !jsonCompare($data, p.data)
968-
? $data
969-
: p.data ?? p.data
970-
: p.data,
971-
online: thisDeps.current.online ? online ?? p.online : p.online,
972-
loading: thisDeps.current.loading
973-
? loading ?? p.loading
974-
: p.loading,
975-
error: thisDeps.current.error ? Boolean($error) : p.error,
976-
completedAttempts: thisDeps.current.completedAttempts
977-
? completedAttempts ?? p.completedAttempts
978-
: p.completedAttempts
979-
}
980-
981-
if (jsonCompare(n, p)) return p
982-
983-
return n
984-
})
943+
if (inDeps('data')) {
944+
setData($data)
945+
}
946+
if (inDeps('online')) {
947+
setOnline(online)
948+
}
949+
if (inDeps('loading')) {
950+
setLoading(loading)
951+
}
952+
if (inDeps('error')) {
953+
setError($error)
954+
}
955+
if (inDeps('completedAttempts')) {
956+
setCompletedAttempts(completedAttempts)
957+
}
985958
})
986959
}
987960
}
@@ -993,7 +966,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
993966
requestsProvider.removeListener(resolvedKey, waitFormUpdates)
994967
}
995968
}, [
996-
thisDeps.current,
969+
thisDeps,
997970
JSON.stringify(optionsConfig),
998971
resolvedKey,
999972
resolvedDataKey,
@@ -1172,7 +1145,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
11721145
online: false,
11731146
error: true
11741147
})
1175-
if (thisDeps.current.online) setOnline(false)
1148+
if (inDeps('online')) setOnline(false)
11761149
}
11771150
}
11781151
}, getMiliseconds(attemptInterval as TimeSpan))
@@ -1225,22 +1198,25 @@ export function useFetch<FetchDataType = any, BodyType = any>(
12251198
}
12261199
return d
12271200
},
1228-
[serialize(serialize(optionsConfig)), fetchState]
1201+
[serialize(serialize(optionsConfig)), fetchState, thisDeps]
12291202
)
12301203

12311204
if (!suspense) {
12321205
if (url !== '') {
12331206
suspenseInitialized[resolvedKey] = true
12341207
}
12351208
}
1236-
useEffect(() => {
1209+
1210+
React.useLayoutEffect(() => {
12371211
if (url !== '') {
12381212
if (!jsonCompare(previousProps[resolvedKey], optionsConfig)) {
12391213
abortControllers[resolvedKey]?.abort()
1240-
queue(initializeRevalidation)
1214+
if (inDeps('data')) {
1215+
queue(initializeRevalidation)
1216+
}
12411217
}
12421218
}
1243-
}, [serialize(optionsConfig)])
1219+
}, [serialize(optionsConfig), thisDeps])
12441220

12451221
if (suspense) {
12461222
if (auto) {
@@ -1260,30 +1236,34 @@ export function useFetch<FetchDataType = any, BodyType = any>(
12601236
}
12611237
}
12621238

1263-
React.useMemo(() => {
1239+
React.useLayoutEffect(() => {
12641240
if (!runningRequests[resolvedKey] && isExpired) {
12651241
if (windowExists) {
12661242
if (canRevalidate && url !== '') {
12671243
if (!jsonCompare(previousConfig[resolvedKey], optionsConfig)) {
12681244
if (!isPending(resolvedKey)) {
1269-
initializeRevalidation()
1245+
if (inDeps('data')) {
1246+
initializeRevalidation()
1247+
}
12701248
} else {
12711249
setLoading(true)
12721250
}
12731251
}
12741252
}
12751253
}
12761254
}
1277-
}, [resolvedKey, serialize(optionsConfig), canRevalidate])
1255+
}, [resolvedKey, serialize(optionsConfig), canRevalidate, thisDeps])
12781256

1279-
useEffect(() => {
1257+
React.useLayoutEffect(() => {
12801258
const revalidateAfterUnmount = revalidateOnMount
12811259
? true
12821260
: previousConfig[resolvedKey] !== serialize(optionsConfig)
12831261

12841262
function revalidate() {
12851263
if (!debounce && !canDebounce[resolvedKey]) {
1286-
initializeRevalidation()
1264+
if (inDeps('data')) {
1265+
initializeRevalidation()
1266+
}
12871267
}
12881268
}
12891269

@@ -1298,7 +1278,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
12981278
}
12991279

13001280
// eslint-disable-next-line react-hooks/exhaustive-deps
1301-
}, [serialize(optionsConfig)])
1281+
}, [serialize(optionsConfig), thisDeps])
13021282

13031283
useEffect(() => {
13041284
function addFocusListener() {
@@ -1411,7 +1391,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
14111391
? new Date(cacheProvider.get('expiration' + resolvedDataKey))
14121392
: null
14131393

1414-
const isFailed = hasErrors[resolvedDataKey] || error
1394+
const isFailed = hasErrors[resolvedDataKey] || hasErrors[resolvedKey] || error
14151395

14161396
const responseData =
14171397
(error && isFailed ? (cacheIfError ? thisCache : null) : thisCache) ?? def
@@ -1426,64 +1406,64 @@ export function useFetch<FetchDataType = any, BodyType = any>(
14261406

14271407
return {
14281408
get revalidating() {
1429-
thisDeps.current.loading = true
1409+
thisDeps.loading = true
14301410
return oneRequestResolved && isLoading
14311411
},
14321412
get hasData() {
1433-
thisDeps.current.data = true
1413+
thisDeps.data = true
14341414
return oneRequestResolved
14351415
},
14361416
get success() {
1437-
thisDeps.current.loading = true
1438-
thisDeps.current.error = true
1417+
thisDeps.loading = true
1418+
thisDeps.error = true
14391419
return isSuccess
14401420
},
14411421
get loadingFirst() {
1442-
thisDeps.current.loading = true
1422+
thisDeps.loading = true
14431423
return loadingFirst
14441424
},
14451425
get requestStart() {
1446-
thisDeps.current.loading = true
1426+
thisDeps.loading = true
14471427
return getDateIfValid($requestStart)
14481428
},
14491429
get requestEnd() {
1450-
thisDeps.current.loading = true
1430+
thisDeps.loading = true
14511431
return getDateIfValid($requestEnd)
14521432
},
14531433
get expiration() {
1454-
thisDeps.current.loading = true
1434+
thisDeps.loading = true
14551435
return getDateIfValid(isFailed ? null : expirationDate)
14561436
},
14571437
get responseTime() {
1458-
thisDeps.current.loading = true
1438+
thisDeps.loading = true
14591439
return requestResponseTimes[resolvedDataKey] ?? null
14601440
},
14611441
get data() {
1462-
thisDeps.current.data = true
1442+
thisDeps.data = true
14631443
return responseData
14641444
},
14651445
get loading() {
1466-
thisDeps.current.loading = true
1446+
thisDeps.loading = true
14671447
return isLoading
14681448
},
14691449
get error() {
1470-
thisDeps.current.error = true
1450+
thisDeps.error = true
14711451
return isFailed || false
14721452
},
14731453
get online() {
1474-
thisDeps.current.online = true
1454+
thisDeps.online = true
14751455
return online
14761456
},
14771457
get code() {
1478-
thisDeps.current.loading = true
1458+
thisDeps.loading = true
14791459
return statusCodes[resolvedKey]
14801460
},
14811461
get reFetch() {
1482-
thisDeps.current.loading = true
1462+
thisDeps.loading = true
14831463
return reValidate
14841464
},
14851465
get mutate() {
1486-
thisDeps.current.data = true
1466+
thisDeps.data = true
14871467
return forceMutate
14881468
},
14891469
get fetcher() {
@@ -1508,7 +1488,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
15081488
},
15091489
config: __config,
15101490
get response() {
1511-
thisDeps.current.loading = true
1491+
thisDeps.loading = true
15121492
return lastResponses[resolvedKey]
15131493
},
15141494
id,

0 commit comments

Comments
 (0)