Mercurial > libervia-web
diff libervia/web/pages/_browser/cache.py @ 1531:d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
- `json` module has been greatly improved in Brython, the former workaround is not needed
anymore.
- `fill_identities` is now async so an async method can wait for it to complete
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jun 2023 16:35:34 +0200 |
parents | eb00d593801d |
children | 9b451115e726 |
line wrap: on
line diff
--- a/libervia/web/pages/_browser/cache.py Thu Jun 08 23:32:47 2023 +0200 +++ b/libervia/web/pages/_browser/cache.py Thu Jun 22 16:35:34 2023 +0200 @@ -1,11 +1,13 @@ -from browser import window +from browser import window, console as log from browser.local_storage import storage -from javascript import JSON from dialog import notification -from bridge import Bridge +from bridge import Bridge, AsyncBridge +import json +log.warning = log.warn session_uuid = window.session_uuid bridge = Bridge() +async_bridge = AsyncBridge() # 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 @@ -21,7 +23,7 @@ except KeyError: self.request_data_from_backend() else: - cache = JSON.parse(cache) + cache = json.loads(cache) if cache['metadata']['session_uuid'] != session_uuid: print("data in cache are not valid for this session, resetting") del storage['libervia_cache'] @@ -39,10 +41,8 @@ return self._cache['identities'] def update(self): - #Â FIXME: we use window.JSON as a workaround to - # https://github.com/brython-dev/brython/issues/1467 print(f"updating: {self._cache}") - storage['libervia_cache'] = window.JSON.stringify(self._cache) + storage['libervia_cache'] = json.dumps(self._cache) print("cache stored") def _store_if_complete(self): @@ -63,7 +63,7 @@ def identities_base_get_cb(self, identities_raw): print("base identities received") - identities = JSON.parse(identities_raw) + identities = json.loads(identities_raw) self._cache['identities'].update(identities) self._store_if_complete() @@ -91,33 +91,28 @@ errback=lambda e: self.request_failed(e, "Can't get base identities: {exc}") ) - def _fill_identities_cb(self, new_identities_raw, callback): - new_identities = JSON.parse(new_identities_raw) - print(f"new identities: {new_identities.keys()}") - self._cache['identities'].update(new_identities) - self.update() - if callback: - callback() - - def fill_identities(self, entities, callback=None): + async def fill_identities(self, entities) -> None: """Check that identities for entities exist, request them otherwise""" to_get = {e for e in entities if e not in self._cache['identities']} if to_get: - bridge.identities_get( - list(to_get), - ['avatar', 'nicknames'], - callback=lambda identities: self._fill_identities_cb( - identities, callback), - errback=lambda failure_: notification.show( - f"Can't get identities: {failure_}", + try: + new_identities_raw = await async_bridge.identities_get( + list(to_get), + ['avatar', 'nicknames'], + ) + except Exception as e: + notification.show( + f"Can't get identities: {e}", "error" ) - ) + else: + new_identities = json.loads(new_identities_raw) + print(f"new identities: {new_identities.keys()}") + self._cache['identities'].update(new_identities) + self.update() else: # we already have all identities - print("no missing identity") - if callback: - callback() + log.debug("no missing identity") def match_identity(self, entity_jid, text, identity=None): """Returns True if a text match an entity identity