export interface NavigationInformation {
type: NavigationType
direction: NavigationDirection
- distance: number
+ delta: number
}
export interface NavigationCallback {
* 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
+ * @param delta - distance to travel. If delta is \< 0, it will go back,
+ * if it's \> 0, it will go forward by that amount of entries.
* @param triggerListeners - whether this should trigger listeners attached to
* the history
*/
- go(distance: number, triggerListeners?: boolean): void
+ go(delta: number, triggerListeners?: boolean): void
/**
* Attach a listener to the History implementation that is triggered when the
const deltaFromCurrent = fromState
? state.position - fromState.position
: ''
- const distance = deltaFromCurrent || 0
+ const delta = deltaFromCurrent || 0
// console.log({ deltaFromCurrent })
- // Here we could also revert the navigation by calling history.go(-distance)
+ // Here we could also revert the navigation by calling history.go(-delta)
// this listener will have to be adapted to not trigger again and to wait for the url
// to be updated before triggering the listeners. Some kind of validation function would also
// need to be passed to the listeners so the navigation can be accepted
// call all listeners
listeners.forEach(listener => {
listener(location.value, from, {
- distance,
+ delta,
type: NavigationType.pop,
- direction: distance
- ? distance > 0
+ direction: delta
+ ? delta > 0
? NavigationDirection.forward
: NavigationDirection.back
: NavigationDirection.unknown,
historyNavigation.location,
historyNavigation.replace
)
- function go(distance: number, triggerListeners = true) {
+ function go(delta: number, triggerListeners = true) {
if (!triggerListeners) historyListeners.pauseListeners()
- history.go(distance)
+ history.go(delta)
}
const routerHistory: RouterHistory = {
// it's overridden right after
function triggerListeners(
to: HistoryLocationNormalized,
from: HistoryLocationNormalized,
- {
- direction,
- distance,
- }: Pick<NavigationInformation, 'direction' | 'distance'>
+ { direction, delta }: Pick<NavigationInformation, 'direction' | 'delta'>
): void {
const info: NavigationInformation = {
direction,
- distance,
+ delta,
type: NavigationType.pop,
}
for (let callback of listeners) {
listeners = []
},
- go(distance, shouldTrigger = true) {
+ go(delta, shouldTrigger = true) {
const from = this.location
const direction: NavigationDirection =
- // we are considering distance === 0 going forward, but in abstract mode
- // using 0 for the distance doesn't make sense like it does in html5 where
+ // we are considering delta === 0 going forward, but in abstract mode
+ // using 0 for the delta doesn't make sense like it does in html5 where
// it reloads the page
- distance < 0 ? NavigationDirection.back : NavigationDirection.forward
- position = Math.max(0, Math.min(position + distance, queue.length - 1))
+ delta < 0 ? NavigationDirection.back : NavigationDirection.forward
+ position = Math.max(0, Math.min(position + delta, queue.length - 1))
if (shouldTrigger) {
triggerListeners(this.location, from, {
direction,
- distance,
+ delta,
})
}
},
// merged
back(): void
forward(): void
- go(distance: number): void
+ go(delta: number): void
beforeEach(guard: NavigationGuardWithThis<undefined>): () => void
beforeResolve(guard: NavigationGuardWithThis<undefined>): () => void
if (isBrowser) {
saveScrollPosition(
- getScrollKey(from.fullPath, info.distance),
+ getScrollKey(from.fullPath, info.delta),
computeScrollPosition()
)
}
return error as NavigationFailure
}
if (error.type === ErrorTypes.NAVIGATION_GUARD_REDIRECT) {
- history.go(-info.distance, false)
+ history.go(-info.delta, false)
// the error is already handled by router.push we just want to avoid
// logging the error
pushWithRedirect(
return Promise.reject()
}
// TODO: test on different browsers ensure consistent behavior
- history.go(-info.distance, false)
+ history.go(-info.delta, false)
// unrecognized error, transfer to the global handler
return triggerError(error)
})
)
// revert the navigation
- if (failure) history.go(-info.distance, false)
+ if (failure) history.go(-info.delta, false)
triggerAfterEach(
toLocation as RouteLocationNormalizedLoaded,