Skip to content

Commit 2b17530

Browse files
authored
prioritize session file path over local storage (#553)
* prioritize session file path over local storage Signed-off-by: shmck <shawn.j.mckay@gmail.com> * move local storage lookup later Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 8522ec6 commit 2b17530

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

‎src/services/storage/index.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ class Storage<T> {
3030
this.defaultValue = defaultValue
3131
}
3232
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) {
3734
try {
38-
// optionally read from file as a fallback to local storage
35+
// 1. read from file instead of local storage if specified
3936
const sessionFile = await readFile(SESSION_STORAGE_PATH, `${this.filePath}.json`)
4037
if (!sessionFile) {
4138
throw new Error('No session file found')
@@ -53,6 +50,16 @@ class Storage<T> {
5350
console.warn(`Failed to read or parse session file: ${SESSION_STORAGE_PATH}/${this.filePath}.json`)
5451
}
5552
}
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
5663
return this.defaultValue
5764
}
5865
public set = (value: T): void => {

0 commit comments

Comments
 (0)