]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(history): add back and forward
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:38:52 +0000 (15:38 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:38:52 +0000 (15:38 +0200)
src/history/html5.2.ts
src/index.ts

index 62329334fa7224e18948e9614c17dd90b488c276..3247ad1b1a98a83e92dfa04a9b8fb699b4a4831b 100644 (file)
@@ -24,6 +24,12 @@ interface StateEntry {
   scroll: ScrollToPosition | null
 }
 
+interface PauseState {
+  currentLocation: HistoryLocationNormalized
+  // location we are going to after pausing
+  to: HistoryLocationNormalized
+}
+
 export default function createHistory(): RouterHistory {
   const { history } = window
 
@@ -89,6 +95,9 @@ export default function createHistory(): RouterHistory {
   let teardowns: Array<() => void> = []
   // TODO: should it be a stack? a Dict. Check if the popstate listener
   // can trigger twice
+  let pauseState: PauseState | null = null
+  // TODO: should it be a stack? a Dict. Check if the popstate listener
+  // can trigger twice
 
   const popStateHandler: PopStateListener = ({
     state,
@@ -101,7 +110,16 @@ export default function createHistory(): RouterHistory {
 
     const from = location
     const fromState = historyState
-    location = createCurrentLocation(window.location)
+    const to = createCurrentLocation(window.location)
+
+    if (pauseState && pauseState.to && pauseState.to.fullPath === to.fullPath) {
+      cs.info('Ignored beacuse paused')
+      // reset pauseState
+      pauseState = null
+      return
+    }
+
+    location = to
     historyState = state
     const deltaFromCurrent = fromState
       ? state.position - fromState.position
@@ -140,6 +158,13 @@ export default function createHistory(): RouterHistory {
     }
   }
 
+  function pauseListeners(to: HistoryLocationNormalized) {
+    pauseState = {
+      currentLocation: location,
+      to,
+    }
+  }
+
   const routerHistory: RouterHistory = {
     // it's overriden right after
     location,
@@ -198,6 +223,20 @@ export default function createHistory(): RouterHistory {
       location = normalized
     },
 
+    back(triggerListeners = true) {
+      const to = historyState.back
+      if (!to) throw new Error('Cannot go back')
+      if (!triggerListeners) pauseListeners(to)
+      history.back()
+    },
+
+    forward(triggerListeners = true) {
+      const to = historyState.forward
+      if (!to) throw new Error('Cannot go forward')
+      if (!triggerListeners) pauseListeners(to)
+      history.forward()
+    },
+
     listen(callback) {
       // settup the listener and prepare teardown callbacks
       listeners.push(callback)
index f46d777c39dca503cdafc71db64b473599a8cea7..714b5d991d6a405a9fec8ea213313f6b74ea1201 100644 (file)
@@ -33,7 +33,9 @@ const plugin: PluginFunction<void> = Vue => {
           // true
         )
 
-        router.doInitialNavigation().catch(() => {})
+        router.doInitialNavigation().catch(err => {
+          console.error('Unhandled error', err)
+        })
       } else {
         // @ts-ignore we are adding this
         this._routerRoot = (this.$parent && this.$parent._routerRoot) || this