@@ -32,6 +32,49 @@ function scrollToHash(hash: string, fallbackTop?: boolean) {
3232 }
3333}
3434
35+ export function createMemoryHistory ( ) {
36+ const entries = [ "/" ] ;
37+ let index = 0 ;
38+ const listeners : ( ( value : string ) => void ) [ ] = [ ] ;
39+
40+ const go = ( n : number ) => {
41+ // https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245
42+ index = Math . max ( 0 , Math . min ( index + n , entries . length - 1 ) ) ;
43+
44+ const value = entries [ index ] ;
45+ listeners . forEach ( listener => listener ( value ) ) ;
46+ } ;
47+
48+ return {
49+ get : ( ) => entries [ index ] ,
50+ set : ( { value, scroll, replace } : LocationChange ) => {
51+ if ( replace ) {
52+ entries [ index ] = value ;
53+ } else {
54+ entries . splice ( index + 1 , entries . length - index , value ) ;
55+ index ++ ;
56+ }
57+ if ( scroll ) {
58+ scrollToHash ( value . split ( "#" ) [ 1 ] || "" , true ) ;
59+ }
60+ } ,
61+ back : ( ) => {
62+ go ( - 1 ) ;
63+ } ,
64+ forward : ( ) => {
65+ go ( 1 ) ;
66+ } ,
67+ go,
68+ listen : ( listener : ( value : string ) => void ) => {
69+ listeners . push ( listener ) ;
70+ return ( ) => {
71+ const index = listeners . indexOf ( listener ) ;
72+ listeners . splice ( index , 1 ) ;
73+ } ;
74+ }
75+ } ;
76+ }
77+
3578export function createIntegration (
3679 get : ( ) => string | LocationChange ,
3780 set : ( next : LocationChange ) => void ,
@@ -137,3 +180,10 @@ export function hashIntegration() {
137180 }
138181 ) ;
139182}
183+
184+ export function memoryIntegration ( ) {
185+ const memoryHistory = createMemoryHistory ( ) ;
186+ return createIntegration ( memoryHistory . get , memoryHistory . set , memoryHistory . listen , {
187+ go : memoryHistory . go
188+ } ) ;
189+ }
0 commit comments