Skip to content

Commit 445abf5

Browse files
committed
prioritize session file path over local storage
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 8522ec6 commit 445abf5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/services/storage/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ class Storage<T> {
3131
}
3232
public get = async (): Promise<T> => {
3333
const value: string | undefined = await this.storage.get(this.key)
34-
if (value) {
35-
return JSON.parse(value)
36-
} else if (SESSION_STORAGE_PATH) {
34+
if (SESSION_STORAGE_PATH) {
3735
try {
38-
// optionally read from file as a fallback to local storage
36+
// 1. read from file instead of local storage if specified
3937
const sessionFile = await readFile(SESSION_STORAGE_PATH, `${this.filePath}.json`)
4038
if (!sessionFile) {
4139
throw new Error('No session file found')
@@ -53,6 +51,15 @@ class Storage<T> {
5351
console.warn(`Failed to read or parse session file: ${SESSION_STORAGE_PATH}/${this.filePath}.json`)
5452
}
5553
}
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)