Skip to content

Commit 24ff936

Browse files
escatonposva
andauthored
feat: memory history stores state (#2491)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent ec0bff1 commit 24ff936

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

packages/router/__tests__/history/memory.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ describe('Memory history', () => {
5151
expect(history.location).toEqual(START)
5252
})
5353

54+
it('stores a state', () => {
55+
const history = createMemoryHistory()
56+
history.push(loc, { foo: 'bar' })
57+
expect(history.state).toEqual({ foo: 'bar' })
58+
history.push(loc, { foo: 'baz' })
59+
expect(history.state).toEqual({ foo: 'baz' })
60+
history.go(-1)
61+
expect(history.state).toEqual({ foo: 'bar' })
62+
})
63+
5464
it('does nothing with back if queue contains only one element', () => {
5565
const history = createMemoryHistory()
5666
history.go(-1)

packages/router/src/history/memory.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ import {
2020
*/
2121
export function createMemoryHistory(base: string = ''): RouterHistory {
2222
let listeners: NavigationCallback[] = []
23-
let queue: HistoryLocation[] = [START]
23+
let queue: [url: HistoryLocation, state: HistoryState][] = [[START, {}]]
2424
let position: number = 0
2525
base = normalizeBase(base)
2626

27-
function setLocation(location: HistoryLocation) {
27+
function setLocation(location: HistoryLocation, state: HistoryState = {}) {
2828
position++
2929
if (position !== queue.length) {
3030
// we are in the middle, we remove everything from here in the queue
3131
queue.splice(position)
3232
}
33-
queue.push(location)
33+
queue.push([location, state])
3434
}
3535

3636
function triggerListeners(
@@ -51,19 +51,19 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
5151
const routerHistory: RouterHistory = {
5252
// rewritten by Object.defineProperty
5353
location: START,
54-
// TODO: should be kept in queue
54+
// rewritten by Object.defineProperty
5555
state: {},
5656
base,
5757
createHref: createHref.bind(null, base),
5858

59-
replace(to) {
59+
replace(to, state?: HistoryState) {
6060
// remove current entry and decrement position
6161
queue.splice(position--, 1)
62-
setLocation(to)
62+
setLocation(to, state)
6363
},
6464

65-
push(to, data?: HistoryState) {
66-
setLocation(to)
65+
push(to, state?: HistoryState) {
66+
setLocation(to, state)
6767
},
6868

6969
listen(callback) {
@@ -75,7 +75,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
7575
},
7676
destroy() {
7777
listeners = []
78-
queue = [START]
78+
queue = [[START, {}]]
7979
position = 0
8080
},
8181

@@ -98,14 +98,19 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
9898

9999
Object.defineProperty(routerHistory, 'location', {
100100
enumerable: true,
101-
get: () => queue[position],
101+
get: () => queue[position][0],
102+
})
103+
104+
Object.defineProperty(routerHistory, 'state', {
105+
enumerable: true,
106+
get: () => queue[position][1],
102107
})
103108

104109
if (__TEST__) {
105110
// @ts-expect-error: only for tests
106-
routerHistory.changeURL = function (url: string) {
111+
routerHistory.changeURL = function (url: string, state: HistoryState = {}) {
107112
const from = this.location
108-
queue.splice(position++ + 1, queue.length, url)
113+
queue.splice(position++ + 1, queue.length, [url, state])
109114
triggerListeners(this.location, from, {
110115
direction: NavigationDirection.unknown,
111116
delta: 0,

0 commit comments

Comments
 (0)