mapcomplete/Logic/Web/LocalStorageSource.ts
2021-01-25 03:12:09 +01:00

18 lines
572 B
TypeScript

import {UIEventSource} from "../UIEventSource";
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) => {
localStorage.setItem(key, data);
});
return source;
} catch (e) {
return new UIEventSource<string>(defaultValue);
}
}
}