@tanstack/router-devtools 0.0.1-beta.242 → 0.0.1-beta.243

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.
@@ -9,10 +9,10 @@
9
9
  * @license MIT
10
10
  */
11
11
  (function (global, factory) {
12
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
13
- typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
14
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TanStackRouterDevtools = {}, global.React));
15
- })(this, (function (exports, React) { 'use strict';
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('use-sync-external-store/shim')) :
13
+ typeof define === 'function' && define.amd ? define(['exports', 'react', 'use-sync-external-store/shim'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TanStackRouterDevtools = {}, global.React, global.require$$1));
15
+ })(this, (function (exports, React, require$$1) { 'use strict';
16
16
 
17
17
  function _interopNamespaceDefault(e) {
18
18
  var n = Object.create(null);
@@ -81,6 +81,216 @@
81
81
  }
82
82
  }
83
83
 
84
+ var withSelector = {exports: {}};
85
+
86
+ var withSelector_development = {};
87
+
88
+ /**
89
+ * @license React
90
+ * use-sync-external-store-shim/with-selector.development.js
91
+ *
92
+ * Copyright (c) Facebook, Inc. and its affiliates.
93
+ *
94
+ * This source code is licensed under the MIT license found in the
95
+ * LICENSE file in the root directory of this source tree.
96
+ */
97
+
98
+ var hasRequiredWithSelector_development;
99
+
100
+ function requireWithSelector_development () {
101
+ if (hasRequiredWithSelector_development) return withSelector_development;
102
+ hasRequiredWithSelector_development = 1;
103
+
104
+ {
105
+ (function() {
106
+
107
+ /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
108
+ if (
109
+ typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
110
+ typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
111
+ 'function'
112
+ ) {
113
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
114
+ }
115
+ var React$1 = React;
116
+ var shim = require$$1;
117
+
118
+ /**
119
+ * inlined Object.is polyfill to avoid requiring consumers ship their own
120
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
121
+ */
122
+ function is(x, y) {
123
+ return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
124
+ ;
125
+ }
126
+
127
+ var objectIs = typeof Object.is === 'function' ? Object.is : is;
128
+
129
+ var useSyncExternalStore = shim.useSyncExternalStore;
130
+
131
+ // for CommonJS interop.
132
+
133
+ var useRef = React$1.useRef,
134
+ useEffect = React$1.useEffect,
135
+ useMemo = React$1.useMemo,
136
+ useDebugValue = React$1.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments.
137
+
138
+ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
139
+ // Use this to track the rendered snapshot.
140
+ var instRef = useRef(null);
141
+ var inst;
142
+
143
+ if (instRef.current === null) {
144
+ inst = {
145
+ hasValue: false,
146
+ value: null
147
+ };
148
+ instRef.current = inst;
149
+ } else {
150
+ inst = instRef.current;
151
+ }
152
+
153
+ var _useMemo = useMemo(function () {
154
+ // Track the memoized state using closure variables that are local to this
155
+ // memoized instance of a getSnapshot function. Intentionally not using a
156
+ // useRef hook, because that state would be shared across all concurrent
157
+ // copies of the hook/component.
158
+ var hasMemo = false;
159
+ var memoizedSnapshot;
160
+ var memoizedSelection;
161
+
162
+ var memoizedSelector = function (nextSnapshot) {
163
+ if (!hasMemo) {
164
+ // The first time the hook is called, there is no memoized result.
165
+ hasMemo = true;
166
+ memoizedSnapshot = nextSnapshot;
167
+
168
+ var _nextSelection = selector(nextSnapshot);
169
+
170
+ if (isEqual !== undefined) {
171
+ // Even if the selector has changed, the currently rendered selection
172
+ // may be equal to the new selection. We should attempt to reuse the
173
+ // current value if possible, to preserve downstream memoizations.
174
+ if (inst.hasValue) {
175
+ var currentSelection = inst.value;
176
+
177
+ if (isEqual(currentSelection, _nextSelection)) {
178
+ memoizedSelection = currentSelection;
179
+ return currentSelection;
180
+ }
181
+ }
182
+ }
183
+
184
+ memoizedSelection = _nextSelection;
185
+ return _nextSelection;
186
+ } // We may be able to reuse the previous invocation's result.
187
+
188
+
189
+ // We may be able to reuse the previous invocation's result.
190
+ var prevSnapshot = memoizedSnapshot;
191
+ var prevSelection = memoizedSelection;
192
+
193
+ if (objectIs(prevSnapshot, nextSnapshot)) {
194
+ // The snapshot is the same as last time. Reuse the previous selection.
195
+ return prevSelection;
196
+ } // The snapshot has changed, so we need to compute a new selection.
197
+
198
+
199
+ // The snapshot has changed, so we need to compute a new selection.
200
+ var nextSelection = selector(nextSnapshot); // If a custom isEqual function is provided, use that to check if the data
201
+ // has changed. If it hasn't, return the previous selection. That signals
202
+ // to React that the selections are conceptually equal, and we can bail
203
+ // out of rendering.
204
+
205
+ // If a custom isEqual function is provided, use that to check if the data
206
+ // has changed. If it hasn't, return the previous selection. That signals
207
+ // to React that the selections are conceptually equal, and we can bail
208
+ // out of rendering.
209
+ if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
210
+ return prevSelection;
211
+ }
212
+
213
+ memoizedSnapshot = nextSnapshot;
214
+ memoizedSelection = nextSelection;
215
+ return nextSelection;
216
+ }; // Assigning this to a constant so that Flow knows it can't change.
217
+
218
+
219
+ // Assigning this to a constant so that Flow knows it can't change.
220
+ var maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;
221
+
222
+ var getSnapshotWithSelector = function () {
223
+ return memoizedSelector(getSnapshot());
224
+ };
225
+
226
+ var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? undefined : function () {
227
+ return memoizedSelector(maybeGetServerSnapshot());
228
+ };
229
+ return [getSnapshotWithSelector, getServerSnapshotWithSelector];
230
+ }, [getSnapshot, getServerSnapshot, selector, isEqual]),
231
+ getSelection = _useMemo[0],
232
+ getServerSelection = _useMemo[1];
233
+
234
+ var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
235
+ useEffect(function () {
236
+ inst.hasValue = true;
237
+ inst.value = value;
238
+ }, [value]);
239
+ useDebugValue(value);
240
+ return value;
241
+ }
242
+
243
+ withSelector_development.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;
244
+ /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
245
+ if (
246
+ typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
247
+ typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
248
+ 'function'
249
+ ) {
250
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
251
+ }
252
+
253
+ })();
254
+ }
255
+ return withSelector_development;
256
+ }
257
+
258
+ {
259
+ withSelector.exports = requireWithSelector_development();
260
+ }
261
+
262
+ var withSelectorExports = withSelector.exports;
263
+
264
+ // src/index.ts
265
+ function useStore(store, selector = (d) => d) {
266
+ const slice = withSelectorExports.useSyncExternalStoreWithSelector(
267
+ store.subscribe,
268
+ () => store.state,
269
+ () => store.state,
270
+ selector,
271
+ shallow
272
+ );
273
+ return slice;
274
+ }
275
+ function shallow(objA, objB) {
276
+ if (Object.is(objA, objB)) {
277
+ return true;
278
+ }
279
+ if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
280
+ return false;
281
+ }
282
+ const keysA = Object.keys(objA);
283
+ if (keysA.length !== Object.keys(objB).length) {
284
+ return false;
285
+ }
286
+ for (let i = 0; i < keysA.length; i++) {
287
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
288
+ return false;
289
+ }
290
+ }
291
+ return true;
292
+ }
293
+
84
294
  /**
85
295
  * @tanstack/react-router/src/index.tsx
86
296
  *
@@ -114,6 +324,10 @@
114
324
  if (typeof document !== 'undefined') {
115
325
  window.__TSR_ROUTER_CONTEXT__ = routerContext;
116
326
  }
327
+ function useRouterState(opts) {
328
+ const router = useRouter();
329
+ return useStore(router.__store, opts?.select);
330
+ }
117
331
  function useRouter() {
118
332
  const resolvedContext = typeof document !== 'undefined' ? window.__TSR_ROUTER_CONTEXT__ || routerContext : routerContext;
119
333
  const value = React__namespace.useContext(resolvedContext);
@@ -792,9 +1006,9 @@
792
1006
  activeRouteId,
793
1007
  setActiveRouteId
794
1008
  }) {
795
- const router = useRouter();
796
- const matches = router.state.status === 'pending' ? router.state.pendingMatches ?? [] : router.state.matches;
797
- const match = router.state.matches.find(d => d.routeId === route.id);
1009
+ const routerState = useRouterState();
1010
+ const matches = routerState.status === 'pending' ? routerState.pendingMatches ?? [] : routerState.matches;
1011
+ const match = routerState.matches.find(d => d.routeId === route.id);
798
1012
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
799
1013
  role: "button",
800
1014
  "aria-label": `Open match details for ${route.id}`,
@@ -861,7 +1075,8 @@
861
1075
  ...panelProps
862
1076
  } = props;
863
1077
  const router = useRouter();
864
- const matches = [...(router.state.pendingMatches ?? []), ...router.state.matches];
1078
+ const routerState = useRouterState();
1079
+ const matches = [...(routerState.pendingMatches ?? []), ...routerState.matches];
865
1080
  invariant(router, 'No router was found for the TanStack Router Devtools. Please place the devtools in the <RouterProvider> component tree or pass the router instance to the devtools manually.');
866
1081
 
867
1082
  // useStore(router.__store)
@@ -869,7 +1084,7 @@
869
1084
  const [showMatches, setShowMatches] = useLocalStorage('tanstackRouterDevtoolsShowMatches', true);
870
1085
  const [activeRouteId, setActiveRouteId] = useLocalStorage('tanstackRouterDevtoolsActiveRouteId', '');
871
1086
  const activeMatch = React.useMemo(() => matches.find(d => d.routeId === activeRouteId), [matches, activeRouteId]);
872
- const hasSearch = Object.keys(router.state.location.search || {}).length;
1087
+ const hasSearch = Object.keys(routerState.location.search || {}).length;
873
1088
 
874
1089
  // const preloadMatches = matches.filter((match) => {
875
1090
  // return (
@@ -1025,7 +1240,7 @@
1025
1240
  gap: '.5rem',
1026
1241
  fontWeight: 'bold'
1027
1242
  }
1028
- }, "Pathname", ' ', router.state.location.maskedLocation ? /*#__PURE__*/React.createElement("div", {
1243
+ }, "Pathname", ' ', routerState.location.maskedLocation ? /*#__PURE__*/React.createElement("div", {
1029
1244
  style: {
1030
1245
  padding: '.1rem .5rem',
1031
1246
  background: defaultTheme.warning,
@@ -1043,12 +1258,12 @@
1043
1258
  style: {
1044
1259
  opacity: 0.6
1045
1260
  }
1046
- }, router.state.location.pathname), router.state.location.maskedLocation ? /*#__PURE__*/React.createElement("code", {
1261
+ }, routerState.location.pathname), routerState.location.maskedLocation ? /*#__PURE__*/React.createElement("code", {
1047
1262
  style: {
1048
1263
  color: defaultTheme.warning,
1049
1264
  fontWeight: 'bold'
1050
1265
  }
1051
- }, router.state.location.maskedLocation.pathname) : null), /*#__PURE__*/React.createElement("div", {
1266
+ }, routerState.location.maskedLocation.pathname) : null), /*#__PURE__*/React.createElement("div", {
1052
1267
  style: {
1053
1268
  padding: '.5em',
1054
1269
  background: defaultTheme.backgroundAlt,
@@ -1093,7 +1308,7 @@
1093
1308
  isRoot: true,
1094
1309
  activeRouteId: activeRouteId,
1095
1310
  setActiveRouteId: setActiveRouteId
1096
- }) : /*#__PURE__*/React.createElement("div", null, (router.state.status === 'pending' ? router.state.pendingMatches ?? [] : router.state.matches).map((match, i) => {
1311
+ }) : /*#__PURE__*/React.createElement("div", null, (routerState.status === 'pending' ? routerState.pendingMatches ?? [] : routerState.matches).map((match, i) => {
1097
1312
  return /*#__PURE__*/React.createElement("div", {
1098
1313
  key: match.routeId || i,
1099
1314
  role: "button",
@@ -1209,8 +1424,8 @@
1209
1424
  padding: '.5em'
1210
1425
  }
1211
1426
  }, /*#__PURE__*/React.createElement(Explorer, {
1212
- value: router.state.location.search || {},
1213
- defaultExpanded: Object.keys(router.state.location.search || {}).reduce((obj, next) => {
1427
+ value: routerState.location.search || {},
1428
+ defaultExpanded: Object.keys(routerState.location.search || {}).reduce((obj, next) => {
1214
1429
  obj[next] = {};
1215
1430
  return obj;
1216
1431
  }, {})