Skip to content

Commit f1441b9

Browse files
committed
refactor: allow direct router store values
Match the atom setter contract by accepting direct values in RouterWritableStore and replace callback setters that do not depend on the previous value.
1 parent 616d4c9 commit f1441b9

6 files changed

Lines changed: 30 additions & 32 deletions

File tree

‎packages/react-router/src/Transitioner.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export function Transitioner() {
128128
})
129129

130130
batch(() => {
131-
router.stores.status.set(() => 'idle')
132-
router.stores.resolvedLocation.set(() => router.stores.location.get())
131+
router.stores.status.set('idle')
132+
router.stores.resolvedLocation.set(router.stores.location.get())
133133
})
134134

135135
if (changeInfo.hrefChanged) {

‎packages/router-core/src/router.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ export class RouterCore<
11691169
}
11701170

11711171
if (needsLocationUpdate && this.stores) {
1172-
this.stores.location.set(() => this.latestLocation)
1172+
this.stores.location.set(this.latestLocation)
11731173
}
11741174

11751175
if (
@@ -2362,10 +2362,10 @@ export class RouterCore<
23622362

23632363
// Ingest the new matches
23642364
this.batch(() => {
2365-
this.stores.status.set(() => 'pending')
2366-
this.stores.statusCode.set(() => 200)
2367-
this.stores.isLoading.set(() => true)
2368-
this.stores.location.set(() => this.latestLocation)
2365+
this.stores.status.set('pending')
2366+
this.stores.statusCode.set(200)
2367+
this.stores.isLoading.set(true)
2368+
this.stores.location.set(this.latestLocation)
23692369
this.stores.setPendingMatches(pendingMatches)
23702370
// If a cached match moved to pending matches, remove it from cached matches
23712371
this.stores.setCachedMatches(nextCachedMatches)
@@ -2468,8 +2468,8 @@ export class RouterCore<
24682468
)
24692469
: currentMatches
24702470

2471-
this.stores.isLoading.set(() => false)
2472-
this.stores.loadedAt.set(() => Date.now())
2471+
this.stores.isLoading.set(false)
2472+
this.stores.loadedAt.set(Date.now())
24732473
/**
24742474
* When committing new matches, cache any exiting matches that are still usable.
24752475
* Routes that resolved with `status: 'error'` or `status: 'notFound'` are
@@ -2534,8 +2534,8 @@ export class RouterCore<
25342534
: 200
25352535

25362536
this.batch(() => {
2537-
this.stores.statusCode.set(() => nextStatusCode)
2538-
this.stores.redirect.set(() => redirect)
2537+
this.stores.statusCode.set(nextStatusCode)
2538+
this.stores.redirect.set(redirect)
25392539
})
25402540
}
25412541

@@ -2569,7 +2569,7 @@ export class RouterCore<
25692569
newStatusCode = 500
25702570
}
25712571
if (newStatusCode !== undefined) {
2572-
this.stores.statusCode.set(() => newStatusCode)
2572+
this.stores.statusCode.set(newStatusCode)
25732573
}
25742574
}
25752575

@@ -2651,7 +2651,7 @@ export class RouterCore<
26512651
)
26522652
}
26532653
} else {
2654-
cachedMatch.set(() => next)
2654+
cachedMatch.set(next)
26552655
}
26562656
}
26572657
})

‎packages/router-core/src/ssr/ssr-client.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export async function hydrate(router: AnyRouter): Promise<any> {
258258
// (e.g. preloads, invalidations) don't mistakenly detect a href change
259259
// (resolvedLocation defaults to undefined and router.load() is skipped
260260
// in the normal SSR hydration path).
261-
router.stores.resolvedLocation.set(() => router.stores.location.get())
261+
router.stores.resolvedLocation.set(router.stores.location.get())
262262
return routeChunkPromise
263263
}
264264

@@ -294,10 +294,8 @@ export async function hydrate(router: AnyRouter): Promise<any> {
294294
// Transitioner won't be rendered while loading so it cannot track the change from loading:true to loading:false
295295
if (router.stores.status.get() === 'pending') {
296296
router.batch(() => {
297-
router.stores.status.set(() => 'idle')
298-
router.stores.resolvedLocation.set(() =>
299-
router.stores.location.get(),
300-
)
297+
router.stores.status.set('idle')
298+
router.stores.resolvedLocation.set(router.stores.location.get())
301299
})
302300
}
303301
// hide the pending component once the load is finished

‎packages/router-core/src/stores.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLRUCache } from './lru-cache'
2-
import { arraysEqual } from './utils'
2+
import { arraysEqual, functionalUpdate } from './utils'
33

44
import type { AnyRoute } from './route'
55
import type { RouterState } from './router'
@@ -15,7 +15,7 @@ export interface RouterReadableStore<TValue> {
1515
export interface RouterWritableStore<
1616
TValue,
1717
> extends RouterReadableStore<TValue> {
18-
set: (updater: (prev: TValue) => TValue) => void
18+
set: ((updater: (prev: TValue) => TValue) => void) & ((value: TValue) => void)
1919
}
2020

2121
export type RouterBatchFn = (fn: () => void) => void
@@ -52,8 +52,8 @@ export function createNonReactiveMutableStore<TValue>(
5252
get() {
5353
return value
5454
},
55-
set(updater: (prev: TValue) => TValue) {
56-
value = updater(value)
55+
set(nextOrUpdater: TValue | ((prev: TValue) => TValue)) {
56+
value = functionalUpdate(nextOrUpdater, value)
5757
},
5858
}
5959
}
@@ -331,12 +331,12 @@ function reconcileMatchPool(
331331

332332
existing.routeId = nextMatch.routeId
333333
if (existing.get() !== nextMatch) {
334-
existing.set(() => nextMatch)
334+
existing.set(nextMatch)
335335
}
336336
}
337337

338338
if (!arraysEqual(idStore.get(), nextIds)) {
339-
idStore.set(() => nextIds)
339+
idStore.set(nextIds)
340340
}
341341
})
342342
}

‎packages/solid-router/src/Transitioner.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export function Transitioner() {
134134
})
135135

136136
Solid.batch(() => {
137-
router.stores.status.set(() => 'idle')
138-
router.stores.resolvedLocation.set(() => router.stores.location.get())
137+
router.stores.status.set('idle')
138+
router.stores.resolvedLocation.set(router.stores.location.get())
139139
})
140140

141141
if (changeInfo.hrefChanged) {

‎packages/vue-router/src/Transitioner.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function useTransitionerSetup() {
6060
isTransitioning.value = true
6161
// Also update the router state so useMatch knows we're transitioning
6262
try {
63-
router.stores.isTransitioning.set(() => true)
63+
router.stores.isTransitioning.set(true)
6464
} catch {
6565
// Ignore errors if component is unmounted
6666
}
@@ -71,7 +71,7 @@ export function useTransitionerSetup() {
7171
Vue.nextTick(() => {
7272
try {
7373
isTransitioning.value = false
74-
router.stores.isTransitioning.set(() => false)
74+
router.stores.isTransitioning.set(false)
7575
} catch {
7676
// Ignore errors if component is unmounted
7777
}
@@ -142,8 +142,8 @@ export function useTransitionerSetup() {
142142
if (!isAnyPending.value) {
143143
if (router.stores.status.get() === 'pending') {
144144
batch(() => {
145-
router.stores.status.set(() => 'idle')
146-
router.stores.resolvedLocation.set(() => router.stores.location.get())
145+
router.stores.status.set('idle')
146+
router.stores.resolvedLocation.set(router.stores.location.get())
147147
})
148148
}
149149
}
@@ -220,8 +220,8 @@ export function useTransitionerSetup() {
220220
try {
221221
if (!newValue && router.stores.status.get() === 'pending') {
222222
batch(() => {
223-
router.stores.status.set(() => 'idle')
224-
router.stores.resolvedLocation.set(() => router.stores.location.get())
223+
router.stores.status.set('idle')
224+
router.stores.resolvedLocation.set(router.stores.location.get())
225225
})
226226
}
227227

0 commit comments

Comments
 (0)