mapcomplete/Logic/LocalStorageSource.ts

24 lines
691 B
TypeScript
Raw Normal View History

import {UIEventSource} from "../UI/UIEventSource";
2020-07-31 14:17:16 +00:00
import {UIElement} from "../UI/UIElement";
export class LocalStorageSource {
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
2020-07-31 14:17:16 +00:00
if (UIElement.runningFromConsole) {
// ignore when running from the console
return new UIEventSource<string>(defaultValue);
2020-07-31 14:17:16 +00:00
}
const saved = localStorage.getItem(key);
const source = new UIEventSource<string>(saved ?? defaultValue);
source.addCallback((data) => {
localStorage.setItem(key, data);
console.log("Wriging ", key, data)
});
return source;
}
}