mapcomplete/Logic/LocalStorageSource.ts
Pieter Vander Vennet e35c85fc55 Various small fixes
2020-08-07 20:50:46 +02:00

22 lines
557 B
TypeScript

import {UIEventSource} from "../UI/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);
}
}
}