]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(abstract): avoid listeners with back and forward
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Jun 2019 14:25:37 +0000 (16:25 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Jun 2019 14:25:37 +0000 (16:25 +0200)
__tests__/abstract.spec.js
src/history/abstract.ts
src/history/base.ts
src/history/html5.ts

index 1b8a6b1d6f8550622be706982c8a60c1a2aba49d..6e38c12f5b8f50a9c732f6251623adca5eb0a463 100644 (file)
@@ -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()
+  })
 })
index abef01ae8607532a9d352b587b528859726204d5..38485562182aac871687605532ed90edfc975f5a 100644 (file)
@@ -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() {
index aeb6cf35cd0434e834a51b54b84384d13c6e936e..f844f3228932d9575afe2f7d14fa9a510794aae2 100644 (file)
@@ -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
index 248d935fae93612256fa6e55400173a510d06cda..68de65a12ee9c1c2714ec61bc064ccb14e5a0153 100644 (file)
@@ -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()
   }