@tanstack/router-core 0.0.1-alpha.4 → 0.0.1-alpha.6

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/router-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-alpha.4",
4
+ "version": "0.0.1-alpha.6",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
package/src/index.ts CHANGED
@@ -827,7 +827,8 @@ export interface RouterState {
827
827
  matches: RouteMatch[]
828
828
  lastUpdated: number
829
829
  loaderData: unknown
830
- action?: ActionState
830
+ currentAction?: ActionState
831
+ latestAction?: ActionState
831
832
  actions: Record<string, Action>
832
833
  pending?: PendingState
833
834
  }
@@ -948,7 +949,8 @@ export interface Action<
948
949
  // TError = unknown,
949
950
  > {
950
951
  submit: (submission?: TPayload) => Promise<TResponse>
951
- latest?: ActionState
952
+ current?: ActionState<TPayload, TResponse>
953
+ latest?: ActionState<TPayload, TResponse>
952
954
  pending: ActionState<TPayload, TResponse>[]
953
955
  }
954
956
 
@@ -1013,6 +1015,7 @@ export interface Router<
1013
1015
  routeTree: Route<TAllRouteInfo, RouteInfo>
1014
1016
  routesById: RoutesById<TAllRouteInfo>
1015
1017
  navigationPromise: Promise<void>
1018
+ removeActionQueue: { action: Action; actionState: ActionState }[]
1016
1019
  startedLoadingAt: number
1017
1020
  destroy: () => void
1018
1021
  resolveNavigation: () => void
@@ -1099,6 +1102,7 @@ export function createRouter<
1099
1102
  let router: Router<TRouteConfig, TAllRouteInfo> = {
1100
1103
  options: originalOptions,
1101
1104
  listeners: [],
1105
+ removeActionQueue: [],
1102
1106
  // Resolved after construction
1103
1107
  basepath: '',
1104
1108
  routeTree: undefined!,
@@ -1249,7 +1253,7 @@ export function createRouter<
1249
1253
 
1250
1254
  const toMatches = router.matchRoutes(pathname)
1251
1255
 
1252
- const prevParams = last(fromMatches)?.params
1256
+ const prevParams = { ...last(fromMatches)?.params }
1253
1257
 
1254
1258
  let nextParams =
1255
1259
  (dest.params ?? true) === true
@@ -1261,7 +1265,7 @@ export function createRouter<
1261
1265
  .map((d) => d.options.stringifyParams)
1262
1266
  .filter(Boolean)
1263
1267
  .forEach((fn) => {
1264
- Object.assign(nextParams!, fn!(nextParams!))
1268
+ Object.assign({}, nextParams!, fn!(nextParams!))
1265
1269
  })
1266
1270
  }
1267
1271
 
@@ -1410,6 +1414,17 @@ export function createRouter<
1410
1414
  router.location = next
1411
1415
  }
1412
1416
 
1417
+ // Clear out old actions
1418
+ router.removeActionQueue.forEach(({ action, actionState }) => {
1419
+ if (router.state.currentAction === actionState) {
1420
+ router.state.currentAction = undefined
1421
+ }
1422
+ if (action.current === actionState) {
1423
+ action.current = undefined
1424
+ }
1425
+ })
1426
+ router.removeActionQueue = []
1427
+
1413
1428
  // Cancel any pending matches
1414
1429
  router.cancelMatches()
1415
1430
 
@@ -1418,14 +1433,6 @@ export function createRouter<
1418
1433
  strictParseParams: true,
1419
1434
  })
1420
1435
 
1421
- unloadedMatches.forEach((match, index) => {
1422
- const parent = unloadedMatches[index - 1]
1423
- const child = unloadedMatches[index + 1]
1424
-
1425
- if (parent) match.__.setParentMatch(parent)
1426
- if (child) match.__.addChildMatch(child)
1427
- })
1428
-
1429
1436
  router.state = {
1430
1437
  ...router.state,
1431
1438
  pending: {
@@ -1641,6 +1648,8 @@ export function createRouter<
1641
1648
 
1642
1649
  recurse([router.routeTree])
1643
1650
 
1651
+ cascadeLoaderData(matches)
1652
+
1644
1653
  return matches
1645
1654
  },
1646
1655
 
@@ -1697,7 +1706,7 @@ export function createRouter<
1697
1706
  ...(router.state.pending?.matches ?? []),
1698
1707
  ].forEach((match) => {
1699
1708
  if (unloadedMatchIds.includes(match.matchId)) {
1700
- match.isInvalid = true
1709
+ match.invalidate()
1701
1710
  }
1702
1711
  })
1703
1712
  },
@@ -1746,7 +1755,7 @@ export function createRouter<
1746
1755
  return router.commitLocation(next, location.replace)
1747
1756
  },
1748
1757
 
1749
- navigate: async ({ from, to = '.', search, hash, replace }) => {
1758
+ navigate: async ({ from, to = '.', search, hash, replace, params }) => {
1750
1759
  // If this link simply reloads the current route,
1751
1760
  // make sure it has a new key so it will trigger a data refresh
1752
1761
 
@@ -1772,6 +1781,8 @@ export function createRouter<
1772
1781
  to: toString,
1773
1782
  search,
1774
1783
  hash,
1784
+ replace,
1785
+ params,
1775
1786
  })
1776
1787
  },
1777
1788
 
@@ -2011,12 +2022,14 @@ export function createRoute<
2011
2022
  submission,
2012
2023
  }
2013
2024
 
2025
+ action.current = actionState
2014
2026
  action.latest = actionState
2015
2027
  action.pending.push(actionState)
2016
2028
 
2017
2029
  router.state = {
2018
2030
  ...router.state,
2019
- action: actionState,
2031
+ currentAction: actionState,
2032
+ latestAction: actionState,
2020
2033
  }
2021
2034
 
2022
2035
  router.notify()
@@ -2036,9 +2049,7 @@ export function createRoute<
2036
2049
  actionState.status = 'error'
2037
2050
  } finally {
2038
2051
  action.pending = action.pending.filter((d) => d !== actionState)
2039
- if (actionState === router.state.action) {
2040
- router.state.action = undefined
2041
- }
2052
+ router.removeActionQueue.push({ action, actionState })
2042
2053
  router.notify()
2043
2054
  }
2044
2055
  },
@@ -2308,7 +2319,9 @@ type SearchParamOptions<
2308
2319
  search: SearchReducer<TFromSchema, TToSchema>
2309
2320
  }
2310
2321
 
2311
- type SearchReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
2322
+ type SearchReducer<TFrom, TTo> =
2323
+ | { [TKey in keyof TTo]: TTo[TKey] }
2324
+ | ((current: TFrom) => TTo)
2312
2325
 
2313
2326
  type PathParamOptions<
2314
2327
  TAllRouteInfo extends AnyAllRouteInfo,
@@ -2374,8 +2387,8 @@ export interface RouteMatch<
2374
2387
  }) => void)
2375
2388
  abortController: AbortController
2376
2389
  latestId: string
2377
- setParentMatch: (parentMatch: RouteMatch) => void
2378
- addChildMatch: (childMatch: RouteMatch) => void
2390
+ // setParentMatch: (parentMatch: RouteMatch) => void
2391
+ // addChildMatch: (childMatch: RouteMatch) => void
2379
2392
  validate: () => void
2380
2393
  startPending: () => void
2381
2394
  cancelPending: () => void
@@ -2384,6 +2397,7 @@ export interface RouteMatch<
2384
2397
  }
2385
2398
  cancel: () => void
2386
2399
  load: () => Promise<void>
2400
+ invalidate: () => void
2387
2401
  }
2388
2402
 
2389
2403
  export function createRouteMatch<
@@ -2450,18 +2464,18 @@ export function createRouteMatch<
2450
2464
  clearTimeout(routeMatch.__.pendingMinTimeout)
2451
2465
  delete routeMatch.__.pendingMinPromise
2452
2466
  },
2453
- setParentMatch: (parentMatch?: RouteMatch) => {
2454
- routeMatch.parentMatch = parentMatch
2455
- },
2456
- addChildMatch: (childMatch: RouteMatch) => {
2457
- if (
2458
- routeMatch.childMatches.find((d) => d.matchId === childMatch.matchId)
2459
- ) {
2460
- return
2461
- }
2462
-
2463
- routeMatch.childMatches.push(childMatch)
2464
- },
2467
+ // setParentMatch: (parentMatch?: RouteMatch) => {
2468
+ // routeMatch.parentMatch = parentMatch
2469
+ // },
2470
+ // addChildMatch: (childMatch: RouteMatch) => {
2471
+ // if (
2472
+ // routeMatch.childMatches.find((d) => d.matchId === childMatch.matchId)
2473
+ // ) {
2474
+ // return
2475
+ // }
2476
+
2477
+ // routeMatch.childMatches.push(childMatch)
2478
+ // },
2465
2479
  validate: () => {
2466
2480
  // Validate the search params and stabilize them
2467
2481
  const parentSearch =
@@ -2503,6 +2517,9 @@ export function createRouteMatch<
2503
2517
  routeMatch.__.abortController?.abort()
2504
2518
  routeMatch.__.cancelPending()
2505
2519
  },
2520
+ invalidate: () => {
2521
+ routeMatch.isInvalid = true
2522
+ },
2506
2523
  load: async () => {
2507
2524
  const id = '' + Date.now() + Math.random()
2508
2525
  routeMatch.__.latestId = id
@@ -2590,7 +2607,6 @@ export function createRouteMatch<
2590
2607
  data,
2591
2608
  )
2592
2609
 
2593
- cascadeLoaderData(routeMatch)
2594
2610
  routeMatch.error = undefined
2595
2611
  routeMatch.status = 'success'
2596
2612
  routeMatch.updatedAt = Date.now()
@@ -2648,19 +2664,17 @@ export function createRouteMatch<
2648
2664
  return routeMatch
2649
2665
  }
2650
2666
 
2651
- function cascadeLoaderData(routeMatch: RouteMatch<any, any>) {
2652
- if (routeMatch.parentMatch) {
2653
- routeMatch.loaderData = replaceEqualDeep(routeMatch.loaderData, {
2654
- ...routeMatch.parentMatch.loaderData,
2655
- ...routeMatch.routeLoaderData,
2656
- })
2657
- }
2667
+ function cascadeLoaderData(matches: RouteMatch<any, any>[]) {
2668
+ matches.forEach((match, index) => {
2669
+ const parent = matches[index - 1]
2658
2670
 
2659
- if (routeMatch.childMatches.length) {
2660
- routeMatch.childMatches.forEach((childMatch) => {
2661
- cascadeLoaderData(childMatch)
2662
- })
2663
- }
2671
+ if (parent) {
2672
+ match.loaderData = replaceEqualDeep(match.loaderData, {
2673
+ ...parent.loaderData,
2674
+ ...match.routeLoaderData,
2675
+ })
2676
+ }
2677
+ })
2664
2678
  }
2665
2679
 
2666
2680
  export function matchPathname(