mapcomplete/Logic/Web/LocalStorageSource.ts
2020-08-25 14:28:43 +02:00

19 lines
551 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);
source.addCallback((data) => {
localStorage.setItem(key, data);
});
return source;
} catch (e) {
return new UIEventSource<string>(defaultValue);
}
}
}