@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/build/cjs/fileRoute.js.map +1 -1
- package/build/cjs/index.js +7 -4
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js +1 -1
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/utils.js +2 -0
- package/build/cjs/utils.js.map +1 -1
- package/build/esm/index.js +4 -210
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +194 -189
- package/build/types/index.d.ts +1 -1
- package/build/types/route.d.ts +21 -21
- package/build/types/routeInfo.d.ts +1 -1
- package/build/types/router.d.ts +10 -4
- package/build/umd/index.development.js +2102 -2019
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +21 -11
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -1
- package/src/fileRoute.ts +1 -8
- package/src/index.ts +1 -1
- package/src/route.ts +5 -47
- package/src/routeInfo.ts +1 -3
- package/src/router.ts +14 -10
- package/src/utils.ts +1 -0
- package/build/cjs/history.js +0 -228
- package/build/cjs/history.js.map +0 -1
- package/build/types/history.d.ts +0 -36
- package/src/history.ts +0 -300
package/src/history.ts
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
// While the public API was clearly inspired by the "history" npm package,
|
|
2
|
-
// This implementation attempts to be more lightweight by
|
|
3
|
-
// making assumptions about the way TanStack Router works
|
|
4
|
-
|
|
5
|
-
export interface RouterHistory {
|
|
6
|
-
location: HistoryLocation
|
|
7
|
-
subscribe: (cb: () => void) => () => void
|
|
8
|
-
push: (path: string, state?: any) => void
|
|
9
|
-
replace: (path: string, state?: any) => void
|
|
10
|
-
go: (index: number) => void
|
|
11
|
-
back: () => void
|
|
12
|
-
forward: () => void
|
|
13
|
-
createHref: (href: string) => string
|
|
14
|
-
block: (blockerFn: BlockerFn) => () => void
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface HistoryLocation extends ParsedPath {
|
|
18
|
-
state: HistoryState
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface ParsedPath {
|
|
22
|
-
href: string
|
|
23
|
-
pathname: string
|
|
24
|
-
search: string
|
|
25
|
-
hash: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface HistoryState {
|
|
29
|
-
key: string
|
|
30
|
-
__tempLocation?: HistoryLocation
|
|
31
|
-
__tempKey?: string
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
type BlockerFn = (retry: () => void, cancel: () => void) => void
|
|
35
|
-
|
|
36
|
-
const pushStateEvent = 'pushstate'
|
|
37
|
-
const popStateEvent = 'popstate'
|
|
38
|
-
const beforeUnloadEvent = 'beforeunload'
|
|
39
|
-
|
|
40
|
-
const beforeUnloadListener = (event: Event) => {
|
|
41
|
-
event.preventDefault()
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
return (event.returnValue = '')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const stopBlocking = () => {
|
|
47
|
-
removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
48
|
-
capture: true,
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function createHistory(opts: {
|
|
53
|
-
getLocation: () => HistoryLocation
|
|
54
|
-
subscriber: false | ((onUpdate: () => void) => () => void)
|
|
55
|
-
pushState: (path: string, state: any) => void
|
|
56
|
-
replaceState: (path: string, state: any) => void
|
|
57
|
-
go: (n: number) => void
|
|
58
|
-
back: () => void
|
|
59
|
-
forward: () => void
|
|
60
|
-
createHref: (path: string) => string
|
|
61
|
-
}): RouterHistory {
|
|
62
|
-
let location = opts.getLocation()
|
|
63
|
-
let unsub = () => {}
|
|
64
|
-
let subscribers = new Set<() => void>()
|
|
65
|
-
let blockers: BlockerFn[] = []
|
|
66
|
-
let queue: (() => void)[] = []
|
|
67
|
-
|
|
68
|
-
const tryFlush = () => {
|
|
69
|
-
if (blockers.length) {
|
|
70
|
-
blockers[0]?.(tryFlush, () => {
|
|
71
|
-
blockers = []
|
|
72
|
-
stopBlocking()
|
|
73
|
-
})
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
while (queue.length) {
|
|
78
|
-
queue.shift()?.()
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (!opts.subscriber) {
|
|
82
|
-
onUpdate()
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const queueTask = (task: () => void) => {
|
|
87
|
-
queue.push(task)
|
|
88
|
-
tryFlush()
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const onUpdate = () => {
|
|
92
|
-
location = opts.getLocation()
|
|
93
|
-
subscribers.forEach((subscriber) => subscriber())
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
get location() {
|
|
98
|
-
return location
|
|
99
|
-
},
|
|
100
|
-
subscribe: (cb: () => void) => {
|
|
101
|
-
if (subscribers.size === 0) {
|
|
102
|
-
unsub =
|
|
103
|
-
typeof opts.subscriber === 'function'
|
|
104
|
-
? opts.subscriber(onUpdate)
|
|
105
|
-
: () => {}
|
|
106
|
-
}
|
|
107
|
-
subscribers.add(cb)
|
|
108
|
-
|
|
109
|
-
return () => {
|
|
110
|
-
subscribers.delete(cb)
|
|
111
|
-
if (subscribers.size === 0) {
|
|
112
|
-
unsub()
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
push: (path: string, state: any) => {
|
|
117
|
-
assignKey(state)
|
|
118
|
-
queueTask(() => {
|
|
119
|
-
opts.pushState(path, state)
|
|
120
|
-
})
|
|
121
|
-
},
|
|
122
|
-
replace: (path: string, state: any) => {
|
|
123
|
-
assignKey(state)
|
|
124
|
-
queueTask(() => {
|
|
125
|
-
opts.replaceState(path, state)
|
|
126
|
-
})
|
|
127
|
-
},
|
|
128
|
-
go: (index) => {
|
|
129
|
-
queueTask(() => {
|
|
130
|
-
opts.go(index)
|
|
131
|
-
})
|
|
132
|
-
},
|
|
133
|
-
back: () => {
|
|
134
|
-
queueTask(() => {
|
|
135
|
-
opts.back()
|
|
136
|
-
})
|
|
137
|
-
},
|
|
138
|
-
forward: () => {
|
|
139
|
-
queueTask(() => {
|
|
140
|
-
opts.forward()
|
|
141
|
-
})
|
|
142
|
-
},
|
|
143
|
-
createHref: (str) => opts.createHref(str),
|
|
144
|
-
block: (cb) => {
|
|
145
|
-
blockers.push(cb)
|
|
146
|
-
|
|
147
|
-
if (blockers.length === 1) {
|
|
148
|
-
addEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
149
|
-
capture: true,
|
|
150
|
-
})
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return () => {
|
|
154
|
-
blockers = blockers.filter((b) => b !== cb)
|
|
155
|
-
|
|
156
|
-
if (!blockers.length) {
|
|
157
|
-
stopBlocking()
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
},
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function assignKey(state: HistoryState) {
|
|
165
|
-
state.key = createRandomKey()
|
|
166
|
-
// if (state.__actualLocation) {
|
|
167
|
-
// state.__actualLocation.state = {
|
|
168
|
-
// ...state.__actualLocation.state,
|
|
169
|
-
// key,
|
|
170
|
-
// }
|
|
171
|
-
// }
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function createBrowserHistory(opts?: {
|
|
175
|
-
getHref?: () => string
|
|
176
|
-
createHref?: (path: string) => string
|
|
177
|
-
}): RouterHistory {
|
|
178
|
-
const getHref =
|
|
179
|
-
opts?.getHref ??
|
|
180
|
-
(() =>
|
|
181
|
-
`${window.location.pathname}${window.location.search}${window.location.hash}`)
|
|
182
|
-
|
|
183
|
-
const createHref = opts?.createHref ?? ((path) => path)
|
|
184
|
-
|
|
185
|
-
const getLocation = () => parseLocation(getHref(), window.history.state)
|
|
186
|
-
|
|
187
|
-
return createHistory({
|
|
188
|
-
getLocation,
|
|
189
|
-
subscriber: (onUpdate) => {
|
|
190
|
-
window.addEventListener(pushStateEvent, onUpdate)
|
|
191
|
-
window.addEventListener(popStateEvent, onUpdate)
|
|
192
|
-
|
|
193
|
-
var pushState = window.history.pushState
|
|
194
|
-
window.history.pushState = function () {
|
|
195
|
-
let res = pushState.apply(history, arguments as any)
|
|
196
|
-
onUpdate()
|
|
197
|
-
return res
|
|
198
|
-
}
|
|
199
|
-
var replaceState = window.history.replaceState
|
|
200
|
-
window.history.replaceState = function () {
|
|
201
|
-
let res = replaceState.apply(history, arguments as any)
|
|
202
|
-
onUpdate()
|
|
203
|
-
return res
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return () => {
|
|
207
|
-
window.history.pushState = pushState
|
|
208
|
-
window.history.replaceState = replaceState
|
|
209
|
-
window.removeEventListener(pushStateEvent, onUpdate)
|
|
210
|
-
window.removeEventListener(popStateEvent, onUpdate)
|
|
211
|
-
}
|
|
212
|
-
},
|
|
213
|
-
pushState: (path, state) => {
|
|
214
|
-
window.history.pushState(state, '', createHref(path))
|
|
215
|
-
},
|
|
216
|
-
replaceState: (path, state) => {
|
|
217
|
-
window.history.replaceState(state, '', createHref(path))
|
|
218
|
-
},
|
|
219
|
-
back: () => window.history.back(),
|
|
220
|
-
forward: () => window.history.forward(),
|
|
221
|
-
go: (n) => window.history.go(n),
|
|
222
|
-
createHref: (path) => createHref(path),
|
|
223
|
-
})
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export function createHashHistory(): RouterHistory {
|
|
227
|
-
return createBrowserHistory({
|
|
228
|
-
getHref: () => window.location.hash.substring(1),
|
|
229
|
-
createHref: (path) => `#${path}`,
|
|
230
|
-
})
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export function createMemoryHistory(
|
|
234
|
-
opts: {
|
|
235
|
-
initialEntries: string[]
|
|
236
|
-
initialIndex?: number
|
|
237
|
-
} = {
|
|
238
|
-
initialEntries: ['/'],
|
|
239
|
-
},
|
|
240
|
-
): RouterHistory {
|
|
241
|
-
const entries = opts.initialEntries
|
|
242
|
-
let index = opts.initialIndex ?? entries.length - 1
|
|
243
|
-
let currentState = {
|
|
244
|
-
key: createRandomKey(),
|
|
245
|
-
} as HistoryState
|
|
246
|
-
|
|
247
|
-
const getLocation = () => parseLocation(entries[index]!, currentState)
|
|
248
|
-
|
|
249
|
-
return createHistory({
|
|
250
|
-
getLocation,
|
|
251
|
-
subscriber: false,
|
|
252
|
-
pushState: (path, state) => {
|
|
253
|
-
currentState = state
|
|
254
|
-
entries.push(path)
|
|
255
|
-
index++
|
|
256
|
-
},
|
|
257
|
-
replaceState: (path, state) => {
|
|
258
|
-
currentState = state
|
|
259
|
-
entries[index] = path
|
|
260
|
-
},
|
|
261
|
-
back: () => {
|
|
262
|
-
index--
|
|
263
|
-
},
|
|
264
|
-
forward: () => {
|
|
265
|
-
index = Math.min(index + 1, entries.length - 1)
|
|
266
|
-
},
|
|
267
|
-
go: (n) => window.history.go(n),
|
|
268
|
-
createHref: (path) => path,
|
|
269
|
-
})
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function parseLocation(href: string, state: HistoryState): HistoryLocation {
|
|
273
|
-
let hashIndex = href.indexOf('#')
|
|
274
|
-
let searchIndex = href.indexOf('?')
|
|
275
|
-
|
|
276
|
-
return {
|
|
277
|
-
href,
|
|
278
|
-
pathname: href.substring(
|
|
279
|
-
0,
|
|
280
|
-
hashIndex > 0
|
|
281
|
-
? searchIndex > 0
|
|
282
|
-
? Math.min(hashIndex, searchIndex)
|
|
283
|
-
: hashIndex
|
|
284
|
-
: searchIndex > 0
|
|
285
|
-
? searchIndex
|
|
286
|
-
: href.length,
|
|
287
|
-
),
|
|
288
|
-
hash: hashIndex > -1 ? href.substring(hashIndex) : '',
|
|
289
|
-
search:
|
|
290
|
-
searchIndex > -1
|
|
291
|
-
? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)
|
|
292
|
-
: '',
|
|
293
|
-
state: state || {},
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Thanks co-pilot!
|
|
298
|
-
function createRandomKey() {
|
|
299
|
-
return (Math.random() + 1).toString(36).substring(7)
|
|
300
|
-
}
|