From: Eduardo San Martin Morote Date: Wed, 9 Oct 2019 13:14:23 +0000 (+0200) Subject: refactor(router): adapt doInitialNavigation to abstract history X-Git-Tag: v4.0.0-alpha.0~211 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=804843187bc8f7ab3c2965470f9d70fb28e94c5d;p=thirdparty%2Fvuejs%2Frouter.git refactor(router): adapt doInitialNavigation to abstract history --- diff --git a/src/index.ts b/src/index.ts index 519bb126..f46d777c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import { AbstractHistory } from './history/abstract' import { BaseHistory } from './history/base' import { PluginFunction, VueConstructor } from 'vue' import createHistory from './history/html5.2' +import createMemoryHistory from './history/abstract.2' import View from './components/View' import Link from './components/Link' @@ -82,7 +83,7 @@ export { // abstract: AbstractHistory // } -export { createHistory } +export { createHistory, createMemoryHistory } export default class VueRouter extends Router { static install = plugin diff --git a/src/router.ts b/src/router.ts index b8caabcc..2251bf62 100644 --- a/src/router.ts +++ b/src/router.ts @@ -3,7 +3,8 @@ import { RouterHistory, stringifyURL, normalizeQuery, - HistoryLocationNormalized + HistoryLocationNormalized, + START, } from './history/common' import { RouterMatcher } from './matcher' import { @@ -17,19 +18,19 @@ import { PostNavigationGuard, Lazy, MatcherLocation, - RouteQueryAndHash + RouteQueryAndHash, } from './types/index' import { ScrollToPosition, ScrollPosition, - scrollToPosition + scrollToPosition, } from './utils/scroll' import { guardToPromiseFn, extractComponentsGuards } from './utils' import { NavigationGuardRedirect, NavigationAborted, - NavigationCancelled + NavigationCancelled, } from './errors' interface ScrollBehavior { @@ -93,7 +94,7 @@ export class Router { // accept current navigation this.currentRoute = { ...to, - ...matchedRoute + ...matchedRoute, } this.updateReactiveRoute() // TODO: refactor with a state getter @@ -152,7 +153,7 @@ export class Router { // TODO: refactor with url utils query: {}, hash: '', - ...to + ...to, }) } @@ -173,12 +174,12 @@ export class Router { fullPath: stringifyURL({ path: matchedRoute.normalizedLocation.path, query: location.query, - hash: location.hash + hash: location.hash, }), query: normalizeQuery(location.query || {}), hash: location.hash, redirectedFrom, - meta: {} + meta: {}, } if (typeof redirect === 'string') { @@ -207,7 +208,7 @@ export class Router { { ...newLocation, query: normalizeQuery(newLocation.query || {}), - hash: newLocation.hash || '' + hash: newLocation.hash || '', }, currentLocation, normalizedLocation @@ -217,7 +218,7 @@ export class Router { { ...redirect, query: normalizeQuery(redirect.query || {}), - hash: redirect.hash || '' + hash: redirect.hash || '', }, currentLocation, normalizedLocation @@ -228,12 +229,12 @@ export class Router { const url = normalizeLocation({ path: matchedRoute.path, query: location.query, - hash: location.hash + hash: location.hash, }) return { ...matchedRoute, ...url, - redirectedFrom + redirectedFrom, } } } @@ -280,7 +281,7 @@ export class Router { url = normalizeLocation({ query, hash, - ...location + ...location, }) } @@ -475,6 +476,7 @@ export class Router { private updateReactiveRoute() { if (!this.app) return + this.markAsReady() // TODO: matched should be non enumerable and the defineProperty here shouldn't be necessary const route = { ...this.currentRoute } Object.defineProperty(route, 'matched', { enumerable: false }) @@ -513,7 +515,9 @@ export class Router { this.ready = true } - async doInitialNavigation(): Promise { + async doInitialNavigation(): Promise { + if (this.history.location === START) return + console.log('doing initial navigation') // TODO: refactor code that was duplicated from push method const toLocation: RouteLocationNormalized = this.resolveLocation( this.history.location, @@ -533,7 +537,8 @@ export class Router { throw new NavigationCancelled(toLocation, this.currentRoute) } // TODO: setup redirect stack - return this.push(error.to) + await this.push(error.to) + return } else { // TODO: write tests // triggerError as well @@ -565,7 +570,6 @@ export class Router { for (const guard of this.afterGuards) guard(toLocation, from) this.markAsReady() - return this.currentRoute } private async handleScroll(