From: Eduardo San Martin Morote Date: Wed, 9 Oct 2019 13:13:22 +0000 (+0200) Subject: refactor(history): createMemoryHistory X-Git-Tag: v4.0.0-alpha.0~212 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e96b3c47b985b6517017d85a8e9ed9ad0fddca9;p=thirdparty%2Fvuejs%2Frouter.git refactor(history): createMemoryHistory --- diff --git a/src/history/abstract.2.ts b/src/history/abstract.2.ts new file mode 100644 index 00000000..3b24249b --- /dev/null +++ b/src/history/abstract.2.ts @@ -0,0 +1,82 @@ +// import consola from 'consola' +import { + RouterHistory, + NavigationCallback, + START, + normalizeLocation, + HistoryLocationNormalized, + HistoryState, +} from './common' + +// TODO: implement navigation direction in listeners + +// const cs = console +// const cs = consola.withTag('abstract') + +export default function createAbstractHistory(): RouterHistory { + let listeners: NavigationCallback[] = [] + // TODO: make sure this is right as the first location is nowhere so maybe this should be empty instead + let queue: HistoryLocationNormalized[] = [START] + let position: number = 0 + + function setLocation(location: HistoryLocationNormalized) { + position++ + if (position === queue.length) { + // we are at the end, we can simply append a new entry + queue.push(location) + } else { + // we are in the middle, we remove everything from here in the queue + queue.splice(position) + queue.push(location) + } + } + + const routerHistory: RouterHistory = { + // rewritten by Object.defineProperty + location: START, + + replace(to) { + const toNormalized = normalizeLocation(to) + // remove current entry and decrement position + queue.splice(position--, 1) + setLocation(toNormalized) + }, + + push(to, data?: HistoryState) { + setLocation(normalizeLocation(to)) + }, + + listen(callback) { + listeners.push(callback) + return () => { + const index = listeners.indexOf(callback) + if (index > -1) listeners.splice(index, 1) + } + }, + destroy() { + listeners = [] + }, + + // back(triggerListeners: boolean = true) { + // const from = this.location + // if (this.position > 0) this.position-- + // if (triggerListeners) { + // this.triggerListeners(this.location, from, { + // direction: NavigationDirection.back, + // }) + // } + // }, + + // forward(triggerListeners: boolean = true) { + // const from = this.location + // if (this.position < this.queue.length - 1) this.position++ + // if (triggerListeners) { + // this.triggerListeners(this.location, from, { + // direction: NavigationDirection.forward, + // }) + // } + // }, + } + + return routerHistory +} diff --git a/src/history/common.ts b/src/history/common.ts index 1da7659f..e664beac 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -35,7 +35,7 @@ type HistoryStateValue = | HistoryState | HistoryStateArray -interface HistoryState { +export interface HistoryState { [x: number]: HistoryStateValue [x: string]: HistoryStateValue } @@ -60,6 +60,15 @@ export interface NavigationCallback { ): void } +// starting point for abstract history +const START_PATH = '' +export const START: HistoryLocationNormalized = { + fullPath: START_PATH, + path: START_PATH, + query: {}, + hash: '', +} + export interface RouterHistory { location: HistoryLocationNormalized push(to: RawHistoryLocation, data?: any): void diff --git a/src/history/html5.2.ts b/src/history/html5.2.ts index 01bb8f0c..62329334 100644 --- a/src/history/html5.2.ts +++ b/src/history/html5.2.ts @@ -5,8 +5,9 @@ import { normalizeLocation, NavigationType, NavigationDirection, + HistoryLocationNormalized, + HistoryState, } from './common' -import { HistoryLocationNormalized, HistoryState } from './base' import { computeScrollPosition, ScrollToPosition } from '../utils/scroll' // import consola from 'consola' @@ -139,13 +140,14 @@ export default function createHistory(): RouterHistory { } } - return { + const routerHistory: RouterHistory = { + // it's overriden right after location, replace(to) { const normalized = normalizeLocation(to) - cs.info('replace', location, normalized) + // cs.info('replace', location, normalized) const state: StateEntry = buildState( historyState.back, @@ -168,6 +170,8 @@ export default function createHistory(): RouterHistory { const normalized = normalizeLocation(to) // Add to current entry the information of where we are going + // as well as saving the current position + // TODO: the scroll position computation should be customizable const currentState = { ...historyState, forward: normalized, @@ -181,14 +185,14 @@ export default function createHistory(): RouterHistory { ...data, } - cs.info( - 'push', - location.fullPath, - '->', - normalized.fullPath, - 'with state', - state - ) + // cs.info( + // 'push', + // location.fullPath, + // '->', + // normalized.fullPath, + // 'with state', + // state + // ) changeLocation(state, '', normalized.fullPath, false) location = normalized @@ -213,4 +217,10 @@ export default function createHistory(): RouterHistory { window.removeEventListener('popstate', popStateHandler) }, } + + Object.defineProperty(routerHistory, 'location', { + get: () => location, + }) + + return routerHistory }