Skip to content

use either the file or local storage tutorial#565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/services/storage/index.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,7 @@ class Storage<T> {
public get = async (): Promise<T> => {
if (SESSION_STORAGE_PATH) {
try {
// 1. read from file instead of local storage if specified
// 1. attempt to read from file instead of local storage if specified
const sessionFile = await readFile(SESSION_STORAGE_PATH, `${this.filePath}.json`)
if (!sessionFile) {
throw new Error('No session file found')
Expand All@@ -50,14 +50,15 @@ class Storage<T> {
} catch (err: any) {
logger(`Failed to read or parse session file: ${SESSION_STORAGE_PATH}/${this.filePath}.json: ${err.message}`)
}
}
const value: string | undefined = await this.storage.get(this.key)
if (value) {
// 2. read from local storage
try {
return JSON.parse(value)
} catch (err: any) {
logger(`Failed to parse session state from local storage: ${value}: ${err.message}`)
} else {
// 2. attempt to read from local storage
const value: string | undefined = await this.storage.get(this.key)
if (value) {
try {
return JSON.parse(value)
} catch (err: any) {
logger(`Failed to parse session state from local storage: ${value}: ${err.message}`)
}
}
}
// 3. fallback to the default
Expand Down
close