From: Eduardo San Martin Morote Date: Thu, 16 Apr 2020 14:19:13 +0000 (+0200) Subject: refactor(history): remove back/forward from private RouterHistory X-Git-Tag: v4.0.0-alpha.6~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b688848bcd55a8a0ed1681b4c8994e9d05858ef;p=thirdparty%2Fvuejs%2Frouter.git refactor(history): remove back/forward from private RouterHistory They are redundant given `go(distance: number)` and they were always calling go at the end --- diff --git a/__tests__/history/memory.spec.ts b/__tests__/history/memory.spec.ts index ded1092d..bd079ef4 100644 --- a/__tests__/history/memory.spec.ts +++ b/__tests__/history/memory.spec.ts @@ -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() }) }) diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index 22f06fe6..74355d64 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -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 diff --git a/src/history/common.ts b/src/history/common.ts index 3d8eb28f..dd594bb5 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -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 diff --git a/src/history/html5.ts b/src/history/html5.ts index f5cb3220..99d02875 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -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, diff --git a/src/history/memory.ts b/src/history/memory.ts index 53362f1c..02db6e33 100644 --- a/src/history/memory.ts +++ b/src/history/memory.ts @@ -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 =