changeset 1622:c2065de5f6d0

browser (cache): better handling of roster and identities: roster and identities are now handled and updated separately.
author Goffi <goffi@goffi.org>
date Thu, 15 May 2025 17:52:59 +0200
parents d7c8a986f4fb
children fdb5689fb826
files libervia/web/pages/_browser/cache.py
diffstat 1 files changed, 22 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/web/pages/_browser/cache.py	Tue May 06 00:40:53 2025 +0200
+++ b/libervia/web/pages/_browser/cache.py	Thu May 15 17:52:59 2025 +0200
@@ -9,6 +9,8 @@
 session_uuid = window.session_uuid
 bridge = Bridge()
 async_bridge = AsyncBridge()
+profile = window.profile or ""
+STORAGE_KEY = f"libervia_cache_{profile}"
 
 # XXX: we don't use browser.object_storage because it is affected by
 #   https://github.com/brython-dev/brython/issues/1467 and mixing local_storage.storage
@@ -19,19 +21,29 @@
 class Cache:
 
     def __init__(self):
+        self._cache = {
+            'metadata': {
+                "session_uuid": session_uuid,
+            },
+            'roster': {},
+            'identities': {},
+        }
         try:
-            cache = storage['libervia_cache']
+            cache = storage[STORAGE_KEY]
         except KeyError:
             self.request_data_from_backend()
         else:
             cache = json.loads(cache)
             if cache['metadata']['session_uuid'] != session_uuid:
-                log.debug("data in cache are not valid for this session, resetting")
-                del storage['libervia_cache']
+                log.debug("Data in cache are not valid for this session, resetting.")
+                del storage[STORAGE_KEY]
+                self.request_data_from_backend()
+            elif not cache.get("roster") or not cache.get("identities"):
+                log.debug("Incomplete data in cache, requesting backend.")
                 self.request_data_from_backend()
             else:
                 self._cache = cache
-                log.debug("storage cache is used")
+                log.debug("Storage cache is used.")
 
     @property
     def roster(self):
@@ -44,45 +56,30 @@
     def update(self):
         log.debug(f"updating: {self._cache}")
         # FIXME: workaround in Brython 3.13.0
-        # storage['libervia_cache'] = json.dumps(self._cache)
-        storage['libervia_cache'] = javascript.JSON.stringify(self._cache)
+        # storage[STORAGE_KEY] = json.dumps(self._cache)
+        storage[STORAGE_KEY] = javascript.JSON.stringify(self._cache)
         log.debug("cache stored")
 
-    def _store_if_complete(self):
-        self._completed_count -= 1
-        if self._completed_count == 0:
-            del self._completed_count
-            self.update()
-
     def get_contacts_cb(self, contacts):
         log.debug("roster received")
-        roster = self._cache['roster']
+        roster = self._cache['roster'] = {}
         for contact_jid, attributes, groups in contacts:
             roster[contact_jid] = {
                 'attributes': attributes,
                 'groups': groups,
             }
-        self._store_if_complete()
+        self.update()
 
     def identities_base_get_cb(self, identities_raw):
         log.debug("base identities received")
         identities = json.loads(identities_raw)
-        self._cache['identities'].update(identities)
-        self._store_if_complete()
+        self._cache['identities'] = identities
+        self.update()
 
     def request_failed(self, exc, message):
         notification.show(message.format(exc=exc), "error")
-        self._store_if_complete()
 
     def request_data_from_backend(self):
-        self._cache = {
-            'metadata': {
-                "session_uuid": session_uuid,
-            },
-            'roster': {},
-            'identities': {},
-        }
-        self._completed_count = 2
         log.debug("requesting roster to backend")
         bridge.contacts_get(
             callback=self.get_contacts_cb,