2020-07-24 17:53:09 +02:00
|
|
|
import {UIEventSource} from "../UI/UIEventSource";
|
2020-07-31 16:17:16 +02:00
|
|
|
import {UIElement} from "../UI/UIElement";
|
2020-07-24 17:53:09 +02:00
|
|
|
|
|
|
|
export class LocalStorageSource {
|
|
|
|
|
|
|
|
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
|
2020-07-31 16:17:16 +02:00
|
|
|
|
|
|
|
if (UIElement.runningFromConsole) {
|
|
|
|
|
2020-07-26 02:01:34 +02:00
|
|
|
// ignore when running from the console
|
|
|
|
return new UIEventSource<string>(defaultValue);
|
2020-07-31 16:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-24 17:53:09 +02:00
|
|
|
const saved = localStorage.getItem(key);
|
|
|
|
const source = new UIEventSource<string>(saved ?? defaultValue);
|
|
|
|
|
|
|
|
source.addCallback((data) => {
|
|
|
|
localStorage.setItem(key, data);
|
|
|
|
console.log("Wriging ", key, data)
|
|
|
|
});
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
}
|