mapcomplete/Logic/LocalStorageSource.ts

23 lines
624 B
TypeScript
Raw Normal View History

import {UIEventSource} from "../UI/UIEventSource";
export class LocalStorageSource {
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
2020-07-31 14:17:16 +00:00
2020-07-31 15:11:44 +00:00
try {
2020-07-31 14:17:16 +00:00
2020-07-31 15:11:44 +00:00
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);
console.log("Writing to local storage", key, data)
2020-07-31 15:11:44 +00:00
});
return source;
} catch (e) {
return new UIEventSource<string>(defaultValue);
}
}
2020-07-31 15:11:44 +00:00
}