From: Eduardo San Martin Morote Date: Tue, 4 Jun 2019 16:30:43 +0000 (+0200) Subject: feat(abstract): history log X-Git-Tag: v4.0.0-alpha.0~358 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35ecfb45aff671f0c65b88b906240aac3a12b386;p=thirdparty%2Fvuejs%2Frouter.git feat(abstract): history log --- diff --git a/__tests__/abstract.spec.js b/__tests__/abstract.spec.js new file mode 100644 index 00000000..e0cb613f --- /dev/null +++ b/__tests__/abstract.spec.js @@ -0,0 +1,95 @@ +// @ts-check +require('./helper') +const expect = require('expect') +const { AbstractHistory } = require('../src/history/abstract') +const { START } = require('../src/history/base') + +/** @type {import('../src/history/base').HistoryLocation} */ +const loc = { + path: '/foo', +} +const loc2 = { + path: '/bar', +} + +const normaliezedLoc = { + path: '/foo', + query: {}, + hash: '', + fullPath: '/foo', +} + +const normaliezedLoc2 = { + path: '/bar', + query: {}, + hash: '', + fullPath: '/bar', +} + +describe('Abstract/in memory history', () => { + it('starts at /', () => { + const history = new AbstractHistory() + expect(history.location).toEqual({ + fullPath: '/', + path: '/', + query: {}, + hash: '', + }) + }) + + it('can push a location', () => { + const history = new AbstractHistory() + // normalized version + history.push({ path: '/somewhere', hash: '#hey', query: { foo: 'foo' } }) + expect(history.location).toEqual({ + fullPath: '/somewhere?foo=foo#hey', + path: '/somewhere', + query: { foo: 'foo' }, + hash: '#hey', + }) + + // partial version + history.push({ path: '/path', hash: '#ho' }) + expect(history.location).toEqual({ + fullPath: '/path#ho', + path: '/path', + query: {}, + hash: '#ho', + }) + }) + + it('saves forward information', () => {}) + + it('can replace a location', () => {}) + it('can simulate a navigation', () => {}) + + it('add entries to the queue', () => { + const history = new AbstractHistory() + expect(history.queue).toHaveLength(0) + history.push(loc) + expect(history.queue).toHaveLength(1) + expect(history.queue[0]).toEqual(normaliezedLoc) + history.push(loc2) + expect(history.queue).toHaveLength(2) + expect(history.queue[1]).toEqual(normaliezedLoc2) + }) + + it('can go back', () => { + const history = new AbstractHistory() + history.push(loc) + history.push(loc2) + history.back() + expect(history.queue).toHaveLength(1) + expect(history.location).toEqual(normaliezedLoc) + history.back() + expect(history.queue).toHaveLength(0) + expect(history.location).toEqual(START) + }) + + it('does nothing with back if queue is empty', () => { + const history = new AbstractHistory() + history.back() + expect(history.location).toEqual(START) + }) + it('does nothing with forward if at end of log', () => {}) +}) diff --git a/src/history/abstract.ts b/src/history/abstract.ts index fdeff9e0..7b8098d7 100644 --- a/src/history/abstract.ts +++ b/src/history/abstract.ts @@ -1,32 +1,53 @@ -import consola from 'consola' -import { BaseHistory, HistoryLocationNormalized } from './base' -import { NavigationCallback, HistoryState } from './base' +// import consola from 'consola' +import { BaseHistory, HistoryLocation, HistoryLocationNormalized } from './base' +import { NavigationCallback, HistoryState, START } from './base' -const cs = consola.withTag('abstract') +// const cs = consola.withTag('abstract') export class AbstractHistory extends BaseHistory { // private _listeners: NavigationCallback[] = [] - private _teardowns: Array<() => void> = [] + private teardowns: Array<() => void> = [] + public queue: HistoryLocationNormalized[] = [START] constructor() { super() - cs.info('created') + debugger } // TODO: is this necessary ensureLocation() {} - replace(to: HistoryLocationNormalized) {} + replace(to: HistoryLocation) {} - push(to: HistoryLocationNormalized, data?: HistoryState) {} + push(to: HistoryLocation, data?: HistoryState) { + const toNormalized = this.utils.normalizeLocation(to) + this.location = toNormalized + } listen(callback: NavigationCallback) { return () => {} } + get location() { + console.log('read location', this.queue) + return this.queue[this.queue.length - 1] + } + + set location(location: HistoryLocationNormalized) { + // super() call tries to push before the array is created + console.log('set location', location) + if (!this.queue) this.queue = [] + // TODO: handle in the middle + this.queue.push(location) + } + + back() { + this.queue.pop() + } + destroy() { - for (const teardown of this._teardowns) teardown() - this._teardowns = [] + for (const teardown of this.teardowns) teardown() + this.teardowns = [] } back() {} diff --git a/src/history/base.ts b/src/history/base.ts index 7bce5356..caea1e0b 100644 --- a/src/history/base.ts +++ b/src/history/base.ts @@ -55,6 +55,8 @@ export interface NavigationCallback { ): void } +// TODO: should BaseHistory be just an interface instead? + export abstract class BaseHistory { // previousState: object location: HistoryLocationNormalized = START