]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(history): createMemoryHistory
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:13:22 +0000 (15:13 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:13:22 +0000 (15:13 +0200)
src/history/abstract.2.ts [new file with mode: 0644]
src/history/common.ts
src/history/html5.2.ts

diff --git a/src/history/abstract.2.ts b/src/history/abstract.2.ts
new file mode 100644 (file)
index 0000000..3b24249
--- /dev/null
@@ -0,0 +1,82 @@
+// import consola from 'consola'
+import {
+  RouterHistory,
+  NavigationCallback,
+  START,
+  normalizeLocation,
+  HistoryLocationNormalized,
+  HistoryState,
+} from './common'
+
+// TODO: implement navigation direction in listeners
+
+// const cs = console
+// const cs = consola.withTag('abstract')
+
+export default function createAbstractHistory(): RouterHistory {
+  let listeners: NavigationCallback[] = []
+  // TODO: make sure this is right as the first location is nowhere so maybe this should be empty instead
+  let queue: HistoryLocationNormalized[] = [START]
+  let position: number = 0
+
+  function setLocation(location: HistoryLocationNormalized) {
+    position++
+    if (position === queue.length) {
+      // we are at the end, we can simply append a new entry
+      queue.push(location)
+    } else {
+      // we are in the middle, we remove everything from here in the queue
+      queue.splice(position)
+      queue.push(location)
+    }
+  }
+
+  const routerHistory: RouterHistory = {
+    // rewritten by Object.defineProperty
+    location: START,
+
+    replace(to) {
+      const toNormalized = normalizeLocation(to)
+      // remove current entry and decrement position
+      queue.splice(position--, 1)
+      setLocation(toNormalized)
+    },
+
+    push(to, data?: HistoryState) {
+      setLocation(normalizeLocation(to))
+    },
+
+    listen(callback) {
+      listeners.push(callback)
+      return () => {
+        const index = listeners.indexOf(callback)
+        if (index > -1) listeners.splice(index, 1)
+      }
+    },
+    destroy() {
+      listeners = []
+    },
+
+    // back(triggerListeners: boolean = true) {
+    //   const from = this.location
+    //   if (this.position > 0) this.position--
+    //   if (triggerListeners) {
+    //     this.triggerListeners(this.location, from, {
+    //       direction: NavigationDirection.back,
+    //     })
+    //   }
+    // },
+
+    // forward(triggerListeners: boolean = true) {
+    //   const from = this.location
+    //   if (this.position < this.queue.length - 1) this.position++
+    //   if (triggerListeners) {
+    //     this.triggerListeners(this.location, from, {
+    //       direction: NavigationDirection.forward,
+    //     })
+    //   }
+    // },
+  }
+
+  return routerHistory
+}
index 1da7659f2239ae5c884f9b0cc670e349bdccaafb..e664beac3460032735940a87d06960dd97cf2e6c 100644 (file)
@@ -35,7 +35,7 @@ type HistoryStateValue =
   | HistoryState
   | HistoryStateArray
 
-interface HistoryState {
+export interface HistoryState {
   [x: number]: HistoryStateValue
   [x: string]: HistoryStateValue
 }
@@ -60,6 +60,15 @@ export interface NavigationCallback {
   ): void
 }
 
+// starting point for abstract history
+const START_PATH = ''
+export const START: HistoryLocationNormalized = {
+  fullPath: START_PATH,
+  path: START_PATH,
+  query: {},
+  hash: '',
+}
+
 export interface RouterHistory {
   location: HistoryLocationNormalized
   push(to: RawHistoryLocation, data?: any): void
index 01bb8f0ced68f338fae79be91c8ea5bafff446d4..62329334fa7224e18948e9614c17dd90b488c276 100644 (file)
@@ -5,8 +5,9 @@ import {
   normalizeLocation,
   NavigationType,
   NavigationDirection,
+  HistoryLocationNormalized,
+  HistoryState,
 } from './common'
-import { HistoryLocationNormalized, HistoryState } from './base'
 import { computeScrollPosition, ScrollToPosition } from '../utils/scroll'
 // import consola from 'consola'
 
@@ -139,13 +140,14 @@ export default function createHistory(): RouterHistory {
     }
   }
 
-  return {
+  const routerHistory: RouterHistory = {
+    // it's overriden right after
     location,
 
     replace(to) {
       const normalized = normalizeLocation(to)
 
-      cs.info('replace', location, normalized)
+      // cs.info('replace', location, normalized)
 
       const state: StateEntry = buildState(
         historyState.back,
@@ -168,6 +170,8 @@ export default function createHistory(): RouterHistory {
       const normalized = normalizeLocation(to)
 
       // Add to current entry the information of where we are going
+      // as well as saving the current position
+      // TODO: the scroll position computation should be customizable
       const currentState = {
         ...historyState,
         forward: normalized,
@@ -181,14 +185,14 @@ export default function createHistory(): RouterHistory {
         ...data,
       }
 
-      cs.info(
-        'push',
-        location.fullPath,
-        '->',
-        normalized.fullPath,
-        'with state',
-        state
-      )
+      // cs.info(
+      //   'push',
+      //   location.fullPath,
+      //   '->',
+      //   normalized.fullPath,
+      //   'with state',
+      //   state
+      // )
 
       changeLocation(state, '', normalized.fullPath, false)
       location = normalized
@@ -213,4 +217,10 @@ export default function createHistory(): RouterHistory {
       window.removeEventListener('popstate', popStateHandler)
     },
   }
+
+  Object.defineProperty(routerHistory, 'location', {
+    get: () => location,
+  })
+
+  return routerHistory
 }