@@ -6,7 +6,6 @@ type Subscription = {
6
6
} ;
7
7
8
8
const RA_STORE = 'RaStore' ;
9
- const prefixLength = RA_STORE . length ;
10
9
11
10
// localStorage isn't available in incognito mode. We need to detect it
12
11
const testLocalStorage = ( ) => {
@@ -39,7 +38,12 @@ let localStorageAvailable = testLocalStorage();
39
38
* </Admin>
40
39
* );
41
40
*/
42
- export const localStorageStore = ( version : string = '1' ) : Store => {
41
+ export const localStorageStore = (
42
+ version : string = '1' ,
43
+ appKey : string = ''
44
+ ) : Store => {
45
+ const prefix = `${ RA_STORE } ${ appKey } ` ;
46
+ const prefixLength = prefix . length ;
43
47
const subscriptions : { [ key : string ] : Subscription } = { } ;
44
48
const publish = ( key : string , value : any ) => {
45
49
Object . keys ( subscriptions ) . forEach ( id => {
@@ -53,7 +57,7 @@ export const localStorageStore = (version: string = '1'): Store => {
53
57
// Whenever the local storage changes in another document, look for matching subscribers.
54
58
// This allows to synchronize state across tabs
55
59
const onLocalStorageChange = ( event : StorageEvent ) : void => {
56
- if ( event . key ?. substring ( 0 , prefixLength ) !== RA_STORE ) {
60
+ if ( event . key ?. substring ( 0 , prefixLength ) !== prefix ) {
57
61
return ;
58
62
}
59
63
const key = event . key . substring ( prefixLength + 1 ) ;
@@ -77,13 +81,11 @@ export const localStorageStore = (version: string = '1'): Store => {
77
81
return {
78
82
setup : ( ) => {
79
83
if ( localStorageAvailable ) {
80
- const storedVersion = getStorage ( ) . getItem (
81
- `${ RA_STORE } .version`
82
- ) ;
84
+ const storedVersion = getStorage ( ) . getItem ( `${ prefix } .version` ) ;
83
85
if ( storedVersion && storedVersion !== version ) {
84
86
getStorage ( ) . clear ( ) ;
85
87
}
86
- getStorage ( ) . setItem ( `${ RA_STORE } .version` , version ) ;
88
+ getStorage ( ) . setItem ( `${ prefix } .version` , version ) ;
87
89
window . addEventListener ( 'storage' , onLocalStorageChange ) ;
88
90
}
89
91
} ,
@@ -93,7 +95,7 @@ export const localStorageStore = (version: string = '1'): Store => {
93
95
}
94
96
} ,
95
97
getItem < T = any > ( key : string , defaultValue ?: T ) : T {
96
- const valueFromStorage = getStorage ( ) . getItem ( `${ RA_STORE } .${ key } ` ) ;
98
+ const valueFromStorage = getStorage ( ) . getItem ( `${ prefix } .${ key } ` ) ;
97
99
98
100
// eslint-disable-next-line eqeqeq
99
101
return valueFromStorage == null
@@ -102,23 +104,20 @@ export const localStorageStore = (version: string = '1'): Store => {
102
104
} ,
103
105
setItem < T = any > ( key : string , value : T ) : void {
104
106
if ( value === undefined ) {
105
- getStorage ( ) . removeItem ( `${ RA_STORE } .${ key } ` ) ;
107
+ getStorage ( ) . removeItem ( `${ prefix } .${ key } ` ) ;
106
108
} else {
107
- getStorage ( ) . setItem (
108
- `${ RA_STORE } .${ key } ` ,
109
- JSON . stringify ( value )
110
- ) ;
109
+ getStorage ( ) . setItem ( `${ prefix } .${ key } ` , JSON . stringify ( value ) ) ;
111
110
}
112
111
publish ( key , value ) ;
113
112
} ,
114
113
removeItem ( key : string ) : void {
115
- getStorage ( ) . removeItem ( `${ RA_STORE } .${ key } ` ) ;
114
+ getStorage ( ) . removeItem ( `${ prefix } .${ key } ` ) ;
116
115
publish ( key , undefined ) ;
117
116
} ,
118
117
reset ( ) : void {
119
118
const storage = getStorage ( ) ;
120
119
for ( let i = 0 ; i < storage . length ; i ++ ) {
121
- if ( storage . key ( i ) ?. substring ( 0 , prefixLength ) === RA_STORE ) {
120
+ if ( storage . key ( i ) ?. substring ( 0 , prefixLength ) === prefix ) {
122
121
const key = storage . key ( i ) ?. substring ( prefixLength + 1 ) ;
123
122
if ( ! key || ! storage . key ( i ) ) return ;
124
123
// @ts -ignore
0 commit comments