Skip to content

Commit 030a55f

Browse files
refactor(router-core): router updateMatch can skip matches if it found a match (#4828)
`updateMatch` can trigger some heavy synchronous work (it updates the store, which in turn, will re-run a bunch of things), so it's nice if itself can avoid doing unnecessary work. - it doesn't need to return anything, the return value is not used - it doesn't need to search through the 3 arrays of matches if it found a result in the 1st or 2nd. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 40e4657 commit 030a55f

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

packages/router-core/src/router.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ export type GetMatchFn = (matchId: string) => AnyRouteMatch | undefined
661661
export type UpdateMatchFn = (
662662
id: string,
663663
updater: (match: AnyRouteMatch) => AnyRouteMatch,
664-
) => AnyRouteMatch
664+
) => void
665665

666666
export type LoadRouteChunkFn = (route: AnyRoute) => Promise<Array<void>>
667667

@@ -2052,29 +2052,20 @@ export class RouterCore<
20522052
}
20532053

20542054
updateMatch: UpdateMatchFn = (id, updater) => {
2055-
let updated!: AnyRouteMatch
2056-
const isPending = this.state.pendingMatches?.find((d) => d.id === id)
2057-
const isMatched = this.state.matches.find((d) => d.id === id)
2058-
const isCached = this.state.cachedMatches.find((d) => d.id === id)
2059-
2060-
const matchesKey = isPending
2055+
const matchesKey = this.state.pendingMatches?.some((d) => d.id === id)
20612056
? 'pendingMatches'
2062-
: isMatched
2057+
: this.state.matches.some((d) => d.id === id)
20632058
? 'matches'
2064-
: isCached
2059+
: this.state.cachedMatches.some((d) => d.id === id)
20652060
? 'cachedMatches'
20662061
: ''
20672062

20682063
if (matchesKey) {
20692064
this.__store.setState((s) => ({
20702065
...s,
2071-
[matchesKey]: s[matchesKey]?.map((d) =>
2072-
d.id === id ? (updated = updater(d)) : d,
2073-
),
2066+
[matchesKey]: s[matchesKey]?.map((d) => (d.id === id ? updater(d) : d)),
20742067
}))
20752068
}
2076-
2077-
return updated
20782069
}
20792070

20802071
getMatch: GetMatchFn = (matchId: string) => {

0 commit comments

Comments
 (0)