Re-add persistence

This commit is contained in:
Philipp Heckel 2022-02-24 15:17:47 -05:00
parent fef46823eb
commit 0958c1d527
3 changed files with 24 additions and 18 deletions

View file

@ -1,42 +1,41 @@
import {topicUrl} from "./utils";
import Subscription from "./Subscription"; import Subscription from "./Subscription";
import Subscriptions from "./Subscriptions";
export class Repository { export class Repository {
loadSubscriptions() { loadSubscriptions() {
console.log(`[Repository] Loading subscriptions from localStorage`); console.log(`[Repository] Loading subscriptions from localStorage`);
const subscriptions = {}; const subscriptions = new Subscriptions();
const rawSubscriptions = localStorage.getItem('subscriptions'); const serialized = localStorage.getItem('subscriptions');
if (rawSubscriptions === null) { if (serialized === null) return subscriptions;
return {};
}
try { try {
const serializedSubscriptions = JSON.parse(rawSubscriptions); const serializedSubscriptions = JSON.parse(serialized);
serializedSubscriptions.forEach(s => { serializedSubscriptions.forEach(s => {
const subscription = new Subscription(s.baseUrl, s.topic); const subscription = new Subscription(s.baseUrl, s.topic);
subscription.notifications = s.notifications; subscription.addNotifications(s.notifications);
subscriptions[topicUrl(s.baseUrl, s.topic)] = subscription; subscriptions.add(subscription);
}); });
console.log(`[Repository] Loaded ${subscriptions.size()} subscription(s) from localStorage`);
return subscriptions; return subscriptions;
} catch (e) { } catch (e) {
console.log("LocalStorage", `Unable to deserialize subscriptions: ${e.message}`) console.log(`[Repository] Unable to deserialize subscriptions: ${e.message}`);
return {}; return subscriptions;
} }
} }
saveSubscriptions(subscriptions) { saveSubscriptions(subscriptions) {
return; console.log(`[Repository] Saving ${subscriptions.size()} subscription(s) to localStorage`);
console.log(`[Repository] Saving subscriptions ${subscriptions} to localStorage`);
const serializedSubscriptions = Object.keys(subscriptions).map(k => { const serialized = JSON.stringify(subscriptions.map( (id, subscription) => {
const subscription = subscriptions[k];
return { return {
baseUrl: subscription.baseUrl, baseUrl: subscription.baseUrl,
topic: subscription.topic, topic: subscription.topic,
notifications: subscription.notifications notifications: subscription.getNotifications(),
last: subscription.last
} }
}); }));
localStorage.setItem('subscriptions', JSON.stringify(serializedSubscriptions)); localStorage.setItem('subscriptions', serialized);
} }
} }

View file

@ -42,6 +42,10 @@ class Subscriptions {
return first; return first;
} }
size() {
return this.subscriptions.size;
}
clone() { clone() {
const c = new Subscriptions(); const c = new Subscriptions();
c.subscriptions = new Map(this.subscriptions); c.subscriptions = new Map(this.subscriptions);

View file

@ -153,6 +153,9 @@ const App = () => {
const toggleDrawer = () => { const toggleDrawer = () => {
setDrawerOpen(!drawerOpen); setDrawerOpen(!drawerOpen);
}; };
useEffect(() => {
setSubscriptions(repository.loadSubscriptions());
}, [/* initial render only */]);
useEffect(() => { useEffect(() => {
connectionManager.refresh(subscriptions, handleNotification); connectionManager.refresh(subscriptions, handleNotification);
repository.saveSubscriptions(subscriptions); repository.saveSubscriptions(subscriptions);