history.push(loc)
history.back()
expect(spy).toHaveBeenCalledTimes(1)
- expect(spy).toHaveBeenCalledWith(START, normaliezedLoc, { direction: 'back' })
+ expect(spy).toHaveBeenCalledWith(START, normaliezedLoc, {
+ direction: 'back',
+ })
history.forward()
expect(spy).toHaveBeenCalledTimes(2)
expect(spy).toHaveBeenLastCalledWith(normaliezedLoc, START, {
// @ts-ignore
expect(history.listeners).toHaveLength(0)
})
+
+ it('can avoid listeners with back and forward', () => {
+ const history = new AbstractHistory()
+ const spy = jest.fn()
+ history.listen(spy)
+ history.push(loc)
+ history.back(false)
+ expect(spy).not.toHaveBeenCalled()
+ history.forward(false)
+ expect(spy).not.toHaveBeenCalled()
+ })
})
}
}
- back() {
+ back(triggerListeners: boolean = true) {
const from = this.location
if (this.position > 0) this.position--
- this.triggerListeners(this.location, from, {
- direction: NavigationDirection.back,
- })
+ if (triggerListeners) {
+ this.triggerListeners(this.location, from, {
+ direction: NavigationDirection.back,
+ })
+ }
}
- forward() {
+ forward(triggerListeners: boolean = true) {
const from = this.location
if (this.position < this.queue.length - 1) this.position++
- this.triggerListeners(this.location, from, {
- direction: NavigationDirection.forward,
- })
+ if (triggerListeners) {
+ this.triggerListeners(this.location, from, {
+ direction: NavigationDirection.forward,
+ })
+ }
}
destroy() {
* Goes back in history log. Should trigger any listener added via
* `listen`. If we are on the first entry, behaviour may change depending
* on implementation
+ * @param triggerListeners should default to true. If false, won't trigger listeners added via .listen()
*/
- abstract back(): void
+ abstract back(triggerListeners?: boolean): void
/**
* Goes forward in history log. Should trigger any listener added via
* `listen`. If we are on the last entry, behaviour may change depending
* on implementation
+ * @param triggerListeners should default to true. If false, won't trigger listeners added via .listen()
*/
- abstract forward(): void
+ abstract forward(triggerListeners?: boolean): void
/**
* Notifies back whenever the location changes due to user interactions
this.location = normalized
}
- back() {
+ back(triggerListeners: boolean = true) {
// TODO: do not trigger listen
this.history.back()
}
- forward() {
+
+ forward(triggerListeners: boolean = true) {
// TODO: do not trigger listen
this.history.forward()
}