@tanstack/router-core 0.0.1-beta.194 → 0.0.1-beta.196

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-beta.194",
4
+ "version": "0.0.1-beta.196",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -43,6 +43,7 @@
43
43
  "tiny-invariant": "^1.3.1",
44
44
  "tiny-warning": "^1.0.3",
45
45
  "@tanstack/store": "^0.0.1",
46
+ "@tanstack/history": "0.0.1-beta.194",
46
47
  "@gisatcz/cross-package-react-context": "^0.2.0"
47
48
  },
48
49
  "scripts": {
package/src/fileRoute.ts CHANGED
@@ -130,14 +130,7 @@ export class FileRoute<
130
130
  >,
131
131
  'getParentRoute' | 'path' | 'id'
132
132
  > &
133
- UpdatableRouteOptions<
134
- TLoader,
135
- TSearchSchema,
136
- TFullSearchSchema,
137
- TAllParams,
138
- TRouteContext,
139
- TContext
140
- >,
133
+ UpdatableRouteOptions<TLoader, TFullSearchSchema, TAllParams, TContext>,
141
134
  ): Route<
142
135
  TParentRoute,
143
136
  TPath,
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
+ export * from '@tanstack/history'
1
2
  export { default as invariant } from 'tiny-invariant'
2
3
  export { default as warning } from 'tiny-warning'
3
- export * from './history'
4
4
  export * from './link'
5
5
  export * from './path'
6
6
  export * from './qss'
package/src/route.ts CHANGED
@@ -24,7 +24,6 @@ export interface RegisterRouteComponent<
24
24
  TLoader = unknown,
25
25
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
26
26
  TAllParams extends AnyPathParams = AnyPathParams,
27
- TRouteContext extends Record<string, any> = AnyContext,
28
27
  TAllContext extends Record<string, any> = AnyContext,
29
28
  > {
30
29
  // RouteComponent: unknown // This is registered by the framework
@@ -32,7 +31,6 @@ export interface RegisterRouteComponent<
32
31
  export interface RegisterErrorRouteComponent<
33
32
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
34
33
  TAllParams extends AnyPathParams = AnyPathParams,
35
- TRouteContext extends Record<string, any> = AnyContext,
36
34
  TAllContext extends Record<string, any> = AnyContext,
37
35
  > {
38
36
  // ErrorRouteComponent: unknown // This is registered by the framework
@@ -40,7 +38,6 @@ export interface RegisterErrorRouteComponent<
40
38
  export interface RegisterPendingRouteComponent<
41
39
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
42
40
  TAllParams extends AnyPathParams = AnyPathParams,
43
- TRouteContext extends Record<string, any> = AnyContext,
44
41
  TAllContext extends Record<string, any> = AnyContext,
45
42
  > {
46
43
  // PendingRouteComponent: unknown // This is registered by the framework
@@ -50,7 +47,6 @@ export interface RegisterRouteProps<
50
47
  TLoader = unknown,
51
48
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
52
49
  TAllParams extends AnyPathParams = AnyPathParams,
53
- TRouteContext extends Record<string, any> = AnyContext,
54
50
  TAllContext extends Record<string, any> = AnyContext,
55
51
  > {
56
52
  // RouteProps: unknown // This is registered by the framework
@@ -58,7 +54,6 @@ export interface RegisterRouteProps<
58
54
  export interface RegisterErrorRouteProps<
59
55
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
60
56
  TAllParams extends AnyPathParams = AnyPathParams,
61
- TRouteContext extends Record<string, any> = AnyContext,
62
57
  TAllContext extends Record<string, any> = AnyContext,
63
58
  > {
64
59
  // ErrorRouteProps: unknown // This is registered by the framework
@@ -67,7 +62,6 @@ export interface RegisterErrorRouteProps<
67
62
  export interface RegisterPendingRouteProps<
68
63
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
69
64
  TAllParams extends AnyPathParams = AnyPathParams,
70
- TRouteContext extends Record<string, any> = AnyContext,
71
65
  TAllContext extends Record<string, any> = AnyContext,
72
66
  > {
73
67
  // PendingRouteProps: unknown // This is registered by the framework
@@ -77,13 +71,11 @@ export type RegisteredRouteComponent<
77
71
  TLoader = unknown,
78
72
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
79
73
  TAllParams extends AnyPathParams = AnyPathParams,
80
- TRouteContext extends Record<string, any> = AnyContext,
81
74
  TAllContext extends Record<string, any> = AnyContext,
82
75
  > = RegisterRouteComponent<
83
76
  TLoader,
84
77
  TFullSearchSchema,
85
78
  TAllParams,
86
- TRouteContext,
87
79
  TAllContext
88
80
  > extends {
89
81
  RouteComponent: infer T
@@ -94,12 +86,10 @@ export type RegisteredRouteComponent<
94
86
  export type RegisteredErrorRouteComponent<
95
87
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
96
88
  TAllParams extends AnyPathParams = AnyPathParams,
97
- TRouteContext extends Record<string, any> = AnyContext,
98
89
  TAllContext extends Record<string, any> = AnyContext,
99
90
  > = RegisterErrorRouteComponent<
100
91
  TFullSearchSchema,
101
92
  TAllParams,
102
- TRouteContext,
103
93
  TAllContext
104
94
  > extends {
105
95
  ErrorRouteComponent: infer T
@@ -110,12 +100,10 @@ export type RegisteredErrorRouteComponent<
110
100
  export type RegisteredPendingRouteComponent<
111
101
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
112
102
  TAllParams extends AnyPathParams = AnyPathParams,
113
- TRouteContext extends Record<string, any> = AnyContext,
114
103
  TAllContext extends Record<string, any> = AnyContext,
115
104
  > = RegisterPendingRouteComponent<
116
105
  TFullSearchSchema,
117
106
  TAllParams,
118
- TRouteContext,
119
107
  TAllContext
120
108
  > extends {
121
109
  PendingRouteComponent: infer T
@@ -127,13 +115,11 @@ export type RegisteredRouteProps<
127
115
  TLoader = unknown,
128
116
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
129
117
  TAllParams extends AnyPathParams = AnyPathParams,
130
- TRouteContext extends Record<string, any> = AnyContext,
131
118
  TAllContext extends Record<string, any> = AnyContext,
132
119
  > = RegisterRouteProps<
133
120
  TLoader,
134
121
  TFullSearchSchema,
135
122
  TAllParams,
136
- TRouteContext,
137
123
  TAllContext
138
124
  > extends {
139
125
  RouteProps: infer T
@@ -144,14 +130,8 @@ export type RegisteredRouteProps<
144
130
  export type RegisteredErrorRouteProps<
145
131
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
146
132
  TAllParams extends AnyPathParams = AnyPathParams,
147
- TRouteContext extends Record<string, any> = AnyContext,
148
133
  TAllContext extends Record<string, any> = AnyContext,
149
- > = RegisterRouteProps<
150
- TFullSearchSchema,
151
- TAllParams,
152
- TRouteContext,
153
- TAllContext
154
- > extends {
134
+ > = RegisterRouteProps<TFullSearchSchema, TAllParams, TAllContext> extends {
155
135
  ErrorRouteProps: infer T
156
136
  }
157
137
  ? T
@@ -160,14 +140,8 @@ export type RegisteredErrorRouteProps<
160
140
  export type RegisteredPendingRouteProps<
161
141
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
162
142
  TAllParams extends AnyPathParams = AnyPathParams,
163
- TRouteContext extends Record<string, any> = AnyContext,
164
143
  TAllContext extends Record<string, any> = AnyContext,
165
- > = RegisterRouteProps<
166
- TFullSearchSchema,
167
- TAllParams,
168
- TRouteContext,
169
- TAllContext
170
- > extends {
144
+ > = RegisterRouteProps<TFullSearchSchema, TAllParams, TAllContext> extends {
171
145
  PendingRouteProps: infer T
172
146
  }
173
147
  ? T
@@ -194,7 +168,7 @@ export type MetaOptions = keyof PickRequired<RouteMeta> extends never
194
168
  meta: RouteMeta
195
169
  }
196
170
 
197
- export type AnyRouteProps = RegisteredRouteProps<any, any, any, any, any>
171
+ export type AnyRouteProps = RegisteredRouteProps<any, any, any, any>
198
172
 
199
173
  export type RouteOptions<
200
174
  TParentRoute extends AnyRoute = AnyRoute,
@@ -221,14 +195,7 @@ export type RouteOptions<
221
195
  TRouteContext,
222
196
  TAllContext
223
197
  > &
224
- UpdatableRouteOptions<
225
- TLoader,
226
- TSearchSchema,
227
- TFullSearchSchema,
228
- TAllParams,
229
- TRouteContext,
230
- TAllContext
231
- >
198
+ UpdatableRouteOptions<TLoader, TFullSearchSchema, TAllParams, TAllContext>
232
199
 
233
200
  export type ParamsFallback<
234
201
  TPath extends string,
@@ -320,10 +287,8 @@ type BeforeLoadFn<
320
287
 
321
288
  export type UpdatableRouteOptions<
322
289
  TLoader,
323
- TSearchSchema extends Record<string, any>,
324
290
  TFullSearchSchema extends Record<string, any>,
325
291
  TAllParams extends AnyPathParams,
326
- TRouteContext extends Record<string, any>,
327
292
  TAllContext extends Record<string, any>,
328
293
  > = MetaOptions & {
329
294
  // If true, this route will be matched as case-sensitive
@@ -335,21 +300,18 @@ export type UpdatableRouteOptions<
335
300
  TLoader,
336
301
  TFullSearchSchema,
337
302
  TAllParams,
338
- TRouteContext,
339
303
  TAllContext
340
304
  >
341
305
  // The content to be rendered when the route encounters an error
342
306
  errorComponent?: RegisteredErrorRouteComponent<
343
307
  TFullSearchSchema,
344
308
  TAllParams,
345
- TRouteContext,
346
309
  TAllContext
347
310
  > //
348
311
  // If supported by your framework, the content to be rendered as the fallback content until the route is ready to render
349
312
  pendingComponent?: RegisteredPendingRouteComponent<
350
313
  TFullSearchSchema,
351
314
  TAllParams,
352
- TRouteContext,
353
315
  TAllContext
354
316
  >
355
317
  // Filter functions that can manipulate search params *before* they are passed to links and navigate
@@ -438,7 +400,7 @@ export interface LoaderContext<
438
400
  abortController: AbortController
439
401
  preload: boolean
440
402
  params: TAllParams
441
- context: DeepMergeAll<[TAllContext, TLoaderContext, TRouteContext]>
403
+ context: Expand<DeepMergeAll<[TAllContext, TLoaderContext, TRouteContext]>>
442
404
  }
443
405
 
444
406
  export type SearchFilter<T, U = T> = (prev: T) => U
@@ -635,10 +597,8 @@ export class Route<
635
597
  > &
636
598
  UpdatableRouteOptions<
637
599
  TLoader,
638
- TSearchSchema,
639
600
  TFullSearchSchema,
640
601
  TAllParams,
641
- TRouteContext,
642
602
  TAllContext
643
603
  >,
644
604
  ) {
@@ -743,10 +703,8 @@ export class Route<
743
703
  update = (
744
704
  options: UpdatableRouteOptions<
745
705
  TLoader,
746
- TSearchSchema,
747
706
  TFullSearchSchema,
748
707
  TAllParams,
749
- TRouteContext,
750
708
  TAllContext
751
709
  >,
752
710
  ) => {
package/src/routeInfo.ts CHANGED
@@ -61,9 +61,7 @@ export type RoutePaths<TRouteTree extends AnyRoute> =
61
61
 
62
62
  export type FullSearchSchema<TRouteTree extends AnyRoute> = Partial<
63
63
  Expand<
64
- UnionToIntersection<
65
- ParseRoute<TRouteTree>['types']['fullSearchSchema']
66
- > & {}
64
+ UnionToIntersection<ParseRoute<TRouteTree>['types']['fullSearchSchema']>
67
65
  >
68
66
  >
69
67
 
package/src/router.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { Store } from '@tanstack/store'
2
2
  import invariant from 'tiny-invariant'
3
+ import {
4
+ createBrowserHistory,
5
+ createMemoryHistory,
6
+ HistoryLocation,
7
+ HistoryState,
8
+ RouterHistory,
9
+ } from '@tanstack/history'
3
10
 
4
11
  //
5
12
 
@@ -53,13 +60,6 @@ import {
53
60
  partialDeepEqual,
54
61
  NonNullableUpdater,
55
62
  } from './utils'
56
- import {
57
- createBrowserHistory,
58
- createMemoryHistory,
59
- HistoryLocation,
60
- HistoryState,
61
- RouterHistory,
62
- } from './history'
63
63
 
64
64
  //
65
65
 
@@ -69,6 +69,13 @@ declare global {
69
69
  }
70
70
  }
71
71
 
72
+ declare module '@tanstack/history' {
73
+ interface HistoryState {
74
+ __tempLocation?: HistoryLocation
75
+ __tempKey?: string
76
+ }
77
+ }
78
+
72
79
  export interface Register {
73
80
  // router: Router
74
81
  }
@@ -163,19 +170,16 @@ export interface RouterOptions<
163
170
  unknown,
164
171
  AnySearchSchema,
165
172
  AnyPathParams,
166
- AnyContext,
167
173
  AnyContext
168
174
  >
169
175
  defaultErrorComponent?: RegisteredErrorRouteComponent<
170
176
  AnySearchSchema,
171
177
  AnyPathParams,
172
- AnyContext,
173
178
  AnyContext
174
179
  >
175
180
  defaultPendingComponent?: RegisteredPendingRouteComponent<
176
181
  AnySearchSchema,
177
182
  AnyPathParams,
178
- AnyContext,
179
183
  AnyContext
180
184
  >
181
185
  defaultMaxAge?: number
package/src/utils.ts CHANGED
@@ -19,6 +19,7 @@ export type PickRequired<T> = {
19
19
  [K in keyof T as undefined extends T[K] ? never : K]: T[K]
20
20
  }
21
21
 
22
+ // export type Expand<T> = T
22
23
  export type Expand<T> = T extends object
23
24
  ? T extends infer O
24
25
  ? { [K in keyof O]: O[K] }
@@ -1,228 +0,0 @@
1
- /**
2
- * @tanstack/router-core/src/index.ts
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- // While the public API was clearly inspired by the "history" npm package,
16
- // This implementation attempts to be more lightweight by
17
- // making assumptions about the way TanStack Router works
18
-
19
- const pushStateEvent = 'pushstate';
20
- const popStateEvent = 'popstate';
21
- const beforeUnloadEvent = 'beforeunload';
22
- const beforeUnloadListener = event => {
23
- event.preventDefault();
24
- // @ts-ignore
25
- return event.returnValue = '';
26
- };
27
- const stopBlocking = () => {
28
- removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
29
- capture: true
30
- });
31
- };
32
- function createHistory(opts) {
33
- let location = opts.getLocation();
34
- let unsub = () => {};
35
- let subscribers = new Set();
36
- let blockers = [];
37
- let queue = [];
38
- const tryFlush = () => {
39
- if (blockers.length) {
40
- blockers[0]?.(tryFlush, () => {
41
- blockers = [];
42
- stopBlocking();
43
- });
44
- return;
45
- }
46
- while (queue.length) {
47
- queue.shift()?.();
48
- }
49
- if (!opts.subscriber) {
50
- onUpdate();
51
- }
52
- };
53
- const queueTask = task => {
54
- queue.push(task);
55
- tryFlush();
56
- };
57
- const onUpdate = () => {
58
- location = opts.getLocation();
59
- subscribers.forEach(subscriber => subscriber());
60
- };
61
- return {
62
- get location() {
63
- return location;
64
- },
65
- subscribe: cb => {
66
- if (subscribers.size === 0) {
67
- unsub = typeof opts.subscriber === 'function' ? opts.subscriber(onUpdate) : () => {};
68
- }
69
- subscribers.add(cb);
70
- return () => {
71
- subscribers.delete(cb);
72
- if (subscribers.size === 0) {
73
- unsub();
74
- }
75
- };
76
- },
77
- push: (path, state) => {
78
- assignKey(state);
79
- queueTask(() => {
80
- opts.pushState(path, state);
81
- });
82
- },
83
- replace: (path, state) => {
84
- assignKey(state);
85
- queueTask(() => {
86
- opts.replaceState(path, state);
87
- });
88
- },
89
- go: index => {
90
- queueTask(() => {
91
- opts.go(index);
92
- });
93
- },
94
- back: () => {
95
- queueTask(() => {
96
- opts.back();
97
- });
98
- },
99
- forward: () => {
100
- queueTask(() => {
101
- opts.forward();
102
- });
103
- },
104
- createHref: str => opts.createHref(str),
105
- block: cb => {
106
- blockers.push(cb);
107
- if (blockers.length === 1) {
108
- addEventListener(beforeUnloadEvent, beforeUnloadListener, {
109
- capture: true
110
- });
111
- }
112
- return () => {
113
- blockers = blockers.filter(b => b !== cb);
114
- if (!blockers.length) {
115
- stopBlocking();
116
- }
117
- };
118
- }
119
- };
120
- }
121
- function assignKey(state) {
122
- state.key = createRandomKey();
123
- // if (state.__actualLocation) {
124
- // state.__actualLocation.state = {
125
- // ...state.__actualLocation.state,
126
- // key,
127
- // }
128
- // }
129
- }
130
-
131
- function createBrowserHistory(opts) {
132
- const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
133
- const createHref = opts?.createHref ?? (path => path);
134
- const getLocation = () => parseLocation(getHref(), window.history.state);
135
- return createHistory({
136
- getLocation,
137
- subscriber: onUpdate => {
138
- window.addEventListener(pushStateEvent, onUpdate);
139
- window.addEventListener(popStateEvent, onUpdate);
140
- var pushState = window.history.pushState;
141
- window.history.pushState = function () {
142
- let res = pushState.apply(history, arguments);
143
- onUpdate();
144
- return res;
145
- };
146
- var replaceState = window.history.replaceState;
147
- window.history.replaceState = function () {
148
- let res = replaceState.apply(history, arguments);
149
- onUpdate();
150
- return res;
151
- };
152
- return () => {
153
- window.history.pushState = pushState;
154
- window.history.replaceState = replaceState;
155
- window.removeEventListener(pushStateEvent, onUpdate);
156
- window.removeEventListener(popStateEvent, onUpdate);
157
- };
158
- },
159
- pushState: (path, state) => {
160
- window.history.pushState(state, '', createHref(path));
161
- },
162
- replaceState: (path, state) => {
163
- window.history.replaceState(state, '', createHref(path));
164
- },
165
- back: () => window.history.back(),
166
- forward: () => window.history.forward(),
167
- go: n => window.history.go(n),
168
- createHref: path => createHref(path)
169
- });
170
- }
171
- function createHashHistory() {
172
- return createBrowserHistory({
173
- getHref: () => window.location.hash.substring(1),
174
- createHref: path => `#${path}`
175
- });
176
- }
177
- function createMemoryHistory(opts = {
178
- initialEntries: ['/']
179
- }) {
180
- const entries = opts.initialEntries;
181
- let index = opts.initialIndex ?? entries.length - 1;
182
- let currentState = {
183
- key: createRandomKey()
184
- };
185
- const getLocation = () => parseLocation(entries[index], currentState);
186
- return createHistory({
187
- getLocation,
188
- subscriber: false,
189
- pushState: (path, state) => {
190
- currentState = state;
191
- entries.push(path);
192
- index++;
193
- },
194
- replaceState: (path, state) => {
195
- currentState = state;
196
- entries[index] = path;
197
- },
198
- back: () => {
199
- index--;
200
- },
201
- forward: () => {
202
- index = Math.min(index + 1, entries.length - 1);
203
- },
204
- go: n => window.history.go(n),
205
- createHref: path => path
206
- });
207
- }
208
- function parseLocation(href, state) {
209
- let hashIndex = href.indexOf('#');
210
- let searchIndex = href.indexOf('?');
211
- return {
212
- href,
213
- pathname: href.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : href.length),
214
- hash: hashIndex > -1 ? href.substring(hashIndex) : '',
215
- search: searchIndex > -1 ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) : '',
216
- state: state || {}
217
- };
218
- }
219
-
220
- // Thanks co-pilot!
221
- function createRandomKey() {
222
- return (Math.random() + 1).toString(36).substring(7);
223
- }
224
-
225
- exports.createBrowserHistory = createBrowserHistory;
226
- exports.createHashHistory = createHashHistory;
227
- exports.createMemoryHistory = createMemoryHistory;
228
- //# sourceMappingURL=history.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"history.js","sources":["../../src/history.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blockerFn: BlockerFn) => () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n __tempLocation?: HistoryLocation\n __tempKey?: string\n}\n\ntype BlockerFn = (retry: () => void, cancel: () => void) => void\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nfunction createHistory(opts: {\n getLocation: () => HistoryLocation\n subscriber: false | ((onUpdate: () => void) => () => void)\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n}): RouterHistory {\n let location = opts.getLocation()\n let unsub = () => {}\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const tryFlush = () => {\n if (blockers.length) {\n blockers[0]?.(tryFlush, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n\n if (!opts.subscriber) {\n onUpdate()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryFlush()\n }\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n if (subscribers.size === 0) {\n unsub =\n typeof opts.subscriber === 'function'\n ? opts.subscriber(onUpdate)\n : () => {}\n }\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n if (subscribers.size === 0) {\n unsub()\n }\n }\n },\n push: (path: string, state: any) => {\n assignKey(state)\n queueTask(() => {\n opts.pushState(path, state)\n })\n },\n replace: (path: string, state: any) => {\n assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n }\n}\n\nfunction assignKey(state: HistoryState) {\n state.key = createRandomKey()\n // if (state.__actualLocation) {\n // state.__actualLocation.state = {\n // ...state.__actualLocation.state,\n // key,\n // }\n // }\n}\n\nexport function createBrowserHistory(opts?: {\n getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n const getLocation = () => parseLocation(getHref(), window.history.state)\n\n return createHistory({\n getLocation,\n subscriber: (onUpdate) => {\n window.addEventListener(pushStateEvent, onUpdate)\n window.addEventListener(popStateEvent, onUpdate)\n\n var pushState = window.history.pushState\n window.history.pushState = function () {\n let res = pushState.apply(history, arguments as any)\n onUpdate()\n return res\n }\n var replaceState = window.history.replaceState\n window.history.replaceState = function () {\n let res = replaceState.apply(history, arguments as any)\n onUpdate()\n return res\n }\n\n return () => {\n window.history.pushState = pushState\n window.history.replaceState = replaceState\n window.removeEventListener(pushStateEvent, onUpdate)\n window.removeEventListener(popStateEvent, onUpdate)\n }\n },\n pushState: (path, state) => {\n window.history.pushState(state, '', createHref(path))\n },\n replaceState: (path, state) => {\n window.history.replaceState(state, '', createHref(path))\n },\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n })\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseLocation(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n subscriber: false,\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","unsub","subscribers","Set","blockers","queue","tryFlush","length","shift","subscriber","onUpdate","queueTask","task","push","forEach","subscribe","cb","size","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","parseLocation","history","res","apply","arguments","n","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","href","hashIndex","indexOf","searchIndex","slice","undefined","random","toString"],"mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;;AAiCA,MAAMA,cAAc,GAAG,WAAW,CAAA;AAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;AAChC,MAAMC,iBAAiB,GAAG,cAAc,CAAA;AAExC,MAAMC,oBAAoB,GAAIC,KAAY,IAAK;EAC7CA,KAAK,CAACC,cAAc,EAAE,CAAA;AACtB;AACA,EAAA,OAAQD,KAAK,CAACE,WAAW,GAAG,EAAE,CAAA;AAChC,CAAC,CAAA;AAED,MAAMC,YAAY,GAAGA,MAAM;AACzBC,EAAAA,mBAAmB,CAACN,iBAAiB,EAAEC,oBAAoB,EAAE;AAC3DM,IAAAA,OAAO,EAAE,IAAA;AACX,GAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,SAASC,aAAaA,CAACC,IAStB,EAAiB;AAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;AACjC,EAAA,IAAIC,KAAK,GAAGA,MAAM,EAAE,CAAA;AACpB,EAAA,IAAIC,WAAW,GAAG,IAAIC,GAAG,EAAc,CAAA;EACvC,IAAIC,QAAqB,GAAG,EAAE,CAAA;EAC9B,IAAIC,KAAqB,GAAG,EAAE,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,EACJoB,WAAW,GAAG,CAAC,CAAC,GACZH,IAAI,CAACI,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGI,SAAS,GAAGJ,SAAS,CAAC,GACjE,EAAE;IACRvC,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASgB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACoB,IAAI,CAACQ,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACf,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;;;"}
@@ -1,36 +0,0 @@
1
- export interface RouterHistory {
2
- location: HistoryLocation;
3
- subscribe: (cb: () => void) => () => void;
4
- push: (path: string, state?: any) => void;
5
- replace: (path: string, state?: any) => void;
6
- go: (index: number) => void;
7
- back: () => void;
8
- forward: () => void;
9
- createHref: (href: string) => string;
10
- block: (blockerFn: BlockerFn) => () => void;
11
- }
12
- export interface HistoryLocation extends ParsedPath {
13
- state: HistoryState;
14
- }
15
- export interface ParsedPath {
16
- href: string;
17
- pathname: string;
18
- search: string;
19
- hash: string;
20
- }
21
- export interface HistoryState {
22
- key: string;
23
- __tempLocation?: HistoryLocation;
24
- __tempKey?: string;
25
- }
26
- type BlockerFn = (retry: () => void, cancel: () => void) => void;
27
- export declare function createBrowserHistory(opts?: {
28
- getHref?: () => string;
29
- createHref?: (path: string) => string;
30
- }): RouterHistory;
31
- export declare function createHashHistory(): RouterHistory;
32
- export declare function createMemoryHistory(opts?: {
33
- initialEntries: string[];
34
- initialIndex?: number;
35
- }): RouterHistory;
36
- export {};