2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
2020-07-24 17:53:09 +02:00
|
|
|
|
2021-03-29 14:10:20 +02:00
|
|
|
/**
|
|
|
|
* UIEventsource-wrapper around localStorage
|
|
|
|
*/
|
2020-07-24 17:53:09 +02:00
|
|
|
export class LocalStorageSource {
|
|
|
|
|
|
|
|
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
|
2020-07-31 17:11:44 +02:00
|
|
|
try {
|
|
|
|
const saved = localStorage.getItem(key);
|
2021-01-25 03:12:09 +01:00
|
|
|
const source = new UIEventSource<string>(saved ?? defaultValue, "localstorage:"+key);
|
2020-07-24 17:53:09 +02:00
|
|
|
|
2020-07-31 17:11:44 +02:00
|
|
|
source.addCallback((data) => {
|
2021-03-29 14:10:20 +02:00
|
|
|
try{
|
|
|
|
localStorage.setItem(key, data);
|
|
|
|
}catch(e){
|
|
|
|
// Probably exceeded the quota with this item!
|
|
|
|
// Lets nuke everything
|
|
|
|
localStorage.clear()
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:11:44 +02:00
|
|
|
});
|
|
|
|
return source;
|
|
|
|
} catch (e) {
|
|
|
|
return new UIEventSource<string>(defaultValue);
|
|
|
|
}
|
2020-07-24 17:53:09 +02:00
|
|
|
}
|
2020-07-31 17:11:44 +02:00
|
|
|
}
|