From 0958c1d527dbbf12c3726dbf41e0be235f8fbebd Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Thu, 24 Feb 2022 15:17:47 -0500 Subject: [PATCH] Re-add persistence --- web/src/app/Repository.js | 35 +++++++++++++++++------------------ web/src/app/Subscriptions.js | 4 ++++ web/src/components/App.js | 3 +++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/web/src/app/Repository.js b/web/src/app/Repository.js index 7ceb9a7b..6651c039 100644 --- a/web/src/app/Repository.js +++ b/web/src/app/Repository.js @@ -1,42 +1,41 @@ -import {topicUrl} from "./utils"; import Subscription from "./Subscription"; +import Subscriptions from "./Subscriptions"; export class Repository { loadSubscriptions() { console.log(`[Repository] Loading subscriptions from localStorage`); - const subscriptions = {}; - const rawSubscriptions = localStorage.getItem('subscriptions'); - if (rawSubscriptions === null) { - return {}; - } + const subscriptions = new Subscriptions(); + const serialized = localStorage.getItem('subscriptions'); + if (serialized === null) return subscriptions; + try { - const serializedSubscriptions = JSON.parse(rawSubscriptions); + const serializedSubscriptions = JSON.parse(serialized); serializedSubscriptions.forEach(s => { const subscription = new Subscription(s.baseUrl, s.topic); - subscription.notifications = s.notifications; - subscriptions[topicUrl(s.baseUrl, s.topic)] = subscription; + subscription.addNotifications(s.notifications); + subscriptions.add(subscription); }); + console.log(`[Repository] Loaded ${subscriptions.size()} subscription(s) from localStorage`); return subscriptions; } catch (e) { - console.log("LocalStorage", `Unable to deserialize subscriptions: ${e.message}`) - return {}; + console.log(`[Repository] Unable to deserialize subscriptions: ${e.message}`); + return subscriptions; } } saveSubscriptions(subscriptions) { - return; - console.log(`[Repository] Saving subscriptions ${subscriptions} to localStorage`); + console.log(`[Repository] Saving ${subscriptions.size()} subscription(s) to localStorage`); - const serializedSubscriptions = Object.keys(subscriptions).map(k => { - const subscription = subscriptions[k]; + const serialized = JSON.stringify(subscriptions.map( (id, subscription) => { return { baseUrl: subscription.baseUrl, topic: subscription.topic, - notifications: subscription.notifications + notifications: subscription.getNotifications(), + last: subscription.last } - }); - localStorage.setItem('subscriptions', JSON.stringify(serializedSubscriptions)); + })); + localStorage.setItem('subscriptions', serialized); } } diff --git a/web/src/app/Subscriptions.js b/web/src/app/Subscriptions.js index aafe7d5a..916da3a6 100644 --- a/web/src/app/Subscriptions.js +++ b/web/src/app/Subscriptions.js @@ -42,6 +42,10 @@ class Subscriptions { return first; } + size() { + return this.subscriptions.size; + } + clone() { const c = new Subscriptions(); c.subscriptions = new Map(this.subscriptions); diff --git a/web/src/components/App.js b/web/src/components/App.js index 7f91e4ee..89f5b6f9 100644 --- a/web/src/components/App.js +++ b/web/src/components/App.js @@ -153,6 +153,9 @@ const App = () => { const toggleDrawer = () => { setDrawerOpen(!drawerOpen); }; + useEffect(() => { + setSubscriptions(repository.loadSubscriptions()); + }, [/* initial render only */]); useEffect(() => { connectionManager.refresh(subscriptions, handleNotification); repository.saveSubscriptions(subscriptions);