From: Eduardo San Martin Morote Date: Wed, 5 Jun 2019 14:25:37 +0000 (+0200) Subject: feat(abstract): avoid listeners with back and forward X-Git-Tag: v4.0.0-alpha.0~352 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b79a933885e64494d52bfdb1c45c1149e210897f;p=thirdparty%2Fvuejs%2Frouter.git feat(abstract): avoid listeners with back and forward --- diff --git a/__tests__/abstract.spec.js b/__tests__/abstract.spec.js index 1b8a6b1d..6e38c12f 100644 --- a/__tests__/abstract.spec.js +++ b/__tests__/abstract.spec.js @@ -173,7 +173,9 @@ describe('Abstract/in memory history', () => { 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, { @@ -227,4 +229,15 @@ describe('Abstract/in memory history', () => { // @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() + }) }) diff --git a/src/history/abstract.ts b/src/history/abstract.ts index abef01ae..38485562 100644 --- a/src/history/abstract.ts +++ b/src/history/abstract.ts @@ -60,20 +60,24 @@ export class AbstractHistory extends BaseHistory { } } - 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() { diff --git a/src/history/base.ts b/src/history/base.ts index aeb6cf35..f844f322 100644 --- a/src/history/base.ts +++ b/src/history/base.ts @@ -82,15 +82,17 @@ export abstract class BaseHistory { * 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 diff --git a/src/history/html5.ts b/src/history/html5.ts index 248d935f..68de65a1 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -91,11 +91,12 @@ export class HTML5History extends BaseHistory { 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() }