Awful use of localstorage
This commit is contained in:
37
web/src/app/Storage.js
Normal file
37
web/src/app/Storage.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import {topicUrl} from "./utils";
|
||||
import Subscription from "./Subscription";
|
||||
|
||||
const LocalStorage = {
|
||||
getSubscriptions() {
|
||||
const subscriptions = {};
|
||||
const rawSubscriptions = localStorage.getItem('subscriptions');
|
||||
if (rawSubscriptions === null) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
const serializedSubscriptions = JSON.parse(rawSubscriptions);
|
||||
serializedSubscriptions.forEach(s => {
|
||||
const subscription = new Subscription(s.baseUrl, s.topic);
|
||||
subscription.notifications = s.notifications;
|
||||
subscriptions[topicUrl(s.baseUrl, s.topic)] = subscription;
|
||||
});
|
||||
return subscriptions;
|
||||
} catch (e) {
|
||||
console.log("LocalStorage", `Unable to deserialize subscriptions: ${e.message}`)
|
||||
return {};
|
||||
}
|
||||
},
|
||||
saveSubscriptions(subscriptions) {
|
||||
const serializedSubscriptions = Object.keys(subscriptions).map(k => {
|
||||
const subscription = subscriptions[k];
|
||||
return {
|
||||
baseUrl: subscription.baseUrl,
|
||||
topic: subscription.topic,
|
||||
notifications: subscription.notifications
|
||||
}
|
||||
});
|
||||
localStorage.setItem('subscriptions', JSON.stringify(serializedSubscriptions));
|
||||
}
|
||||
};
|
||||
|
||||
export default LocalStorage;
|
||||
@@ -1,10 +1,10 @@
|
||||
|
||||
export default class WsConnection {
|
||||
id = '';
|
||||
constructor(subscription, onNotification) {
|
||||
constructor(subscription, onChange) {
|
||||
this.id = subscription.id;
|
||||
this.subscription = subscription;
|
||||
this.onNotification = onNotification;
|
||||
this.onChange = onChange;
|
||||
this.ws = null;
|
||||
}
|
||||
start() {
|
||||
@@ -26,7 +26,7 @@ export default class WsConnection {
|
||||
}
|
||||
console.log('adding')
|
||||
this.subscription.addNotification(data);
|
||||
this.onNotification(this.subscription);
|
||||
this.onChange(this.subscription);
|
||||
} catch (e) {
|
||||
console.log(this.id, `[message] Error handling message: ${e}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user