Add more checks in the import helper after user testing

This commit is contained in:
Pieter Vander Vennet 2022-03-24 03:11:29 +01:00
parent 2dac893bb3
commit 9617dbc34d
15 changed files with 344 additions and 94 deletions

View file

@ -8,18 +8,24 @@ import {Utils} from "../../Utils";
export class IdbLocalStorage {
public static Get<T>(key: string, options?: { defaultValue?: T, whenLoaded?: (t: T) => void }): UIEventSource<T> {
public static Get<T>(key: string, options?: { defaultValue?: T, whenLoaded?: (t: T | null) => void }): UIEventSource<T> {
const src = new UIEventSource<T>(options?.defaultValue, "idb-local-storage:" + key)
if (Utils.runningFromConsole) {
return src;
}
src.addCallback(v => idb.set(key, v))
idb.get(key).then(v => {
src.setData(v ?? options?.defaultValue);
if (options?.whenLoaded !== undefined) {
options?.whenLoaded(v)
}
}).catch(err => {
console.warn("Loading from local storage failed due to", err)
if (options?.whenLoaded !== undefined) {
options?.whenLoaded(null)
}
})
src.addCallback(v => idb.set(key, v))
return src;
}