]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: remove old TODOS
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 20:28:35 +0000 (22:28 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 20:28:35 +0000 (22:28 +0200)
src/errors.ts
src/history/html5.ts
src/history/utils.ts
src/matcher.ts
src/router.ts
src/utils/guardToPromiseFn.ts

index f7038b492c0752c66d12231cb8a70d4f79d0edcb..d95a45fadb42a31fa768786361956ec2115e9a6d 100644 (file)
@@ -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) {
index 2a7de0ed4791e03974697d930244737b27467e7e..11fcfd0fd317caacf582673af0e689ae79ccb070 100644 (file)
@@ -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,
index 0df4ede8819df4abfb331d06727f79cab8959706..902ece634e1c1e927b8c84a53122d1e969f58c7e 100644 (file)
@@ -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
 }
 
 /**
index f207e23e52f3735df91664f61f7361b114f50150..8982b07b0bd475612e3b315df639e1059c9c20c8 100644 (file)
@@ -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) {
index 3d0ddaf1fea2799bc9596afa3bb964dd635722cc..6cb0eee3622a87264c78a60fb45e8b25a4618d9d 100644 (file)
@@ -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> {
-    // TODO: Will probably need to be some kind of queue in the future that allows to remove
-    // elements and other stuff
     let guards: Lazy<any>[]
 
     // TODO: is it okay to resolve all matched component or should we do it in order
index 6304eb242c9c642aa6fbca06c0f895e574108da3..4d67744fe753bb3407f92fd4ac7b0454a230b0fd 100644 (file)
@@ -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()
       }