-
-
Notifications
You must be signed in to change notification settings - Fork 760
/
Copy pathindex.ts
636 lines (568 loc) · 17.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// While the public API was clearly inspired by the "history" npm package,
// This implementation attempts to be more lightweight by
// making assumptions about the way TanStack Router works
export interface NavigateOptions {
ignoreBlocker?: boolean
}
type SubscriberHistoryAction =
| {
type: Exclude<HistoryAction, 'GO'>
}
| {
type: 'GO'
index: number
}
type SubscriberArgs = {
location: HistoryLocation
action: SubscriberHistoryAction
}
export interface RouterHistory {
location: HistoryLocation
length: number
subscribers: Set<(opts: SubscriberArgs) => void>
subscribe: (cb: (opts: SubscriberArgs) => void) => () => void
push: (path: string, state?: any, navigateOpts?: NavigateOptions) => void
replace: (path: string, state?: any, navigateOpts?: NavigateOptions) => void
go: (index: number, navigateOpts?: NavigateOptions) => void
back: (navigateOpts?: NavigateOptions) => void
forward: (navigateOpts?: NavigateOptions) => void
canGoBack: () => boolean
createHref: (href: string) => string
block: (blocker: NavigationBlocker) => () => void
flush: () => void
destroy: () => void
notify: (action: SubscriberHistoryAction) => void
_ignoreSubscribers?: boolean
}
export interface HistoryLocation extends ParsedPath {
state: ParsedHistoryState
}
export interface ParsedPath {
href: string
pathname: string
search: string
hash: string
}
export interface HistoryState {}
export type ParsedHistoryState = HistoryState & {
key?: string
__TSR_index: number
}
type ShouldAllowNavigation = any
export type HistoryAction = 'PUSH' | 'REPLACE' | 'FORWARD' | 'BACK' | 'GO'
export type BlockerFnArgs = {
currentLocation: HistoryLocation
nextLocation: HistoryLocation
action: HistoryAction
}
export type BlockerFn = (
args: BlockerFnArgs,
) => Promise<ShouldAllowNavigation> | ShouldAllowNavigation
export type NavigationBlocker = {
blockerFn: BlockerFn
enableBeforeUnload?: (() => boolean) | boolean
}
type TryNavigateArgs = {
task: () => void
type: 'PUSH' | 'REPLACE' | 'BACK' | 'FORWARD' | 'GO'
navigateOpts?: NavigateOptions
} & (
| {
type: 'PUSH' | 'REPLACE'
path: string
state: any
}
| {
type: 'BACK' | 'FORWARD' | 'GO'
}
)
const stateIndexKey = '__TSR_index'
const popStateEvent = 'popstate'
const beforeUnloadEvent = 'beforeunload'
export function createHistory(opts: {
getLocation: () => HistoryLocation
getLength: () => number
pushState: (path: string, state: any) => void
replaceState: (path: string, state: any) => void
go: (n: number) => void
back: (ignoreBlocker: boolean) => void
forward: (ignoreBlocker: boolean) => void
createHref: (path: string) => string
flush?: () => void
destroy?: () => void
onBlocked?: () => void
getBlockers?: () => Array<NavigationBlocker>
setBlockers?: (blockers: Array<NavigationBlocker>) => void
// Avoid notifying on forward/back/go, used for browser history as we already get notified by the popstate event
notifyOnIndexChange?: boolean
}): RouterHistory {
let location = opts.getLocation()
const subscribers = new Set<(opts: SubscriberArgs) => void>()
const notify = (action: SubscriberHistoryAction) => {
location = opts.getLocation()
subscribers.forEach((subscriber) => subscriber({ location, action }))
}
const handleIndexChange = (action: SubscriberHistoryAction) => {
if (opts.notifyOnIndexChange ?? true) notify(action)
else location = opts.getLocation()
}
const tryNavigation = async ({
task,
navigateOpts,
...actionInfo
}: TryNavigateArgs) => {
const ignoreBlocker = navigateOpts?.ignoreBlocker ?? false
if (ignoreBlocker) {
task()
return
}
const blockers = opts.getBlockers?.() ?? []
const isPushOrReplace =
actionInfo.type === 'PUSH' || actionInfo.type === 'REPLACE'
if (typeof document !== 'undefined' && blockers.length && isPushOrReplace) {
for (const blocker of blockers) {
const nextLocation = parseHref(actionInfo.path, actionInfo.state)
const isBlocked = await blocker.blockerFn({
currentLocation: location,
nextLocation,
action: actionInfo.type,
})
if (isBlocked) {
opts.onBlocked?.()
return
}
}
}
task()
}
return {
get location() {
return location
},
get length() {
return opts.getLength()
},
subscribers,
subscribe: (cb: (opts: SubscriberArgs) => void) => {
subscribers.add(cb)
return () => {
subscribers.delete(cb)
}
},
push: (path, state, navigateOpts) => {
const currentIndex = location.state[stateIndexKey]
state = assignKeyAndIndex(currentIndex + 1, state)
tryNavigation({
task: () => {
opts.pushState(path, state)
notify({ type: 'PUSH' })
},
navigateOpts,
type: 'PUSH',
path,
state,
})
},
replace: (path, state, navigateOpts) => {
const currentIndex = location.state[stateIndexKey]
state = assignKeyAndIndex(currentIndex, state)
tryNavigation({
task: () => {
opts.replaceState(path, state)
notify({ type: 'REPLACE' })
},
navigateOpts,
type: 'REPLACE',
path,
state,
})
},
go: (index, navigateOpts) => {
tryNavigation({
task: () => {
opts.go(index)
handleIndexChange({ type: 'GO', index })
},
navigateOpts,
type: 'GO',
})
},
back: (navigateOpts) => {
tryNavigation({
task: () => {
opts.back(navigateOpts?.ignoreBlocker ?? false)
handleIndexChange({ type: 'BACK' })
},
navigateOpts,
type: 'BACK',
})
},
forward: (navigateOpts) => {
tryNavigation({
task: () => {
opts.forward(navigateOpts?.ignoreBlocker ?? false)
handleIndexChange({ type: 'FORWARD' })
},
navigateOpts,
type: 'FORWARD',
})
},
canGoBack: () => location.state[stateIndexKey] !== 0,
createHref: (str) => opts.createHref(str),
block: (blocker) => {
if (!opts.setBlockers) return () => {}
const blockers = opts.getBlockers?.() ?? []
opts.setBlockers([...blockers, blocker])
return () => {
const blockers = opts.getBlockers?.() ?? []
opts.setBlockers?.(blockers.filter((b) => b !== blocker))
}
},
flush: () => opts.flush?.(),
destroy: () => opts.destroy?.(),
notify,
}
}
function assignKeyAndIndex(index: number, state: HistoryState | undefined) {
if (!state) {
state = {} as HistoryState
}
return {
...state,
key: createRandomKey(),
[stateIndexKey]: index,
} as ParsedHistoryState
}
/**
* Creates a history object that can be used to interact with the browser's
* navigation. This is a lightweight API wrapping the browser's native methods.
* It is designed to work with TanStack Router, but could be used as a standalone API as well.
* IMPORTANT: This API implements history throttling via a microtask to prevent
* excessive calls to the history API. In some browsers, calling history.pushState or
* history.replaceState in quick succession can cause the browser to ignore subsequent
* calls. This API smooths out those differences and ensures that your application
* state will *eventually* match the browser state. In most cases, this is not a problem,
* but if you need to ensure that the browser state is up to date, you can use the
* `history.flush` method to immediately flush all pending state changes to the browser URL.
* @param opts
* @param opts.getHref A function that returns the current href (path + search + hash)
* @param opts.createHref A function that takes a path and returns a href (path + search + hash)
* @returns A history instance
*/
export function createBrowserHistory(opts?: {
parseLocation?: () => HistoryLocation
createHref?: (path: string) => string
window?: any
}): RouterHistory {
const win =
opts?.window ??
(typeof document !== 'undefined' ? window : (undefined as any))
const originalPushState = win.history.pushState
const originalReplaceState = win.history.replaceState
let blockers: Array<NavigationBlocker> = []
const _getBlockers = () => blockers
const _setBlockers = (newBlockers: Array<NavigationBlocker>) =>
(blockers = newBlockers)
const createHref = opts?.createHref ?? ((path) => path)
const parseLocation =
opts?.parseLocation ??
(() =>
parseHref(
`${win.location.pathname}${win.location.search}${win.location.hash}`,
win.history.state,
))
// Ensure there is always a key to start
if (!win.history.state?.key) {
win.history.replaceState(
{
[stateIndexKey]: 0,
key: createRandomKey(),
},
'',
)
}
let currentLocation = parseLocation()
let rollbackLocation: HistoryLocation | undefined
let nextPopIsGo = false
let ignoreNextPop = false
let skipBlockerNextPop = false
let ignoreNextBeforeUnload = false
const getLocation = () => currentLocation
let next:
| undefined
| {
// This is the latest location that we were attempting to push/replace
href: string
// This is the latest state that we were attempting to push/replace
state: any
// This is the latest type that we were attempting to push/replace
isPush: boolean
}
// We need to track the current scheduled update to prevent
// multiple updates from being scheduled at the same time.
let scheduled: Promise<void> | undefined
// This function flushes the next update to the browser history
const flush = () => {
if (!next) {
return
}
// We need to ignore any updates to the subscribers while we update the browser history
history._ignoreSubscribers = true
// Update the browser history
;(next.isPush ? win.history.pushState : win.history.replaceState)(
next.state,
'',
next.href,
)
// Stop ignoring subscriber updates
history._ignoreSubscribers = false
// Reset the nextIsPush flag and clear the scheduled update
next = undefined
scheduled = undefined
rollbackLocation = undefined
}
// This function queues up a call to update the browser history
const queueHistoryAction = (
type: 'push' | 'replace',
destHref: string,
state: any,
) => {
const href = createHref(destHref)
if (!scheduled) {
rollbackLocation = currentLocation
}
// Update the location in memory
currentLocation = parseHref(destHref, state)
// Keep track of the next location we need to flush to the URL
next = {
href,
state,
isPush: next?.isPush || type === 'push',
}
if (!scheduled) {
// Schedule an update to the browser history
scheduled = Promise.resolve().then(() => flush())
}
}
// NOTE: this function can probably be removed
const onPushPop = (type: 'PUSH' | 'REPLACE') => {
currentLocation = parseLocation()
history.notify({ type })
}
const onPushPopEvent = async () => {
if (ignoreNextPop) {
ignoreNextPop = false
return
}
const nextLocation = parseLocation()
const delta =
nextLocation.state[stateIndexKey] - currentLocation.state[stateIndexKey]
const isForward = delta === 1
const isBack = delta === -1
const isGo = (!isForward && !isBack) || nextPopIsGo
nextPopIsGo = false
const action = isGo ? 'GO' : isBack ? 'BACK' : 'FORWARD'
const notify: SubscriberHistoryAction = isGo
? {
type: 'GO',
index: delta,
}
: {
type: isBack ? 'BACK' : 'FORWARD',
}
if (skipBlockerNextPop) {
skipBlockerNextPop = false
} else {
const blockers = _getBlockers()
if (typeof document !== 'undefined' && blockers.length) {
for (const blocker of blockers) {
const isBlocked = await blocker.blockerFn({
currentLocation,
nextLocation,
action,
})
if (isBlocked) {
ignoreNextPop = true
win.history.go(1)
history.notify(notify)
return
}
}
}
}
currentLocation = parseLocation()
history.notify(notify)
}
const onBeforeUnload = (e: BeforeUnloadEvent) => {
if (ignoreNextBeforeUnload) {
ignoreNextBeforeUnload = false
return
}
let shouldBlock = false
// If one blocker has a non-disabled beforeUnload, we should block
const blockers = _getBlockers()
if (typeof document !== 'undefined' && blockers.length) {
for (const blocker of blockers) {
const shouldHaveBeforeUnload = blocker.enableBeforeUnload ?? true
if (shouldHaveBeforeUnload === true) {
shouldBlock = true
break
}
if (
typeof shouldHaveBeforeUnload === 'function' &&
shouldHaveBeforeUnload() === true
) {
shouldBlock = true
break
}
}
}
if (shouldBlock) {
e.preventDefault()
return (e.returnValue = '')
}
return
}
const history = createHistory({
getLocation,
getLength: () => win.history.length,
pushState: (href, state) => queueHistoryAction('push', href, state),
replaceState: (href, state) => queueHistoryAction('replace', href, state),
back: (ignoreBlocker) => {
if (ignoreBlocker) skipBlockerNextPop = true
ignoreNextBeforeUnload = true
return win.history.back()
},
forward: (ignoreBlocker) => {
if (ignoreBlocker) skipBlockerNextPop = true
ignoreNextBeforeUnload = true
win.history.forward()
},
go: (n) => {
nextPopIsGo = true
win.history.go(n)
},
createHref: (href) => createHref(href),
flush,
destroy: () => {
win.history.pushState = originalPushState
win.history.replaceState = originalReplaceState
win.removeEventListener(beforeUnloadEvent, onBeforeUnload, {
capture: true,
})
win.removeEventListener(popStateEvent, onPushPopEvent)
},
onBlocked: () => {
// If a navigation is blocked, we need to rollback the location
// that we optimistically updated in memory.
if (rollbackLocation && currentLocation !== rollbackLocation) {
currentLocation = rollbackLocation
}
},
getBlockers: _getBlockers,
setBlockers: _setBlockers,
notifyOnIndexChange: false,
})
win.addEventListener(beforeUnloadEvent, onBeforeUnload, { capture: true })
win.addEventListener(popStateEvent, onPushPopEvent)
win.history.pushState = function (...args: Array<any>) {
const res = originalPushState.apply(win.history, args as any)
if (!history._ignoreSubscribers) onPushPop('PUSH')
return res
}
win.history.replaceState = function (...args: Array<any>) {
const res = originalReplaceState.apply(win.history, args as any)
if (!history._ignoreSubscribers) onPushPop('REPLACE')
return res
}
return history
}
export function createHashHistory(opts?: { window?: any }): RouterHistory {
const win =
opts?.window ??
(typeof document !== 'undefined' ? window : (undefined as any))
return createBrowserHistory({
window: win,
parseLocation: () => {
const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'
return parseHref(hashHref, win.history.state)
},
createHref: (href) =>
`${win.location.pathname}${win.location.search}#${href}`,
})
}
export function createMemoryHistory(
opts: {
initialEntries: Array<string>
initialIndex?: number
} = {
initialEntries: ['/'],
},
): RouterHistory {
const entries = opts.initialEntries
let index = opts.initialIndex
? Math.min(Math.max(opts.initialIndex, 0), entries.length - 1)
: entries.length - 1
const states = entries.map((_entry, index) =>
assignKeyAndIndex(index, undefined),
)
const getLocation = () => parseHref(entries[index]!, states[index])
return createHistory({
getLocation,
getLength: () => entries.length,
pushState: (path, state) => {
// Removes all subsequent entries after the current index to start a new branch
if (index < entries.length - 1) {
entries.splice(index + 1)
states.splice(index + 1)
}
states.push(state)
entries.push(path)
index = Math.max(entries.length - 1, 0)
},
replaceState: (path, state) => {
states[index] = state
entries[index] = path
},
back: () => {
index = Math.max(index - 1, 0)
},
forward: () => {
index = Math.min(index + 1, entries.length - 1)
},
go: (n) => {
index = Math.min(Math.max(index + n, 0), entries.length - 1)
},
createHref: (path) => path,
})
}
export function parseHref(
href: string,
state: ParsedHistoryState | undefined,
): HistoryLocation {
const hashIndex = href.indexOf('#')
const searchIndex = href.indexOf('?')
return {
href,
pathname: href.substring(
0,
hashIndex > 0
? searchIndex > 0
? Math.min(hashIndex, searchIndex)
: hashIndex
: searchIndex > 0
? searchIndex
: href.length,
),
hash: hashIndex > -1 ? href.substring(hashIndex) : '',
search:
searchIndex > -1
? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)
: '',
state: state || { [stateIndexKey]: 0, key: createRandomKey() },
}
}
// Thanks co-pilot!
function createRandomKey() {
return (Math.random() + 1).toString(36).substring(7)
}