2021-11-15 11:51:32 +01:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import * as idb from "idb-keyval"
|
2022-01-06 14:39:42 +01:00
|
|
|
import {Utils} from "../../Utils";
|
2022-01-18 18:52:42 +01:00
|
|
|
|
2021-11-15 11:51:32 +01:00
|
|
|
/**
|
|
|
|
* UIEventsource-wrapper around indexedDB key-value
|
|
|
|
*/
|
|
|
|
export class IdbLocalStorage {
|
|
|
|
|
|
|
|
|
|
|
|
public static Get<T>(key: string, options: { defaultValue?: T }): UIEventSource<T>{
|
|
|
|
const src = new UIEventSource<T>(options.defaultValue, "idb-local-storage:"+key)
|
2022-01-06 14:39:42 +01:00
|
|
|
if(Utils.runningFromConsole){
|
|
|
|
return src;
|
|
|
|
}
|
2021-11-16 02:57:26 +01:00
|
|
|
idb.get(key).then(v => src.setData(v ?? options.defaultValue))
|
|
|
|
src.addCallback(v => idb.set(key, v))
|
2021-11-15 11:51:32 +01:00
|
|
|
return src;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SetDirectly(key: string, value){
|
|
|
|
idb.set(key, value)
|
|
|
|
}
|
|
|
|
|
2021-11-16 02:57:26 +01:00
|
|
|
static GetDirectly(key: string) {
|
|
|
|
return idb.get(key)
|
|
|
|
}
|
2021-11-15 11:51:32 +01:00
|
|
|
}
|