HistoryState,
NavigationType,
NavigationDirection,
+ NavigationInformation,
} from './common'
// TODO: implement navigation direction in listeners
function triggerListeners(
to: HistoryLocationNormalized,
from: HistoryLocationNormalized,
- { direction }: { direction: NavigationDirection }
+ {
+ direction,
+ distance,
+ }: Pick<NavigationInformation, 'direction' | 'distance'>
): void {
- // TODO: proper type
- const info: Parameters<NavigationCallback>[2] = {
+ const info: NavigationInformation = {
direction,
+ distance,
type: NavigationType.pop,
}
for (let callback of listeners) {
},
back(shouldTrigger = true) {
- const from = this.location
- if (position > 0) position--
- if (shouldTrigger) {
- triggerListeners(this.location, from, {
- direction: NavigationDirection.back,
- })
- }
+ this.go(-1, shouldTrigger)
+ },
+
+ forward(shouldTrigger = true) {
+ this.go(1, shouldTrigger)
},
- forward(shouldTrigger: boolean = true) {
+ go(distance, shouldTrigger = true) {
const from = this.location
- if (position < queue.length - 1) position++
+ 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
+ // it reloads the page
+ distance < 0 ? NavigationDirection.back : NavigationDirection.forward
+ position = Math.max(0, Math.min(position + distance, queue.length - 1))
if (shouldTrigger) {
triggerListeners(this.location, from, {
- direction: NavigationDirection.forward,
+ direction,
+ distance,
})
}
},