@tanstack/react-router 0.0.1-alpha.10 → 0.0.1-alpha.11

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.
@@ -1305,6 +1305,40 @@ function createRoute(routeConfig, options, parent, router) {
1305
1305
  return router.state.actions[id];
1306
1306
  })();
1307
1307
 
1308
+ const loader = router.state.loaders[id] || (() => {
1309
+ router.state.loaders[id] = {
1310
+ pending: [],
1311
+ fetch: async loaderContext => {
1312
+ if (!route) {
1313
+ return;
1314
+ }
1315
+
1316
+ const loaderState = {
1317
+ loadedAt: Date.now(),
1318
+ loaderContext
1319
+ };
1320
+ loader.current = loaderState;
1321
+ loader.latest = loaderState;
1322
+ loader.pending.push(loaderState); // router.state = {
1323
+ // ...router.state,
1324
+ // currentAction: loaderState,
1325
+ // latestAction: loaderState,
1326
+ // }
1327
+
1328
+ router.notify();
1329
+
1330
+ try {
1331
+ return await (route.options.loader == null ? void 0 : route.options.loader(loaderContext));
1332
+ } finally {
1333
+ loader.pending = loader.pending.filter(d => d !== loaderState); // router.removeActionQueue.push({ loader, loaderState })
1334
+
1335
+ router.notify();
1336
+ }
1337
+ }
1338
+ };
1339
+ return router.state.loaders[id];
1340
+ })();
1341
+
1308
1342
  let route = {
1309
1343
  routeId: id,
1310
1344
  routeRouteId: routeId,
@@ -1315,6 +1349,7 @@ function createRoute(routeConfig, options, parent, router) {
1315
1349
  childRoutes: undefined,
1316
1350
  parentRoute: parent,
1317
1351
  action,
1352
+ loader: loader,
1318
1353
  buildLink: options => {
1319
1354
  return router.buildLink(_extends({}, options, {
1320
1355
  from: fullPath
@@ -1504,7 +1539,31 @@ function createRouteMatch(router, route, opts) {
1504
1539
  hasLoaders: () => {
1505
1540
  return !!(route.options.loader || elementTypes.some(d => typeof route.options[d] === 'function'));
1506
1541
  },
1507
- load: async opts => {
1542
+ load: async loaderOpts => {
1543
+ const now = Date.now();
1544
+ const minMaxAge = loaderOpts != null && loaderOpts.preload ? Math.max(loaderOpts == null ? void 0 : loaderOpts.maxAge, loaderOpts == null ? void 0 : loaderOpts.gcMaxAge) : 0; // If this is a preload, add it to the preload cache
1545
+
1546
+ if (loaderOpts != null && loaderOpts.preload && minMaxAge > 0) {
1547
+ // If the match is currently active, don't preload it
1548
+ if (router.state.matches.find(d => d.matchId === routeMatch.matchId)) {
1549
+ return;
1550
+ }
1551
+
1552
+ router.matchCache[routeMatch.matchId] = {
1553
+ gc: now + loaderOpts.gcMaxAge,
1554
+ match: routeMatch
1555
+ };
1556
+ } // If the match is invalid, errored or idle, trigger it to load
1557
+
1558
+
1559
+ if (routeMatch.status === 'success' && routeMatch.getIsInvalid() || routeMatch.status === 'error' || routeMatch.status === 'idle') {
1560
+ const maxAge = loaderOpts != null && loaderOpts.preload ? loaderOpts == null ? void 0 : loaderOpts.maxAge : undefined;
1561
+ routeMatch.fetch({
1562
+ maxAge
1563
+ });
1564
+ }
1565
+ },
1566
+ fetch: async opts => {
1508
1567
  const id = '' + Date.now() + Math.random();
1509
1568
  routeMatch.__.latestId = id; // If the match was in an error state, set it
1510
1569
  // to a loading state again. Otherwise, keep it
@@ -1534,12 +1593,7 @@ function createRouteMatch(router, route, opts) {
1534
1593
  return;
1535
1594
  }
1536
1595
 
1537
- if (typeof routeElement === 'function') {
1538
- const res = await routeElement(routeMatch);
1539
- routeMatch.__[type] = res;
1540
- } else {
1541
- routeMatch.__[type] = routeMatch.options[type];
1542
- }
1596
+ routeMatch.__[type] = await router.options.createElement(routeElement);
1543
1597
  }));
1544
1598
  })();
1545
1599
 
@@ -1714,6 +1768,7 @@ function createRouter(userOptions) {
1714
1768
  location: null,
1715
1769
  matches: [],
1716
1770
  actions: {},
1771
+ loaders: {},
1717
1772
  loaderData: {},
1718
1773
  lastUpdated: Date.now(),
1719
1774
  isFetching: false,
@@ -2050,32 +2105,11 @@ function createRouter(userOptions) {
2050
2105
  return matches;
2051
2106
  },
2052
2107
  loadMatches: async (resolvedMatches, loaderOpts) => {
2053
- const now = Date.now();
2054
- const minMaxAge = loaderOpts != null && loaderOpts.preload ? Math.max(loaderOpts == null ? void 0 : loaderOpts.maxAge, loaderOpts == null ? void 0 : loaderOpts.gcMaxAge) : 0;
2055
2108
  const matchPromises = resolvedMatches.map(async match => {
2056
2109
  // Validate the match (loads search params etc)
2057
- match.__.validate(); // If this is a preload, add it to the preload cache
2110
+ match.__.validate();
2058
2111
 
2059
-
2060
- if (loaderOpts != null && loaderOpts.preload && minMaxAge > 0) {
2061
- // If the match is currently active, don't preload it
2062
- if (router.state.matches.find(d => d.matchId === match.matchId)) {
2063
- return;
2064
- }
2065
-
2066
- router.matchCache[match.matchId] = {
2067
- gc: now + loaderOpts.gcMaxAge,
2068
- match
2069
- };
2070
- } // If the match is invalid, errored or idle, trigger it to load
2071
-
2072
-
2073
- if (match.status === 'success' && match.getIsInvalid() || match.status === 'error' || match.status === 'idle') {
2074
- const maxAge = loaderOpts != null && loaderOpts.preload ? loaderOpts == null ? void 0 : loaderOpts.maxAge : undefined;
2075
- match.load({
2076
- maxAge
2077
- });
2078
- }
2112
+ match.load(loaderOpts);
2079
2113
 
2080
2114
  if (match.status === 'loading') {
2081
2115
  // If requested, start the pending timers
@@ -2477,6 +2511,17 @@ const useRouterSubscription = router => {
2477
2511
  function createReactRouter(opts) {
2478
2512
  const makeRouteExt = (route, router) => {
2479
2513
  return {
2514
+ useRoute: function useRoute(subRouteId) {
2515
+ if (subRouteId === void 0) {
2516
+ subRouteId = '.';
2517
+ }
2518
+
2519
+ const resolvedRouteId = router.resolvePath(route.routeId, subRouteId);
2520
+ const resolvedRoute = router.getRoute(resolvedRouteId);
2521
+ useRouterSubscription(router);
2522
+ invariant(resolvedRoute, "Could not find a route for route \"" + resolvedRouteId + "\"! Did you forget to add it to your route config?");
2523
+ return resolvedRoute;
2524
+ },
2480
2525
  linkProps: options => {
2481
2526
  var _functionalUpdate, _functionalUpdate2;
2482
2527
 
@@ -2584,12 +2629,6 @@ function createReactRouter(opts) {
2584
2629
  useRouterSubscription(router);
2585
2630
  return router.state;
2586
2631
  },
2587
- useRoute: routeId => {
2588
- const route = router.getRoute(routeId);
2589
- useRouterSubscription(router);
2590
- invariant(route, "Could not find a route for route \"" + routeId + "\"! Did you forget to add it to your route config?");
2591
- return route;
2592
- },
2593
2632
  useMatch: routeId => {
2594
2633
  useRouterSubscription(router);
2595
2634
  invariant(routeId !== rootRouteId, "\"" + rootRouteId + "\" cannot be used with useMatch! Did you mean to useRoute(\"" + rootRouteId + "\")?");
@@ -2617,6 +2656,19 @@ function createReactRouter(opts) {
2617
2656
  } = _ref;
2618
2657
  const routeExt = makeRouteExt(route, router);
2619
2658
  Object.assign(route, routeExt);
2659
+ },
2660
+ createElement: async element => {
2661
+ if (typeof element === 'function') {
2662
+ const res = await element(); // Support direct import() calls
2663
+
2664
+ if (typeof res === 'object' && res.default) {
2665
+ return /*#__PURE__*/React.createElement(res.default);
2666
+ } else {
2667
+ return res;
2668
+ }
2669
+ }
2670
+
2671
+ return element;
2620
2672
  }
2621
2673
  }));
2622
2674
  return coreRouter;