mapcomplete/Logic/Web/IdbLocalStorage.ts

26 lines
666 B
TypeScript
Raw Normal View History

import {UIEventSource} from "../UIEventSource";
import * as idb from "idb-keyval"
/**
* 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)
idb.get(key).then(v => {
src.setData(v ?? options.defaultValue)
})
src.stabilized(1000).addCallback(v => {
idb.set(key, v)
})
return src;
}
public static SetDirectly(key: string, value){
idb.set(key, value)
}
}