From d48c73e718064dbdc34f9cdaefeda97cc49e24c2 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 5 Jun 2019 15:57:51 +0200 Subject: [PATCH] feat(abstract): remove listeners --- __tests__/abstract.spec.js | 72 ++++++++++++++++++++++++++++++++++++-- src/history/abstract.ts | 12 ++++--- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/__tests__/abstract.spec.js b/__tests__/abstract.spec.js index 24aeb525..e4822728 100644 --- a/__tests__/abstract.spec.js +++ b/__tests__/abstract.spec.js @@ -61,11 +61,47 @@ describe('Abstract/in memory history', () => { }) }) - it('saves forward information', () => {}) + it('can replace a location', () => { + const history = new AbstractHistory() + // partial version + history.replace({ path: '/somewhere', hash: '#hey', query: { foo: 'foo' } }) + expect(history.location).toEqual({ + fullPath: '/somewhere?foo=foo#hey', + path: '/somewhere', + query: { foo: 'foo' }, + hash: '#hey', + }) + expect(history.position).toEqual(0) + expect(history.queue).toHaveLength(1) + history.push(loc) + + // partial version + history.replace({ path: '/path', hash: '#ho' }) + expect(history.location).toEqual({ + fullPath: '/path#ho', + path: '/path', + query: {}, + hash: '#ho', + }) + expect(history.position).toEqual(1) + expect(history.queue).toHaveLength(2) + }) - it('can replace a location', () => {}) + it('does not trigger listeners with push', () => { + const history = new AbstractHistory() + const spy = jest.fn() + history.listen(spy) + history.push(loc) + expect(spy).not.toHaveBeenCalled() + }) - it('can simulate a navigation', () => {}) + it('does not trigger listeners with replace', () => { + const history = new AbstractHistory() + const spy = jest.fn() + history.listen(spy) + history.replace(loc) + expect(spy).not.toHaveBeenCalled() + }) it('add entries to the queue', () => { const history = new AbstractHistory() @@ -161,4 +197,34 @@ describe('Abstract/in memory history', () => { expect(spy).not.toHaveBeenCalled() expect(spy2).toHaveBeenCalledTimes(1) }) + + it('removing the same listener is a noop', () => { + const history = new AbstractHistory() + const spy = jest.fn() + const spy2 = jest.fn() + const rem = history.listen(spy) + const rem2 = history.listen(spy2) + rem() + rem() + history.push(loc) + history.back() + expect(spy).not.toHaveBeenCalled() + expect(spy2).toHaveBeenCalledTimes(1) + rem2() + rem2() + history.forward() + expect(spy).not.toHaveBeenCalled() + expect(spy2).toHaveBeenCalledTimes(1) + }) + + it('removes all listeners with destroy', () => { + const history = new AbstractHistory() + const spy = jest.fn() + history.listen(spy) + // @ts-ignore + expect(history.listeners).toHaveLength(1) + history.destroy() + // @ts-ignore + expect(history.listeners).toHaveLength(0) + }) }) diff --git a/src/history/abstract.ts b/src/history/abstract.ts index c0434f1e..8e283ff2 100644 --- a/src/history/abstract.ts +++ b/src/history/abstract.ts @@ -10,8 +10,6 @@ import { NavigationCallback, HistoryState, START } from './base' // const cs = consola.withTag('abstract') export class AbstractHistory extends BaseHistory { - // private _listeners: NavigationCallback[] = [] - private teardowns: Array<() => void> = [] private listeners: NavigationCallback[] = [] public queue: HistoryLocationNormalized[] = [START] public position: number = 0 @@ -23,7 +21,12 @@ export class AbstractHistory extends BaseHistory { // TODO: is this necessary ensureLocation() {} - replace(to: HistoryLocation) {} + replace(to: HistoryLocation) { + const toNormalized = this.utils.normalizeLocation(to) + // remove current entry and decrement position + this.queue.splice(this.position--, 1) + this.location = toNormalized + } push(to: HistoryLocation, data?: HistoryState) { const toNormalized = this.utils.normalizeLocation(to) @@ -70,8 +73,7 @@ export class AbstractHistory extends BaseHistory { } destroy() { - for (const teardown of this.teardowns) teardown() - this.teardowns = [] + this.listeners = [] } private triggerListeners( -- 2.39.5