mapcomplete/Logic/Web/LocalStorageSource.ts

19 lines
551 B
TypeScript
Raw Normal View History

import {UIEventSource} from "../UIEventSource";
export class LocalStorageSource {
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
2020-07-31 15:11:44 +00:00
try {
const saved = localStorage.getItem(key);
const source = new UIEventSource<string>(saved ?? defaultValue);
2020-07-31 15:11:44 +00:00
source.addCallback((data) => {
localStorage.setItem(key, data);
});
return source;
} catch (e) {
return new UIEventSource<string>(defaultValue);
}
}
2020-07-31 15:11:44 +00:00
}