]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(abstract): history log
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 4 Jun 2019 16:30:43 +0000 (18:30 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 4 Jun 2019 16:30:49 +0000 (18:30 +0200)
__tests__/abstract.spec.js [new file with mode: 0644]
src/history/abstract.ts
src/history/base.ts

diff --git a/__tests__/abstract.spec.js b/__tests__/abstract.spec.js
new file mode 100644 (file)
index 0000000..e0cb613
--- /dev/null
@@ -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', () => {})
+})
index fdeff9e0e1bf0efd15ee3dd9b306a13404b50237..7b8098d7a70c84012dadc725ccfa4976b39c387f 100644 (file)
@@ -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() {}
index 7bce535619e008236e440309fa26e685414c148f..caea1e0b92ec7eddd844dd41ebc78655ba54bbd3 100644 (file)
@@ -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