@tanstack/react-router 0.0.1-beta.235 → 0.0.1-beta.237

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 (39) hide show
  1. package/build/cjs/Matches.js +31 -18
  2. package/build/cjs/Matches.js.map +1 -1
  3. package/build/cjs/RouterProvider.js +45 -25
  4. package/build/cjs/RouterProvider.js.map +1 -1
  5. package/build/cjs/index.js +1 -0
  6. package/build/cjs/index.js.map +1 -1
  7. package/build/cjs/route.js +13 -7
  8. package/build/cjs/route.js.map +1 -1
  9. package/build/cjs/router.js +49 -37
  10. package/build/cjs/router.js.map +1 -1
  11. package/build/cjs/useParams.js +7 -2
  12. package/build/cjs/useParams.js.map +1 -1
  13. package/build/cjs/useSearch.js +6 -1
  14. package/build/cjs/useSearch.js.map +1 -1
  15. package/build/cjs/utils.js +4 -1
  16. package/build/cjs/utils.js.map +1 -1
  17. package/build/esm/index.js +155 -92
  18. package/build/esm/index.js.map +1 -1
  19. package/build/stats-html.html +1 -1
  20. package/build/stats-react.json +574 -293
  21. package/build/types/Matches.d.ts +9 -3
  22. package/build/types/RouterProvider.d.ts +3 -0
  23. package/build/types/route.d.ts +30 -10
  24. package/build/types/router.d.ts +6 -3
  25. package/build/types/useParams.d.ts +3 -1
  26. package/build/types/useSearch.d.ts +3 -1
  27. package/build/types/utils.d.ts +3 -1
  28. package/build/umd/index.development.js +423 -95
  29. package/build/umd/index.development.js.map +1 -1
  30. package/build/umd/index.production.js +2 -2
  31. package/build/umd/index.production.js.map +1 -1
  32. package/package.json +4 -2
  33. package/src/Matches.tsx +70 -35
  34. package/src/RouterProvider.tsx +68 -32
  35. package/src/route.ts +37 -15
  36. package/src/router.ts +62 -44
  37. package/src/useParams.tsx +14 -4
  38. package/src/useSearch.tsx +11 -3
  39. package/src/utils.ts +20 -12
@@ -11,6 +11,7 @@
11
11
  'use strict';
12
12
 
13
13
  var history = require('@tanstack/history');
14
+ var store = require('@tanstack/store');
14
15
  var searchParams = require('./searchParams.js');
15
16
  var utils = require('./utils.js');
16
17
  var RouterProvider = require('./RouterProvider.js');
@@ -52,9 +53,6 @@ class Router {
52
53
  // by the router provider once rendered. We provide these so that the
53
54
  // router can be used in a non-react environment if necessary
54
55
  startReactTransition = fn => fn();
55
- setState = updater => {
56
- this.state = utils.functionalUpdate(updater, this.state);
57
- };
58
56
  update = newOptions => {
59
57
  this.options = {
60
58
  ...this.options,
@@ -69,10 +67,20 @@ class Router {
69
67
  this.routeTree = this.options.routeTree;
70
68
  this.buildRouteTree();
71
69
  }
72
- if (!this.state) {
73
- this.state = getInitialRouterState(this.latestLocation);
70
+ if (!this.__store) {
71
+ this.__store = new store.Store(getInitialRouterState(this.latestLocation), {
72
+ onUpdate: () => {
73
+ this.__store.state = {
74
+ ...this.state,
75
+ status: this.state.isTransitioning || this.state.isLoading ? 'pending' : 'idle'
76
+ };
77
+ }
78
+ });
74
79
  }
75
80
  };
81
+ get state() {
82
+ return this.__store.state;
83
+ }
76
84
  buildRouteTree = () => {
77
85
  this.routesById = {};
78
86
  this.routesByPath = {};
@@ -344,7 +352,7 @@ class Router {
344
352
  RouterProvider.getRouteMatch(this.state, id)?.abortController?.abort();
345
353
  };
346
354
  cancelMatches = () => {
347
- this.state.matches.forEach(match => {
355
+ this.state.pendingMatches?.forEach(match => {
348
356
  this.cancelMatch(match.id);
349
357
  });
350
358
  };
@@ -535,6 +543,12 @@ class Router {
535
543
  }) => {
536
544
  let latestPromise;
537
545
  let firstBadMatchIndex;
546
+ const updatePendingMatch = match => {
547
+ this.__store.setState(s => ({
548
+ ...s,
549
+ pendingMatches: s.pendingMatches?.map(d => d.id === match.id ? match : d)
550
+ }));
551
+ };
538
552
 
539
553
  // Check each match middleware to see if the route can be accessed
540
554
  try {
@@ -691,12 +705,10 @@ class Router {
691
705
  loadPromise
692
706
  };
693
707
  if (!preload) {
694
- this.setState(s => ({
695
- ...s,
696
- matches: s.matches.map(d => d.id === match.id ? match : d)
697
- }));
708
+ updatePendingMatch(match);
698
709
  }
699
710
  let didShowPending = false;
711
+ const pendingMinMs = route.options.pendingMinMs ?? this.options.defaultPendingMinMs;
700
712
  await new Promise(async resolve => {
701
713
  // If the route has a pending component and a pendingMs option,
702
714
  // forcefully show the pending component
@@ -708,17 +720,13 @@ class Router {
708
720
  ...match,
709
721
  showPending: true
710
722
  };
711
- this.setState(s => ({
712
- ...s,
713
- matches: s.matches.map(d => d.id === match.id ? match : d)
714
- }));
723
+ updatePendingMatch(match);
715
724
  resolve();
716
725
  });
717
726
  }
718
727
  try {
719
728
  const loaderData = await loadPromise;
720
729
  if (latestPromise = checkLatest()) return await latestPromise;
721
- const pendingMinMs = route.options.pendingMinMs ?? this.options.defaultPendingMinMs;
722
730
  if (didShowPending && pendingMinMs) {
723
731
  await new Promise(r => setTimeout(r, pendingMinMs));
724
732
  }
@@ -748,12 +756,19 @@ class Router {
748
756
  isFetching: false,
749
757
  updatedAt: Date.now()
750
758
  };
759
+ } finally {
760
+ // If we showed the pending component, that means
761
+ // we already moved the pendingMatches to the matches
762
+ // state, so we need to update that specific match
763
+ if (didShowPending && pendingMinMs && match.showPending) {
764
+ this.__store.setState(s => ({
765
+ ...s,
766
+ matches: s.matches?.map(d => d.id === match.id ? match : d)
767
+ }));
768
+ }
751
769
  }
752
770
  if (!preload) {
753
- this.setState(s => ({
754
- ...s,
755
- matches: s.matches.map(d => d.id === match.id ? match : d)
756
- }));
771
+ updatePendingMatch(match);
757
772
  }
758
773
  resolve();
759
774
  });
@@ -782,24 +797,23 @@ class Router {
782
797
  });
783
798
 
784
799
  // Match the routes
785
- let matches = this.matchRoutes(next.pathname, next.search, {
800
+ let pendingMatches = this.matchRoutes(next.pathname, next.search, {
786
801
  debug: true
787
802
  });
788
- this.pendingMatches = matches;
789
803
  const previousMatches = this.state.matches;
790
804
 
791
805
  // Ingest the new matches
792
- this.setState(s => ({
806
+ this.__store.setState(s => ({
793
807
  ...s,
794
- // status: 'pending',
808
+ isLoading: true,
795
809
  location: next,
796
- matches
810
+ pendingMatches
797
811
  }));
798
812
  try {
799
813
  try {
800
814
  // Load the matches
801
815
  await this.loadMatches({
802
- matches,
816
+ matches: pendingMatches,
803
817
  checkLatest: () => this.checkLatest(promise),
804
818
  invalidate: opts?.invalidate
805
819
  });
@@ -814,14 +828,13 @@ class Router {
814
828
  }
815
829
  const exitingMatchIds = previousMatches.filter(id => !this.pendingMatches.includes(id));
816
830
  const enteringMatchIds = this.pendingMatches.filter(id => !previousMatches.includes(id));
817
- const stayingMatchIds = previousMatches.filter(id => this.pendingMatches.includes(id))
818
-
819
- // setState((s) => ({
820
- // ...s,
821
- // status: 'idle',
822
- // resolvedLocation: s.location,
823
- // matches,
824
- // }))
831
+ const stayingMatchIds = previousMatches.filter(id => this.pendingMatches.includes(id));
832
+ this.__store.setState(s => ({
833
+ ...s,
834
+ isLoading: false,
835
+ matches: pendingMatches,
836
+ pendingMatches: undefined
837
+ }))
825
838
 
826
839
  //
827
840
  ;
@@ -976,9 +989,6 @@ class Router {
976
989
  return false;
977
990
  }
978
991
  const baseLocation = opts?.pending ? this.latestLocation : this.state.resolvedLocation;
979
-
980
- // const baseLocation = state.resolvedLocation
981
-
982
992
  if (!baseLocation) {
983
993
  return false;
984
994
  }
@@ -1050,7 +1060,7 @@ class Router {
1050
1060
  }
1051
1061
  return match;
1052
1062
  });
1053
- this.setState(s => {
1063
+ this.__store.setState(s => {
1054
1064
  return {
1055
1065
  ...s,
1056
1066
  matches: matches
@@ -1081,6 +1091,8 @@ class SearchParamError extends Error {}
1081
1091
  class PathParamError extends Error {}
1082
1092
  function getInitialRouterState(location) {
1083
1093
  return {
1094
+ isLoading: false,
1095
+ isTransitioning: false,
1084
1096
  status: 'idle',
1085
1097
  resolvedLocation: location,
1086
1098
  location,