forked from microsoft/OCR-Form-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from localStorage to indexed DB. (microsoft#886)
* Replace localstorage with WebStorageManager. (microsoft#882) * Use singleton and merge data from localStorage. * Add toast for displaying error messages. * Fix build error. * Fix typescript warning. Co-authored-by: SimoTw <[email protected]>
- Loading branch information
1 parent
1b48fd2
commit 67e1903
Showing
17 changed files
with
174 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as localforage from "localforage"; | ||
import { toast } from "react-toastify"; | ||
|
||
class WebStorage { | ||
private isStorageReady: boolean = false; | ||
|
||
constructor() { | ||
localforage.config({ | ||
name: "FOTT-webStorage", | ||
driver: [localforage.INDEXEDDB, localforage.LOCALSTORAGE], | ||
}); | ||
} | ||
|
||
private async waitForStorageReady() { | ||
if (!this.isStorageReady) { | ||
try { | ||
await localforage.ready(); | ||
this.isStorageReady = true; | ||
} catch (error) { | ||
toast.error(`No usable storage driver is found.`); | ||
} | ||
} | ||
} | ||
|
||
public async getItem(key: string) { | ||
try { | ||
await this.waitForStorageReady(); | ||
return await localforage.getItem(key); | ||
} catch (err) { | ||
toast.error(`WebStorage getItem("${key}") error: ${err}`); | ||
} | ||
} | ||
|
||
public async setItem(key: string, value: any) { | ||
try { | ||
await this.waitForStorageReady(); | ||
await localforage.setItem(key, value); | ||
} catch (err) { | ||
toast.error(`WebStorage setItem("${key}") error: ${err}`); | ||
} | ||
} | ||
|
||
public async removeItem(key: string) { | ||
try { | ||
await this.waitForStorageReady(); | ||
await localforage.removeItem(key); | ||
} catch (err) { | ||
toast.error(`WebStorage removeItem("${key}") error: ${err}`); | ||
} | ||
} | ||
} | ||
|
||
export const webStorage = new WebStorage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.