@tanstack/router-core 0.0.1-beta.4 → 0.0.1-beta.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/build/cjs/{packages/router-core/src/index.js → index.js} +25 -8
  2. package/build/cjs/{packages/router-core/src/index.js.map → index.js.map} +1 -1
  3. package/build/cjs/{packages/router-core/src/path.js → path.js} +19 -43
  4. package/build/cjs/path.js.map +1 -0
  5. package/build/cjs/{packages/router-core/src/qss.js → qss.js} +8 -13
  6. package/build/cjs/qss.js.map +1 -0
  7. package/build/cjs/route.js +155 -0
  8. package/build/cjs/route.js.map +1 -0
  9. package/build/cjs/{packages/router-core/src/routeConfig.js → routeConfig.js} +14 -13
  10. package/build/cjs/routeConfig.js.map +1 -0
  11. package/build/cjs/routeMatch.js +242 -0
  12. package/build/cjs/routeMatch.js.map +1 -0
  13. package/build/cjs/router.js +807 -0
  14. package/build/cjs/router.js.map +1 -0
  15. package/build/cjs/{packages/router-core/src/searchParams.js → searchParams.js} +10 -12
  16. package/build/cjs/searchParams.js.map +1 -0
  17. package/build/cjs/sharedClone.js +122 -0
  18. package/build/cjs/sharedClone.js.map +1 -0
  19. package/build/cjs/utils.js +47 -0
  20. package/build/cjs/utils.js.map +1 -0
  21. package/build/esm/index.js +890 -1739
  22. package/build/esm/index.js.map +1 -1
  23. package/build/stats-html.html +59 -49
  24. package/build/stats-react.json +196 -178
  25. package/build/types/index.d.ts +287 -283
  26. package/build/umd/index.development.js +1233 -922
  27. package/build/umd/index.development.js.map +1 -1
  28. package/build/umd/index.production.js +1 -1
  29. package/build/umd/index.production.js.map +1 -1
  30. package/package.json +4 -2
  31. package/src/frameworks.ts +2 -2
  32. package/src/index.ts +1 -1
  33. package/src/link.ts +86 -43
  34. package/src/path.ts +12 -8
  35. package/src/route.ts +170 -158
  36. package/src/routeConfig.ts +105 -77
  37. package/src/routeInfo.ts +26 -8
  38. package/src/routeMatch.ts +204 -217
  39. package/src/router.ts +680 -503
  40. package/src/sharedClone.ts +118 -0
  41. package/src/utils.ts +14 -72
  42. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -33
  43. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  44. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js +0 -33
  45. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js.map +0 -1
  46. package/build/cjs/node_modules/history/index.js +0 -815
  47. package/build/cjs/node_modules/history/index.js.map +0 -1
  48. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +0 -30
  49. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +0 -1
  50. package/build/cjs/packages/router-core/src/path.js.map +0 -1
  51. package/build/cjs/packages/router-core/src/qss.js.map +0 -1
  52. package/build/cjs/packages/router-core/src/route.js +0 -161
  53. package/build/cjs/packages/router-core/src/route.js.map +0 -1
  54. package/build/cjs/packages/router-core/src/routeConfig.js.map +0 -1
  55. package/build/cjs/packages/router-core/src/routeMatch.js +0 -266
  56. package/build/cjs/packages/router-core/src/routeMatch.js.map +0 -1
  57. package/build/cjs/packages/router-core/src/router.js +0 -789
  58. package/build/cjs/packages/router-core/src/router.js.map +0 -1
  59. package/build/cjs/packages/router-core/src/searchParams.js.map +0 -1
  60. package/build/cjs/packages/router-core/src/utils.js +0 -118
  61. package/build/cjs/packages/router-core/src/utils.js.map +0 -1
package/src/route.ts CHANGED
@@ -13,7 +13,6 @@ import {
13
13
  RouteInfo,
14
14
  RouteInfoByPath,
15
15
  } from './routeInfo'
16
- import { RouteMatch } from './routeMatch'
17
16
  import {
18
17
  Action,
19
18
  ActionState,
@@ -22,14 +21,17 @@ import {
22
21
  MatchRouteOptions,
23
22
  Router,
24
23
  } from './router'
25
- import { NoInfer, replaceEqualDeep } from './utils'
24
+ import { NoInfer } from './utils'
25
+ import { createStore } from '@solidjs/reactivity'
26
26
 
27
- export interface AnyRoute extends Route<any, any> {}
27
+ export interface AnyRoute extends Route<any, any, any> {}
28
28
 
29
29
  export interface Route<
30
30
  TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
31
31
  TRouteInfo extends AnyRouteInfo = RouteInfo,
32
+ TRouterContext = unknown,
32
33
  > {
34
+ routeInfo: TRouteInfo
33
35
  routeId: TRouteInfo['id']
34
36
  routeRouteId: TRouteInfo['routeId']
35
37
  routePath: TRouteInfo['path']
@@ -37,28 +39,8 @@ export interface Route<
37
39
  parentRoute?: AnyRoute
38
40
  childRoutes?: AnyRoute[]
39
41
  options: RouteOptions
40
- router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo>
41
- buildLink: <TTo extends string = '.'>(
42
- options: Omit<
43
- LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
44
- 'from'
45
- >,
46
- ) => LinkInfo
47
- matchRoute: <
48
- TTo extends string = '.',
49
- TResolved extends string = ResolveRelativePath<TRouteInfo['id'], TTo>,
50
- >(
51
- matchLocation: CheckRelativePath<
52
- TAllRouteInfo,
53
- TRouteInfo['fullPath'],
54
- NoInfer<TTo>
55
- > &
56
- Omit<ToOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>, 'from'>,
57
- opts?: MatchRouteOptions,
58
- ) => RouteInfoByPath<TAllRouteInfo, TResolved>['allParams']
59
- navigate: <TTo extends string = '.'>(
60
- options: Omit<LinkOptions<TAllRouteInfo, TRouteInfo['id'], TTo>, 'from'>,
61
- ) => Promise<void>
42
+ originalIndex: number
43
+ router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>
62
44
  action: unknown extends TRouteInfo['actionResponse']
63
45
  ?
64
46
  | Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
@@ -79,165 +61,195 @@ export interface Route<
79
61
  TRouteInfo['allParams'],
80
62
  TRouteInfo['routeLoaderData']
81
63
  >
64
+ // buildLink: <TTo extends string = '.'>(
65
+ // options: Omit<
66
+ // LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
67
+ // 'from'
68
+ // >,
69
+ // ) => LinkInfo
70
+ // matchRoute: <
71
+ // TTo extends string = '.',
72
+ // TResolved extends string = ResolveRelativePath<TRouteInfo['id'], TTo>,
73
+ // >(
74
+ // matchLocation: CheckRelativePath<
75
+ // TAllRouteInfo,
76
+ // TRouteInfo['fullPath'],
77
+ // NoInfer<TTo>
78
+ // > &
79
+ // Omit<ToOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>, 'from'>,
80
+ // opts?: MatchRouteOptions,
81
+ // ) => RouteInfoByPath<TAllRouteInfo, TResolved>['allParams']
82
+ // navigate: <TTo extends string = '.'>(
83
+ // options: Omit<
84
+ // LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
85
+ // 'from'
86
+ // >,
87
+ // ) => Promise<void>
82
88
  }
83
89
 
84
90
  export function createRoute<
85
91
  TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
86
92
  TRouteInfo extends AnyRouteInfo = RouteInfo,
93
+ TRouterContext = unknown,
87
94
  >(
88
95
  routeConfig: RouteConfig,
89
96
  options: TRouteInfo['options'],
97
+ originalIndex: number,
90
98
  parent: undefined | Route<TAllRouteInfo, any>,
91
- router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo>,
92
- ): Route<TAllRouteInfo, TRouteInfo> {
99
+ router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>,
100
+ ): Route<TAllRouteInfo, TRouteInfo, TRouterContext> {
93
101
  const { id, routeId, path: routePath, fullPath } = routeConfig
94
102
 
95
- const action =
96
- router.state.actions[id] ||
97
- (() => {
98
- router.state.actions[id] = {
99
- pending: [],
100
- submit: async <T, U>(
101
- submission: T,
102
- actionOpts?: { invalidate?: boolean },
103
- ) => {
104
- if (!route) {
105
- return
106
- }
107
-
108
- const invalidate = actionOpts?.invalidate ?? true
109
-
110
- const actionState: ActionState<T, U> = {
111
- submittedAt: Date.now(),
112
- status: 'pending',
113
- submission,
114
- }
115
-
116
- action.current = actionState
117
- action.latest = actionState
118
- action.pending.push(actionState)
119
-
120
- router.state = {
121
- ...router.state,
122
- currentAction: actionState,
123
- latestAction: actionState,
124
- }
125
-
126
- router.notify()
127
-
128
- try {
129
- const res = await route.options.action?.(submission)
130
- actionState.data = res as U
131
- if (invalidate) {
132
- router.invalidateRoute({ to: '.', fromCurrent: true })
133
- await router.reload()
134
- }
135
- actionState.status = 'success'
136
- return res
137
- } catch (err) {
138
- console.error(err)
139
- actionState.error = err
140
- actionState.status = 'error'
141
- } finally {
142
- action.pending = action.pending.filter((d) => d !== actionState)
143
- router.removeActionQueue.push({ action, actionState })
144
- router.notify()
145
- }
146
- },
147
- }
148
- return router.state.actions[id]!
149
- })()
150
-
151
- const loader =
152
- router.state.loaders[id] ||
153
- (() => {
154
- router.state.loaders[id] = {
155
- pending: [],
156
- fetch: (async (loaderContext: LoaderContext<any, any>) => {
157
- if (!route) {
158
- return
159
- }
160
-
161
- const loaderState: LoaderState<any, any> = {
162
- loadedAt: Date.now(),
163
- loaderContext,
164
- }
165
-
166
- loader.current = loaderState
167
- loader.latest = loaderState
168
- loader.pending.push(loaderState)
169
-
170
- // router.state = {
171
- // ...router.state,
172
- // currentAction: loaderState,
173
- // latestAction: loaderState,
174
- // }
175
-
176
- router.notify()
177
-
178
- try {
179
- return await route.options.loader?.(loaderContext)
180
- } finally {
181
- loader.pending = loader.pending.filter((d) => d !== loaderState)
182
- // router.removeActionQueue.push({ loader, loaderState })
183
- router.notify()
184
- }
185
- }) as any,
186
- }
187
- return router.state.loaders[id]!
188
- })()
189
-
190
- let route: Route<TAllRouteInfo, TRouteInfo> = {
103
+ let route: Route<TAllRouteInfo, TRouteInfo, TRouterContext> = {
104
+ routeInfo: undefined!,
191
105
  routeId: id,
192
106
  routeRouteId: routeId,
107
+ originalIndex,
193
108
  routePath,
194
109
  fullPath,
195
110
  options,
196
111
  router,
197
112
  childRoutes: undefined!,
198
113
  parentRoute: parent,
199
- action,
200
- loader: loader as any,
201
-
202
- buildLink: (options) => {
203
- return router.buildLink({
204
- ...options,
205
- from: fullPath,
206
- } as any) as any
207
- },
114
+ get action() {
115
+ let action =
116
+ router.store.actions[id] ||
117
+ (() => {
118
+ router.setStore((s) => {
119
+ s.actions[id] = {
120
+ submissions: [],
121
+ submit: async <T, U>(
122
+ submission: T,
123
+ actionOpts?: { invalidate?: boolean; multi?: boolean },
124
+ ) => {
125
+ if (!route) {
126
+ return
127
+ }
128
+
129
+ const invalidate = actionOpts?.invalidate ?? true
130
+
131
+ const [actionStore, setActionStore] = createStore<
132
+ ActionState<T, U>
133
+ >({
134
+ submittedAt: Date.now(),
135
+ status: 'pending',
136
+ submission,
137
+ isMulti: !!actionOpts?.multi,
138
+ })
139
+
140
+ router.setStore((s) => {
141
+ if (!actionOpts?.multi) {
142
+ s.actions[id]!.submissions = action.submissions.filter(
143
+ (d) => d.isMulti,
144
+ )
145
+ }
146
+
147
+ s.actions[id]!.current = actionStore
148
+ s.actions[id]!.latest = actionStore
149
+ s.actions[id]!.submissions.push(actionStore)
150
+ })
151
+
152
+ try {
153
+ const res = await route.options.action?.(submission)
154
+
155
+ setActionStore((s) => {
156
+ s.data = res as U
157
+ })
158
+
159
+ if (invalidate) {
160
+ router.invalidateRoute({ to: '.', fromCurrent: true })
161
+ await router.reload()
162
+ }
163
+
164
+ setActionStore((s) => {
165
+ s.status = 'success'
166
+ })
167
+
168
+ return res
169
+ } catch (err) {
170
+ console.error(err)
171
+ setActionStore((s) => {
172
+ s.error = err
173
+ s.status = 'error'
174
+ })
175
+ }
176
+ },
177
+ }
178
+ })
208
179
 
209
- navigate: (options) => {
210
- return router.navigate({
211
- ...options,
212
- from: fullPath,
213
- } as any) as any
180
+ return router.store.actions[id]!
181
+ })()
182
+
183
+ return action
214
184
  },
185
+ get loader() {
186
+ let loader =
187
+ router.store.loaders[id] ||
188
+ (() => {
189
+ router.setStore((s) => {
190
+ s.loaders[id] = {
191
+ pending: [],
192
+ fetch: (async (loaderContext: LoaderContext<any, any>) => {
193
+ if (!route) {
194
+ return
195
+ }
196
+
197
+ const loaderState: LoaderState<any, any> = {
198
+ loadedAt: Date.now(),
199
+ loaderContext,
200
+ }
201
+
202
+ router.setStore((s) => {
203
+ s.loaders[id]!.current = loaderState
204
+ s.loaders[id]!.latest = loaderState
205
+ s.loaders[id]!.pending.push(loaderState)
206
+ })
207
+
208
+ try {
209
+ return await route.options.loader?.(loaderContext)
210
+ } finally {
211
+ router.setStore((s) => {
212
+ s.loaders[id]!.pending = s.loaders[id]!.pending.filter(
213
+ (d) => d !== loaderState,
214
+ )
215
+ })
216
+ }
217
+ }) as any,
218
+ }
219
+ })
215
220
 
216
- matchRoute: (matchLocation, opts) => {
217
- return router.matchRoute(
218
- {
219
- ...matchLocation,
220
- from: fullPath,
221
- } as any,
222
- opts,
223
- )
221
+ return router.store.loaders[id]!
222
+ })()
223
+
224
+ return loader as any
224
225
  },
226
+
227
+ // buildLink: (options) => {
228
+ // return router.buildLink({
229
+ // ...options,
230
+ // from: fullPath,
231
+ // } as any) as any
232
+ // },
233
+
234
+ // navigate: (options) => {
235
+ // return router.navigate({
236
+ // ...options,
237
+ // from: fullPath,
238
+ // } as any) as any
239
+ // },
240
+
241
+ // matchRoute: (matchLocation, opts) => {
242
+ // return router.matchRoute(
243
+ // {
244
+ // ...matchLocation,
245
+ // from: fullPath,
246
+ // } as any,
247
+ // opts,
248
+ // ) as any
249
+ // },
225
250
  }
226
251
 
227
252
  router.options.createRoute?.({ router, route })
228
253
 
229
254
  return route
230
255
  }
231
-
232
- export function cascadeLoaderData(matches: RouteMatch<any, any>[]) {
233
- matches.forEach((match, index) => {
234
- const parent = matches[index - 1]
235
-
236
- if (parent) {
237
- match.loaderData = replaceEqualDeep(match.loaderData, {
238
- ...parent.loaderData,
239
- ...match.routeLoaderData,
240
- })
241
- }
242
- })
243
- }