@@ -30,12 +30,9 @@ class Storage<T> {
30
30
this . defaultValue = defaultValue
31
31
}
32
32
public get = async ( ) :Promise < T > => {
33
- const value :string | undefined = await this . storage . get ( this . key )
34
- if ( value ) {
35
- return JSON . parse ( value )
36
- } else if ( SESSION_STORAGE_PATH ) {
33
+ if ( SESSION_STORAGE_PATH ) {
37
34
try {
38
- //optionally read from fileas a fallback to local storage
35
+ //1. read from fileinstead of local storage if specified
39
36
const sessionFile = await readFile ( SESSION_STORAGE_PATH , `${ this . filePath } .json` )
40
37
if ( ! sessionFile ) {
41
38
throw new Error ( 'No session file found' )
@@ -53,6 +50,16 @@ class Storage<T> {
53
50
console . warn ( `Failed to read or parse session file:${ SESSION_STORAGE_PATH } /${ this . filePath } .json` )
54
51
}
55
52
}
53
+ const value :string | undefined = await this . storage . get ( this . key )
54
+ if ( value ) {
55
+ // 2. read from local storage
56
+ try {
57
+ return JSON . parse ( value )
58
+ } catch ( err ) {
59
+ console . warn ( `Failed to parse session state from local storage:${ value } ` )
60
+ }
61
+ }
62
+ // 3. fallback to the default
56
63
return this . defaultValue
57
64
}
58
65
public set = ( value :T ) :void => {