Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Transitioner.tsx

145 lines3.6 KB

1import * as Solid from 'solid-js'
2import {
3 getLocationChangeInfo,
4 handleHashScroll,
5 trimPathRight,
6} from '@tanstack/router-core'
7import { useRouter } from './useRouter'
8import { useRouterState } from './useRouterState'
9import { usePrevious } from './utils'
10​
11export function Transitioner() {
12 const router = useRouter()
13 let mountLoadForRouter = { router, mounted: false }
14 const isLoading = useRouterState({
15 select: ({ isLoading }) => isLoading,
16 })
17​
18 if (router.isServer) {
19 return null
20 }
21​
22 const [isSolidTransitioning, startSolidTransition] = Solid.useTransition()
23​
24 // Track pending state changes
25 const hasPendingMatches = useRouterState({
26 select: (s) => s.matches.some((d) => d.status === 'pending'),
27 })
28​
29 const previousIsLoading = usePrevious(isLoading)
30​
31 const isAnyPending = () =>
32 isLoading() || isSolidTransitioning() || hasPendingMatches()
33 const previousIsAnyPending = usePrevious(isAnyPending)
34​
35 const isPagePending = () => isLoading() || hasPendingMatches()
36 const previousIsPagePending = usePrevious(isPagePending)
37​
38 router.startTransition = (fn: () => void | Promise<void>) => {
39 Solid.startTransition(() => {
40 startSolidTransition(fn)
41 })
42 }
43​
44 // Subscribe to location changes
45 // and try to load the new location
46 Solid.onMount(() => {
47 const unsub = router.history.subscribe(router.load)
48​
49 const nextLocation = router.buildLocation({
50 to: router.latestLocation.pathname,
51 search: true,
52 params: true,
53 hash: true,
54 state: true,
55 _includeValidateSearch: true,
56 })
57​
58 if (
59 trimPathRight(router.latestLocation.href) !==
60 trimPathRight(nextLocation.href)
61 ) {
62 router.commitLocation({ ...nextLocation, replace: true })
63 }
64​
65 Solid.onCleanup(() => {
66 unsub()
67 })
68 })
69​
70 // Try to load the initial location
71 Solid.createRenderEffect(() => {
72 Solid.untrack(() => {
73 if (
74 // if we are hydrating from SSR, loading is triggered in ssr-client
75 (typeof window !== 'undefined' && router.ssr) ||
76 (mountLoadForRouter.router === router && mountLoadForRouter.mounted)
77 ) {
78 return
79 }
80 mountLoadForRouter = { router, mounted: true }
81 const tryLoad = async () => {
82 try {
83 await router.load()
84 } catch (err) {
85 console.error(err)
86 }
87 }
88 tryLoad()
89 })
90 })
91​
92 Solid.createRenderEffect(
93 Solid.on(
94 [previousIsLoading, isLoading],
95 ([previousIsLoading, isLoading]) => {
96 if (previousIsLoading.previous && !isLoading) {
97 router.emit({
98 type: 'onLoad',
99 ...getLocationChangeInfo(router.state),
100 })
101 }
102 },
103 ),
104 )
105​
106 Solid.createComputed(
107 Solid.on(
108 [isPagePending, previousIsPagePending],
109 ([isPagePending, previousIsPagePending]) => {
110 // emit onBeforeRouteMount
111 if (previousIsPagePending.previous && !isPagePending) {
112 router.emit({
113 type: 'onBeforeRouteMount',
114 ...getLocationChangeInfo(router.state),
115 })
116 }
117 },
118 ),
119 )
120​
121 Solid.createRenderEffect(
122 Solid.on(
123 [isAnyPending, previousIsAnyPending],
124 ([isAnyPending, previousIsAnyPending]) => {
125 // The router was pending and now it's not
126 if (previousIsAnyPending.previous && !isAnyPending) {
127 router.emit({
128 type: 'onResolved',
129 ...getLocationChangeInfo(router.state),
130 })
131​
132 router.__store.setState((s) => ({
133 ...s,
134 status: 'idle',
135 resolvedLocation: s.location,
136 }))
137​
138 handleHashScroll(router)
139 }
140 },
141 ),
142 )
143​
144 return null