From: Eduardo San Martin Morote Date: Mon, 6 May 2019 20:28:35 +0000 (+0200) Subject: refactor: remove old TODOS X-Git-Tag: v4.0.0-alpha.0~371 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d8e73ae3fab0f95d222749e43193f416696d9ea;p=thirdparty%2Fvuejs%2Frouter.git refactor: remove old TODOS --- diff --git a/src/errors.ts b/src/errors.ts index f7038b49..d95a45fa 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -26,7 +26,7 @@ export class InvalidRouteMatch extends Error { * Error used when rejecting a navigation because of a redirection. Contains * information about where we where trying to go and where we are going instead */ -export class RedirectError extends Error { +export class NavigationGuardRedirect extends Error { from: RouteLocationNormalized to: RouteLocation constructor(from: RouteLocationNormalized, to: RouteLocation) { diff --git a/src/history/html5.ts b/src/history/html5.ts index 2a7de0ed..11fcfd0f 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -128,7 +128,6 @@ export class HTML5History extends BaseHistory { // call all listeners const navigationInfo = { type: - // TODO: we should save somekind of id to detect the navigation type state.forward && from.fullPath === state.forward.fullPath ? NavigationType.back : NavigationType.forward, diff --git a/src/history/utils.ts b/src/history/utils.ts index 0df4ede8..902ece63 100644 --- a/src/history/utils.ts +++ b/src/history/utils.ts @@ -54,24 +54,22 @@ export function parseURL(location: string): HistoryLocationNormalized { export function parseQuery(search: string): HistoryQuery { // TODO: optimize by using a for loop const hasLeadingIM = search[0] === '?' - return (hasLeadingIM ? search.slice(1) : search).split('&').reduce( - (query: HistoryQuery, entry: string) => { - const [key, value] = entry.split('=') - if (key in query) { - // an extra variable for ts types - let currentValue = query[key] - if (!Array.isArray(currentValue)) { - currentValue = query[key] = [currentValue] - } - currentValue.push(value) - } else { - query[key] = value + const query: HistoryQuery = {} + const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&') + for (let i = 0; i < searchParams.length; ++i) { + const [key, value] = searchParams[i].split('=') + if (key in query) { + // an extra variable for ts types + let currentValue = query[key] + if (!Array.isArray(currentValue)) { + currentValue = query[key] = [currentValue] } - - return query - }, - {} as HistoryQuery - ) + currentValue.push(value) + } else { + query[key] = value + } + } + return query } /** diff --git a/src/matcher.ts b/src/matcher.ts index f207e23e..8982b07b 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -39,7 +39,7 @@ export class RouterMatcher { let path = parent.record.path // only add the / delimiter if the child path isn't empty if (recordCopy.path) path += '/' - path += record.path // TODO: check for trailing slash? + path += record.path recordCopy.path = path } } @@ -55,7 +55,6 @@ export class RouterMatcher { resolve: pathToRegexp.compile(recordCopy.path), } - // TODO: if children use option end: false ? // TODO: why is the isArray check necessary for ts? if ('children' in record && Array.isArray(record.children)) { for (const childRecord of record.children) { diff --git a/src/router.ts b/src/router.ts index 3d0ddaf1..6cb0eee3 100644 --- a/src/router.ts +++ b/src/router.ts @@ -15,7 +15,7 @@ import { } from './types/index' import { guardToPromiseFn, extractComponentsGuards } from './utils' -import { RedirectError } from './errors' +import { NavigationGuardRedirect } from './errors' export interface RouterOptions { history: BaseHistory @@ -36,10 +36,9 @@ export class Router { this.matcher = new RouterMatcher(options.routes) this.history.listen((to, from, info) => { - // TODO: check navigation guards const matchedRoute = this.matchLocation(to, this.currentRoute) // console.log({ to, matchedRoute }) - // TODO: navigate + // TODO: navigate & guards this.currentRoute = { ...to, @@ -117,7 +116,7 @@ export class Router { try { await this.navigate(toLocation, this.currentRoute) } catch (error) { - if (error instanceof RedirectError) { + if (error instanceof NavigationGuardRedirect) { // TODO: setup redirect stack return this.push(error.to) } else { @@ -163,8 +162,6 @@ export class Router { to: RouteLocationNormalized, from: RouteLocationNormalized ): Promise { - // TODO: Will probably need to be some kind of queue in the future that allows to remove - // elements and other stuff let guards: Lazy[] // TODO: is it okay to resolve all matched component or should we do it in order diff --git a/src/utils/guardToPromiseFn.ts b/src/utils/guardToPromiseFn.ts index 6304eb24..4d67744f 100644 --- a/src/utils/guardToPromiseFn.ts +++ b/src/utils/guardToPromiseFn.ts @@ -6,7 +6,7 @@ import { } from '../types' import { isRouteLocation } from './index' -import { RedirectError } from '../errors' +import { NavigationGuardRedirect } from '../errors' export function guardToPromiseFn( guard: NavigationGuard, @@ -22,8 +22,7 @@ export function guardToPromiseFn( // TODO: handle callback if (valid === false) reject(new Error('Aborted')) else if (isRouteLocation(valid)) { - // TODO: redirect - reject(new RedirectError(to, valid)) + reject(new NavigationGuardRedirect(to, valid)) } else resolve() }