@@ -20,17 +20,17 @@ import {
20
20
*/
21
21
export function createMemoryHistory ( base : string = '' ) : RouterHistory {
22
22
let listeners : NavigationCallback [ ] = [ ]
23
- let queue : HistoryLocation [ ] = [ START ]
23
+ let queue : [ url : HistoryLocation , state : HistoryState ] [ ] = [ [ START , { } ] ]
24
24
let position : number = 0
25
25
base = normalizeBase ( base )
26
26
27
- function setLocation ( location : HistoryLocation ) {
27
+ function setLocation ( location : HistoryLocation , state : HistoryState = { } ) {
28
28
position ++
29
29
if ( position !== queue . length ) {
30
30
// we are in the middle, we remove everything from here in the queue
31
31
queue . splice ( position )
32
32
}
33
- queue . push ( location )
33
+ queue . push ( [ location , state ] )
34
34
}
35
35
36
36
function triggerListeners (
@@ -51,19 +51,19 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
51
51
const routerHistory : RouterHistory = {
52
52
// rewritten by Object.defineProperty
53
53
location : START ,
54
- // TODO: should be kept in queue
54
+ // rewritten by Object.defineProperty
55
55
state : { } ,
56
56
base,
57
57
createHref : createHref . bind ( null , base ) ,
58
58
59
- replace ( to ) {
59
+ replace ( to , state ?: HistoryState ) {
60
60
// remove current entry and decrement position
61
61
queue . splice ( position -- , 1 )
62
- setLocation ( to )
62
+ setLocation ( to , state )
63
63
} ,
64
64
65
- push ( to , data ?: HistoryState ) {
66
- setLocation ( to )
65
+ push ( to , state ?: HistoryState ) {
66
+ setLocation ( to , state )
67
67
} ,
68
68
69
69
listen ( callback ) {
@@ -75,7 +75,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
75
75
} ,
76
76
destroy ( ) {
77
77
listeners = [ ]
78
- queue = [ START ]
78
+ queue = [ [ START , { } ] ]
79
79
position = 0
80
80
} ,
81
81
@@ -98,14 +98,19 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
98
98
99
99
Object . defineProperty ( routerHistory , 'location' , {
100
100
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 ] ,
102
107
} )
103
108
104
109
if ( __TEST__ ) {
105
110
// @ts -expect-error: only for tests
106
- routerHistory . changeURL = function ( url : string ) {
111
+ routerHistory . changeURL = function ( url : string , state : HistoryState = { } ) {
107
112
const from = this . location
108
- queue . splice ( position ++ + 1 , queue . length , url )
113
+ queue . splice ( position ++ + 1 , queue . length , [ url , state ] )
109
114
triggerListeners ( this . location , from , {
110
115
direction : NavigationDirection . unknown ,
111
116
delta : 0 ,
0 commit comments