]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(history): remove back/forward from private RouterHistory
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 16 Apr 2020 14:19:13 +0000 (16:19 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 16 Apr 2020 14:19:13 +0000 (16:19 +0200)
They are redundant given `go(distance: number)` and they were always calling go
at the end

__tests__/history/memory.spec.ts
__tests__/router.spec.ts
src/history/common.ts
src/history/html5.ts
src/history/memory.ts

index ded1092d4813ca5913da1aaab3428f0e1a45375a..bd079ef426a3d5a021786f53fe75076cf62a8ba5 100644 (file)
@@ -60,21 +60,21 @@ describe('Memory history', () => {
     const history = createMemoryHistory()
     history.push(loc)
     history.push(loc2)
-    history.back()
+    history.go(-1)
     expect(history.location).toEqual(normalizedLoc)
-    history.back()
+    history.go(-1)
     expect(history.location).toEqual(START)
   })
 
   it('does nothing with back if queue contains only one element', () => {
     const history = createMemoryHistory()
-    history.back()
+    history.go(-1)
     expect(history.location).toEqual(START)
   })
 
   it('does nothing with forward if at end of log', () => {
     const history = createMemoryHistory()
-    history.forward()
+    history.go(1)
     expect(history.location).toEqual(START)
   })
 
@@ -82,12 +82,12 @@ describe('Memory history', () => {
     const history = createMemoryHistory()
     history.push(loc)
     history.push(loc2)
-    history.back()
-    history.back()
+    history.go(-1)
+    history.go(-1)
     expect(history.location).toEqual(START)
-    history.forward()
+    history.go(1)
     expect(history.location).toEqual(normalizedLoc)
-    history.forward()
+    history.go(1)
     expect(history.location).toEqual(normalizedLoc2)
   })
 
@@ -95,13 +95,13 @@ describe('Memory history', () => {
     const history = createMemoryHistory()
     history.push(loc)
     history.push(loc2)
-    history.back()
-    history.back()
+    history.go(-1)
+    history.go(-1)
     expect(history.location).toEqual(START)
     history.push(loc2)
     expect(history.location).toEqual(normalizedLoc2)
     // does nothing
-    history.forward()
+    history.go(1)
     expect(history.location).toEqual(normalizedLoc2)
   })
 
@@ -110,14 +110,14 @@ describe('Memory history', () => {
     const spy = jest.fn()
     history.listen(spy)
     history.push(loc)
-    history.back()
+    history.go(-1)
     expect(spy).toHaveBeenCalledTimes(1)
     expect(spy).toHaveBeenCalledWith(START, normalizedLoc, {
       direction: 'back',
       distance: -1,
       type: 'pop',
     })
-    history.forward()
+    history.go(1)
     expect(spy).toHaveBeenCalledTimes(2)
     expect(spy).toHaveBeenLastCalledWith(normalizedLoc, START, {
       direction: 'forward',
@@ -134,11 +134,11 @@ describe('Memory history', () => {
     history.listen(spy)()
     const remove = history.listen(spy2)
     history.push(loc)
-    history.back()
+    history.go(-1)
     expect(spy).not.toHaveBeenCalled()
     expect(spy2).toHaveBeenCalledTimes(1)
     remove()
-    history.forward()
+    history.go(1)
     expect(spy).not.toHaveBeenCalled()
     expect(spy2).toHaveBeenCalledTimes(1)
   })
@@ -152,12 +152,12 @@ describe('Memory history', () => {
     rem()
     rem()
     history.push(loc)
-    history.back()
+    history.go(-1)
     expect(spy).not.toHaveBeenCalled()
     expect(spy2).toHaveBeenCalledTimes(1)
     rem2()
     rem2()
-    history.forward()
+    history.go(1)
     expect(spy).not.toHaveBeenCalled()
     expect(spy2).toHaveBeenCalledTimes(1)
   })
@@ -168,7 +168,7 @@ describe('Memory history', () => {
     const spy = jest.fn()
     history.listen(spy)
     history.destroy()
-    history.back()
+    history.go(-1)
     expect(spy).not.toHaveBeenCalled()
   })
 
@@ -177,9 +177,9 @@ describe('Memory history', () => {
     const spy = jest.fn()
     history.listen(spy)
     history.push(loc)
-    history.back(false)
+    history.go(-1, false)
     expect(spy).not.toHaveBeenCalled()
-    history.forward(false)
+    history.go(1, false)
     expect(spy).not.toHaveBeenCalled()
   })
 })
index 22f06fe6499aaeacaf67ba3f0d221498338e521c..74355d6493a6f9b0d034c030dd97352aa2a48a3f 100644 (file)
@@ -387,8 +387,8 @@ describe('Router', () => {
       })
 
       // trigger to history.back()
-      history.back()
-      history.back()
+      history.go(-1)
+      history.go(-1)
 
       expect(router.currentRoute.value.fullPath).toBe('/p/b')
       // resolves the last call to history.back() first
index 3d8eb28fd6287e13e98dda733174bc7f0c893560..dd594bb50490ee43ed3eb82ca67cc9f5c84c8892 100644 (file)
@@ -101,21 +101,13 @@ export interface RouterHistory {
   replace(to: RawHistoryLocation, data?: HistoryState): void
 
   /**
-   * Goes back in history
+   * Traverses history in a given direction.
    *
-   * @param triggerListeners - whether this should trigger listeners attached to
-   * the history
-   */
-  back(triggerListeners?: boolean): void
-  /**
-   * Goes forward in history
-   *
-   * @param triggerListeners - whether this should trigger listeners attached to
-   * the history
-   */
-  forward(triggerListeners?: boolean): void
-  /**
-   * Traverses history in a given direction
+   * @example
+   * ```js
+   * myHistory.go(-1) // equivalent to window.history.back()
+   * myHistory.go(1) // equivalent to window.history.forward()
+   * ```
    *
    * @param distance - distance to travel. If distance is \< 0, it will go back,
    * if it's \> 0, it will go forward
index f5cb32202397b1edd6c1177d1d34712e56dd4331..99d028751e13d6349de7079d46e31554c80ce6d8 100644 (file)
@@ -273,12 +273,6 @@ export default function createWebHistory(base?: string): RouterHistory {
     historyNavigation.location,
     historyNavigation.replace
   )
-  function back(triggerListeners = true) {
-    go(-1, triggerListeners)
-  }
-  function forward(triggerListeners = true) {
-    go(1, triggerListeners)
-  }
   function go(distance: number, triggerListeners = true) {
     if (!triggerListeners) historyListeners.pauseListeners()
     history.go(distance)
@@ -288,8 +282,6 @@ export default function createWebHistory(base?: string): RouterHistory {
     // @ts-ignore
     location: '',
     base,
-    back,
-    forward,
     go,
 
     ...historyNavigation,
index 53362f1ce4dcc27ca28806db7a78ca8f27495154..02db6e3381c17bad3e6e0a15ab0cdbae0ad4f914 100644 (file)
@@ -81,14 +81,6 @@ export default function createMemoryHistory(base: string = ''): RouterHistory {
       listeners = []
     },
 
-    back(shouldTrigger = true) {
-      this.go(-1, shouldTrigger)
-    },
-
-    forward(shouldTrigger = true) {
-      this.go(1, shouldTrigger)
-    },
-
     go(distance, shouldTrigger = true) {
       const from = this.location
       const direction: NavigationDirection =