From: Eduardo San Martin Morote Date: Wed, 9 Oct 2019 13:38:52 +0000 (+0200) Subject: feat(history): add back and forward X-Git-Tag: v4.0.0-alpha.0~208 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e92d00800adc7bc397468754e80e6b921e8c9ff;p=thirdparty%2Fvuejs%2Frouter.git feat(history): add back and forward --- diff --git a/src/history/html5.2.ts b/src/history/html5.2.ts index 62329334..3247ad1b 100644 --- a/src/history/html5.2.ts +++ b/src/history/html5.2.ts @@ -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) diff --git a/src/index.ts b/src/index.ts index f46d777c..714b5d99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,7 +33,9 @@ const plugin: PluginFunction = 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