scroll: ScrollToPosition | null
}
+interface PauseState {
+ currentLocation: HistoryLocationNormalized
+ // location we are going to after pausing
+ to: HistoryLocationNormalized
+}
+
export default function createHistory(): RouterHistory {
const { history } = window
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,
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
}
}
+ function pauseListeners(to: HistoryLocationNormalized) {
+ pauseState = {
+ currentLocation: location,
+ to,
+ }
+ }
+
const routerHistory: RouterHistory = {
// it's overriden right after
location,
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)
// 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