mapcomplete/Logic/Web/LocalStorageSource.ts
2021-03-29 14:10:20 +02:00

28 lines
864 B
TypeScript

import {UIEventSource} from "../UIEventSource";
/**
* UIEventsource-wrapper around localStorage
*/
export class LocalStorageSource {
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
try {
const saved = localStorage.getItem(key);
const source = new UIEventSource<string>(saved ?? defaultValue, "localstorage:"+key);
source.addCallback((data) => {
try{
localStorage.setItem(key, data);
}catch(e){
// Probably exceeded the quota with this item!
// Lets nuke everything
localStorage.clear()
}
});
return source;
} catch (e) {
return new UIEventSource<string>(defaultValue);
}
}
}