/
router.ts
867 lines (777 loc) · 26.4 KB
/
router.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
import {
RouteLocationNormalized,
RouteRecordRaw,
RouteLocationRaw,
PostNavigationGuard,
START_LOCATION_NORMALIZED,
Lazy,
RouteLocationNormalizedLoaded,
RouteLocation,
RouteRecordName,
isRouteName,
NavigationGuardWithThis,
RouteLocationOptions,
MatcherLocationRaw,
} from './types'
import { RouterHistory, HistoryState } from './history/common'
import {
ScrollPositionCoordinates,
ScrollPosition,
getSavedScrollPosition,
getScrollKey,
saveScrollPosition,
computeScrollPosition,
scrollToPosition,
} from './scrollBehavior'
import { createRouterMatcher, PathParserOptions } from './matcher'
import {
createRouterError,
ErrorTypes,
NavigationFailure,
NavigationRedirectError,
} from './errors'
import { applyToParams, isBrowser } from './utils'
import { useCallbacks } from './utils/callbacks'
import { encodeParam, decode, encodeHash } from './encoding'
import {
normalizeQuery,
parseQuery as originalParseQuery,
stringifyQuery as originalStringifyQuery,
} from './query'
import { shallowRef, Ref, nextTick, App } from 'vue'
import { RouteRecord, RouteRecordNormalized } from './matcher/types'
import { parseURL, stringifyURL, isSameRouteLocation } from './location'
import { extractComponentsGuards, guardToPromiseFn } from './navigationGuards'
import { applyRouterPlugin } from './install'
import { warn } from './warning'
/**
* Internal type to define an ErrorHandler
* @internal
*/
export type ErrorHandler = (error: any) => any
// resolve, reject arguments of Promise constructor
type OnReadyCallback = [() => void, (reason?: any) => void]
type Awaitable<T> = T | Promise<T>
export interface ScrollBehavior {
(
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded,
savedPosition: Required<ScrollPositionCoordinates> | null
): Awaitable<ScrollPosition | false | void>
}
export interface RouterOptions extends PathParserOptions {
/**
* History implementation used by the router. Most web applications should use
* `createWebHistory` but it requires the server to be properly configured.
* You can also use a _hash_ based history with `createWebHashHistory` that
* does not require any configuration on the server but isn't handled at all
* by search engines and does poorly on SEO.
*
* @example
* ```js
* createRouter({
* history: createWebHistory(),
* // other options...
* })
* ```
*/
history: RouterHistory
/**
* Initial list of routes that should be added to the router.
*/
routes: RouteRecordRaw[]
/**
* Function to control scrolling when navigating between pages.
*/
scrollBehavior?: ScrollBehavior
/**
* Custom implementation to parse a query.
*
* @example
* Let's say you want to use the package {@link https://github.com/ljharb/qs | `qs`}
* to parse queries, you would need to provide both `parseQuery` and
* {@link RouterOptions.stringifyQuery | `stringifyQuery`}:
* ```js
* import qs from 'qs'
*
* createRouter({
* // other options...
* parse: qs.parse,
* stringifyQuery: qs.stringify,
* })
* ```
*/
parseQuery?: typeof originalParseQuery
/**
* {@link RouterOptions.parseQuery | `parseQuery`} counterpart to handle query parsing.
*/
stringifyQuery?: typeof originalStringifyQuery
/**
* Default class applied to active {@link RouterLink}. If none is provided,
* `router-link-active` will be applied.
*/
linkActiveClass?: string
/**
* Default class applied to exact active {@link RouterLink}. If none is provided,
* `router-link-exact-active` will be applied.
*/
linkExactActiveClass?: string
/**
* Default class applied to non active {@link RouterLink}. If none is provided,
* `router-link-inactive` will be applied.
*/
// linkInactiveClass?: string
}
export interface Router {
readonly history: RouterHistory
readonly currentRoute: Ref<RouteLocationNormalizedLoaded>
readonly options: RouterOptions
addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void
addRoute(route: RouteRecordRaw): () => void
removeRoute(name: RouteRecordName): void
hasRoute(name: RouteRecordName): boolean
getRoutes(): RouteRecord[]
resolve(to: RouteLocationRaw): RouteLocation & { href: string }
push(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>
replace(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>
back(): Promise<NavigationFailure | void | undefined>
forward(): Promise<NavigationFailure | void | undefined>
go(delta: number): Promise<NavigationFailure | void | undefined>
beforeEach(guard: NavigationGuardWithThis<undefined>): () => void
beforeResolve(guard: NavigationGuardWithThis<undefined>): () => void
afterEach(guard: PostNavigationGuard): () => void
onError(handler: ErrorHandler): () => void
isReady(): Promise<void>
install(app: App): void
}
/**
* Create a Router instance that can be used on a Vue app.
*
* @param options - {@link RouterOptions}
*/
export function createRouter(options: RouterOptions): Router {
const matcher = createRouterMatcher(options.routes, options)
let parseQuery = options.parseQuery || originalParseQuery
let stringifyQuery = options.stringifyQuery || originalStringifyQuery
let { scrollBehavior } = options
let routerHistory = options.history
const beforeGuards = useCallbacks<NavigationGuardWithThis<undefined>>()
const beforeResolveGuards = useCallbacks<NavigationGuardWithThis<undefined>>()
const afterGuards = useCallbacks<PostNavigationGuard>()
const currentRoute = shallowRef<RouteLocationNormalizedLoaded>(
START_LOCATION_NORMALIZED
)
let pendingLocation: RouteLocation = START_LOCATION_NORMALIZED
// leave the scrollRestoration if no scrollBehavior is provided
if (isBrowser && scrollBehavior && 'scrollRestoration' in history) {
history.scrollRestoration = 'manual'
}
const normalizeParams = applyToParams.bind(
null,
paramValue => '' + paramValue
)
const encodeParams = applyToParams.bind(null, encodeParam)
const decodeParams = applyToParams.bind(null, decode)
function addRoute(
parentOrRoute: RouteRecordName | RouteRecordRaw,
route?: RouteRecordRaw
) {
let parent: Parameters<typeof matcher['addRoute']>[1] | undefined
let record: RouteRecordRaw
if (isRouteName(parentOrRoute)) {
parent = matcher.getRecordMatcher(parentOrRoute)
record = route!
} else {
record = parentOrRoute
}
return matcher.addRoute(record, parent)
}
function removeRoute(name: RouteRecordName) {
let recordMatcher = matcher.getRecordMatcher(name)
if (recordMatcher) {
matcher.removeRoute(recordMatcher)
} else if (__DEV__) {
warn(`Cannot remove non-existent route "${String(name)}"`)
}
}
function getRoutes() {
return matcher.getRoutes().map(routeMatcher => routeMatcher.record)
}
function hasRoute(name: RouteRecordName): boolean {
return !!matcher.getRecordMatcher(name)
}
function resolve(
rawLocation: Readonly<RouteLocationRaw>,
currentLocation?: Readonly<RouteLocationNormalizedLoaded>
): RouteLocation & { href: string } {
// const objectLocation = routerLocationAsObject(rawLocation)
currentLocation = currentLocation || currentRoute.value
if (typeof rawLocation === 'string') {
let locationNormalized = parseURL(
parseQuery,
rawLocation,
currentLocation.path
)
let matchedRoute = matcher.resolve(
{ path: locationNormalized.path },
currentLocation
)
if (__DEV__) {
let href = routerHistory.base + locationNormalized.fullPath
if (href.startsWith('//'))
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
)
}
return {
// fullPath: locationNormalized.fullPath,
// query: locationNormalized.query,
// hash: locationNormalized.hash,
...locationNormalized,
...matchedRoute,
// path: matchedRoute.path,
// name: matchedRoute.name,
// meta: matchedRoute.meta,
// matched: matchedRoute.matched,
params: decodeParams(matchedRoute.params),
redirectedFrom: undefined,
href: routerHistory.base + locationNormalized.fullPath,
}
}
let matcherLocation: MatcherLocationRaw
// path could be relative in object as well
if ('path' in rawLocation) {
if (
__DEV__ &&
'params' in rawLocation &&
!('name' in rawLocation) &&
Object.keys((rawLocation as any).params).length
) {
warn(
`Path "${
(rawLocation as any).path
}" was passed with params but they will be ignored. Use a named route alongside params instead.`
)
}
matcherLocation = {
...rawLocation,
path: parseURL(parseQuery, rawLocation.path, currentLocation.path).path,
}
} else {
matcherLocation = {
...rawLocation,
params: encodeParams(rawLocation.params),
}
}
let matchedRoute = matcher.resolve(matcherLocation, currentLocation)
const hash = encodeHash(rawLocation.hash || '')
if (__DEV__ && hash && hash[0] !== '#') {
warn(
`A \`hash\` should always start with the character "#". Replace "${hash}" with "#${hash}".`
)
}
// put back the unencoded params as given by the user (avoid the cost of decoding them)
matchedRoute.params =
'params' in rawLocation
? normalizeParams(rawLocation.params)
: decodeParams(matchedRoute.params)
const fullPath = stringifyURL(stringifyQuery, {
...rawLocation,
hash,
path: matchedRoute.path,
})
if (__DEV__) {
let href = routerHistory.base + fullPath
if (href.startsWith('//'))
warn(
`Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.`
)
}
return {
fullPath,
// keep the hash encoded so fullPath is effectively path + encodedQuery +
// hash
hash,
query: normalizeQuery(rawLocation.query),
...matchedRoute,
redirectedFrom: undefined,
href: routerHistory.base + fullPath,
}
}
function locationAsObject(
to: RouteLocationRaw | RouteLocationNormalized
): Exclude<RouteLocationRaw, string> | RouteLocationNormalized {
return typeof to === 'string' ? { path: to } : to
}
function push(to: RouteLocationRaw | RouteLocation) {
return pushWithRedirect(to)
}
function replace(to: RouteLocationRaw | RouteLocationNormalized) {
return push({ ...locationAsObject(to), replace: true })
}
function pushWithRedirect(
to: RouteLocationRaw | RouteLocation,
redirectedFrom?: RouteLocation
): Promise<NavigationFailure | void | undefined> {
const targetLocation: RouteLocation = (pendingLocation = resolve(to))
const from = currentRoute.value
const data: HistoryState | undefined = (to as RouteLocationOptions).state
const force: boolean | undefined = (to as RouteLocationOptions).force
// to could be a string where `replace` is a function
const replace = (to as RouteLocationOptions).replace === true
const lastMatched =
targetLocation.matched[targetLocation.matched.length - 1]
if (lastMatched && 'redirect' in lastMatched) {
const { redirect } = lastMatched
// transform it into an object to pass the original RouteLocaleOptions
let newTargetLocation = locationAsObject(
typeof redirect === 'function' ? redirect(targetLocation) : redirect
)
if (
__DEV__ &&
!('path' in newTargetLocation) &&
!('name' in newTargetLocation)
) {
warn(
`Invalid redirect found:\n${JSON.stringify(
newTargetLocation,
null,
2
)}\n when navigating to "${
targetLocation.fullPath
}". A redirect must contain a name or path.`
)
return Promise.reject(new Error('Invalid redirect'))
}
return pushWithRedirect(
{
// having a path here would be a problem with relative locations but
// at the same time it doesn't make sense for a redirect to be
// relative (no name, no path) because it would create an infinite
// loop. Since newTargetLocation must either have a `path` or a
// `name`, this will never happen
...targetLocation,
...newTargetLocation,
state: data,
force,
replace,
},
// keep original redirectedFrom if it exists
redirectedFrom || targetLocation
)
}
// if it was a redirect we already called `pushWithRedirect` above
const toLocation = targetLocation as RouteLocationNormalized
toLocation.redirectedFrom = redirectedFrom
let failure: NavigationFailure | void | undefined
if (!force && isSameRouteLocation(from, targetLocation)) {
failure = createRouterError<NavigationFailure>(
ErrorTypes.NAVIGATION_DUPLICATED,
{ to: toLocation, from }
)
// trigger scroll to allow scrolling to the same anchor
handleScroll(
from,
from,
// this is a push, the only way for it to be triggered from a
// history.listen is with a redirect, which makes it become a pus
true,
// This cannot be the first navigation because the initial location
// cannot be manually navigated to
false
)
}
return (failure ? Promise.resolve(failure) : navigate(toLocation, from))
.catch((error: NavigationFailure | NavigationRedirectError) => {
// a more recent navigation took place
if (pendingLocation !== toLocation) {
return createRouterError<NavigationFailure>(
ErrorTypes.NAVIGATION_CANCELLED,
{
from,
to: toLocation,
}
)
}
if (
error.type === ErrorTypes.NAVIGATION_ABORTED ||
error.type === ErrorTypes.NAVIGATION_GUARD_REDIRECT
) {
return error
}
// unknown error, rejects
return triggerError(error)
})
.then((failure: NavigationFailure | NavigationRedirectError | void) => {
if (failure) {
if (failure.type === ErrorTypes.NAVIGATION_GUARD_REDIRECT)
// preserve the original redirectedFrom if any
return pushWithRedirect(
// keep options
{
...locationAsObject(failure.to),
state: data,
force,
replace,
},
redirectedFrom || toLocation
)
} else {
// if we fail we don't finalize the navigation
failure = finalizeNavigation(
toLocation as RouteLocationNormalizedLoaded,
from,
true,
replace,
data
)
}
triggerAfterEach(
toLocation as RouteLocationNormalizedLoaded,
from,
failure
)
return failure
})
}
function navigate(
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded
): Promise<any> {
let guards: Lazy<any>[]
// all components here have been resolved once because we are leaving
guards = extractComponentsGuards(
from.matched.filter(record => to.matched.indexOf(record) < 0).reverse(),
'beforeRouteLeave',
to,
from
)
const [
leavingRecords,
updatingRecords,
// enteringRecords,
] = extractChangingRecords(to, from)
for (const record of leavingRecords) {
for (const guard of record.leaveGuards) {
guards.push(guardToPromiseFn(guard, to, from))
}
}
// run the queue of per route beforeRouteLeave guards
return runGuardQueue(guards)
.then(() => {
// check global guards beforeEach
guards = []
for (const guard of beforeGuards.list()) {
guards.push(guardToPromiseFn(guard, to, from))
}
return runGuardQueue(guards)
})
.then(() => {
// check in components beforeRouteUpdate
guards = extractComponentsGuards(
to.matched.filter(record => from.matched.indexOf(record as any) > -1),
'beforeRouteUpdate',
to,
from
)
for (const record of updatingRecords) {
for (const guard of record.updateGuards) {
guards.push(guardToPromiseFn(guard, to, from))
}
}
// run the queue of per route beforeEnter guards
return runGuardQueue(guards)
})
.then(() => {
// check the route beforeEnter
guards = []
for (const record of to.matched) {
// do not trigger beforeEnter on reused views
if (record.beforeEnter && from.matched.indexOf(record as any) < 0) {
if (Array.isArray(record.beforeEnter)) {
for (const beforeEnter of record.beforeEnter)
guards.push(guardToPromiseFn(beforeEnter, to, from))
} else {
guards.push(guardToPromiseFn(record.beforeEnter, to, from))
}
}
}
// run the queue of per route beforeEnter guards
return runGuardQueue(guards)
})
.then(() => {
// NOTE: at this point to.matched is normalized and does not contain any () => Promise<Component>
// check in-component beforeRouteEnter
guards = extractComponentsGuards(
// the type doesn't matter as we are comparing an object per reference
to.matched.filter(record => from.matched.indexOf(record as any) < 0),
'beforeRouteEnter',
to,
from
)
// run the queue of per route beforeEnter guards
return runGuardQueue(guards)
})
.then(() => {
// check global guards beforeResolve
guards = []
for (const guard of beforeResolveGuards.list()) {
guards.push(guardToPromiseFn(guard, to, from))
}
return runGuardQueue(guards)
})
}
function triggerAfterEach(
to: RouteLocationNormalizedLoaded,
from: RouteLocationNormalizedLoaded,
failure?: NavigationFailure | void
): void {
// navigation is confirmed, call afterGuards
// TODO: wrap with error handlers
for (const guard of afterGuards.list()) guard(to, from, failure)
}
/**
* - Cleans up any navigation guards
* - Changes the url if necessary
* - Calls the scrollBehavior
*/
function finalizeNavigation(
toLocation: RouteLocationNormalizedLoaded,
from: RouteLocationNormalizedLoaded,
isPush: boolean,
replace?: boolean,
data?: HistoryState
): NavigationFailure | void {
// a more recent navigation took place
if (pendingLocation !== toLocation) {
return createRouterError<NavigationFailure>(
ErrorTypes.NAVIGATION_CANCELLED,
{
from,
to: toLocation,
}
)
}
const [leavingRecords] = extractChangingRecords(toLocation, from)
for (const record of leavingRecords) {
// remove registered guards from removed matched records
record.leaveGuards = []
// free the references
// TODO: to refactor once keep-alive and transition can be supported
record.instances = {}
}
// only consider as push if it's not the first navigation
const isFirstNavigation = from === START_LOCATION_NORMALIZED
const state = !isBrowser ? {} : history.state
// change URL only if the user did a push/replace and if it's not the initial navigation because
// it's just reflecting the url
if (isPush) {
// on the initial navigation, we want to reuse the scroll position from
// history state if it exists
if (replace || isFirstNavigation)
routerHistory.replace(toLocation, {
scroll: isFirstNavigation && state && state.scroll,
...data,
})
else routerHistory.push(toLocation, data)
}
// accept current navigation
currentRoute.value = toLocation
handleScroll(toLocation, from, isPush, isFirstNavigation)
markAsReady()
}
// attach listener to history to trigger navigations
routerHistory.listen((to, _from, info) => {
// TODO: in dev try catch to correctly log the matcher error
// cannot be a redirect route because it was in history
const toLocation = resolve(to.fullPath) as RouteLocationNormalized
pendingLocation = toLocation
const from = currentRoute.value
// TODO: should be moved to web history?
if (isBrowser) {
saveScrollPosition(
getScrollKey(from.fullPath, info.delta),
computeScrollPosition()
)
}
navigate(toLocation, from)
.catch((error: NavigationFailure | NavigationRedirectError) => {
// a more recent navigation took place
if (pendingLocation !== toLocation) {
return createRouterError<NavigationFailure>(
ErrorTypes.NAVIGATION_CANCELLED,
{
from,
to: toLocation,
}
)
}
if (error.type === ErrorTypes.NAVIGATION_ABORTED) {
return error as NavigationFailure
}
if (error.type === ErrorTypes.NAVIGATION_GUARD_REDIRECT) {
routerHistory.go(-info.delta, false)
// the error is already handled by router.push we just want to avoid
// logging the error
pushWithRedirect(
(error as NavigationRedirectError).to,
toLocation
).catch(() => {
// TODO: in dev show warning, in prod triggerError, same as initial navigation
})
// avoid the then branch
return Promise.reject()
}
// TODO: test on different browsers ensure consistent behavior
routerHistory.go(-info.delta, false)
// unrecognized error, transfer to the global handler
return triggerError(error)
})
.then((failure: NavigationFailure | void) => {
failure =
failure ||
finalizeNavigation(
// after navigation, all matched components are resolved
toLocation as RouteLocationNormalizedLoaded,
from,
false
)
// revert the navigation
if (failure) routerHistory.go(-info.delta, false)
triggerAfterEach(
toLocation as RouteLocationNormalizedLoaded,
from,
failure
)
})
.catch(() => {
// TODO: same as above
})
})
// Initialization and Errors
let readyHandlers = useCallbacks<OnReadyCallback>()
let errorHandlers = useCallbacks<ErrorHandler>()
let ready: boolean
/**
* Trigger errorHandlers added via onError and throws the error as well
* @param error - error to throw
* @returns the error as a rejected promise
*/
function triggerError(error: any) {
markAsReady(error)
errorHandlers.list().forEach(handler => handler(error))
return Promise.reject(error)
}
/**
* Returns a Promise that resolves or reject when the router has finished its
* initial navigation. This will be automatic on client but requires an
* explicit `router.push` call on the server. This behavior can change
* depending on the history implementation used e.g. the defaults history
* implementation (client only) triggers this automatically but the memory one
* (should be used on server) doesn't
*/
function isReady(): Promise<void> {
if (ready && currentRoute.value !== START_LOCATION_NORMALIZED)
return Promise.resolve()
return new Promise((resolve, reject) => {
readyHandlers.add([resolve, reject])
})
}
/**
* Mark the router as ready, resolving the promised returned by isReady(). Can
* only be called once, otherwise does nothing.
* @param err - optional error
*/
function markAsReady(err?: any): void {
if (ready) return
ready = true
readyHandlers
.list()
.forEach(([resolve, reject]) => (err ? reject(err) : resolve()))
readyHandlers.reset()
}
// Scroll behavior
function handleScroll(
to: RouteLocationNormalizedLoaded,
from: RouteLocationNormalizedLoaded,
isPush: boolean,
isFirstNavigation: boolean
): Promise<any> {
if (!isBrowser || !scrollBehavior) return Promise.resolve()
let scrollPosition: Required<ScrollPositionCoordinates> | null =
(!isPush && getSavedScrollPosition(getScrollKey(to.fullPath, 0))) ||
((isFirstNavigation || !isPush) &&
(history.state as HistoryState) &&
history.state.scroll) ||
null
return nextTick()
.then(() => scrollBehavior!(to, from, scrollPosition))
.then(position => position && scrollToPosition(position))
.catch(triggerError)
}
function go(delta: number) {
return new Promise<NavigationFailure | void | undefined>(
(resolve, reject) => {
let removeError = errorHandlers.add(err => {
removeError()
removeAfterEach()
reject(err)
})
let removeAfterEach = afterGuards.add((_to, _from, failure) => {
removeError()
removeAfterEach()
resolve(failure)
})
routerHistory.go(delta)
}
)
}
const router: Router = {
currentRoute,
addRoute,
removeRoute,
hasRoute,
getRoutes,
resolve,
options,
push,
replace,
go,
back: () => go(-1),
forward: () => go(1),
beforeEach: beforeGuards.add,
beforeResolve: beforeResolveGuards.add,
afterEach: afterGuards.add,
onError: errorHandlers.add,
isReady,
history: routerHistory,
install(app: App) {
applyRouterPlugin(app, this)
},
}
return router
}
function runGuardQueue(guards: Lazy<any>[]): Promise<void> {
return guards.reduce(
(promise, guard) => promise.then(() => guard()),
Promise.resolve()
)
}
function extractChangingRecords(
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded
) {
const leavingRecords: RouteRecordNormalized[] = []
const updatingRecords: RouteRecordNormalized[] = []
const enteringRecords: RouteRecordNormalized[] = []
// TODO: could be optimized with one single for loop
for (const record of from.matched) {
if (to.matched.indexOf(record) < 0) leavingRecords.push(record)
else updatingRecords.push(record)
}
for (const record of to.matched) {
// the type doesn't matter because we are comparing per reference
if (from.matched.indexOf(record as any) < 0) enteringRecords.push(record)
}
return [leavingRecords, updatingRecords, enteringRecords]
}