Skip to content

Commit

Permalink
app.storage.tab is only persistent when using Redis
Browse files Browse the repository at this point in the history
This allows storing non-serializable data in "normal" circumstances.
  • Loading branch information
rodja committed Feb 8, 2025
1 parent 09d0d56 commit d568896
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions nicegui/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Storage:
def __init__(self) -> None:
self._general = Storage._create_persistent_dict('general')
self._users: Dict[str, PersistentDict] = {}
self._tabs: Dict[str, PersistentDict] = {}
self._tabs: Dict[str, ObservableDict] = {}

@staticmethod
def _create_persistent_dict(id: str) -> PersistentDict: # pylint: disable=redefined-builtin
Expand Down Expand Up @@ -163,13 +163,19 @@ def tab(self) -> observables.ObservableDict:
async def _create_tab_storage(self, tab_id: str) -> None:
"""Create tab storage for the given tab ID."""
if tab_id not in self._tabs:
self._tabs[tab_id] = Storage._create_persistent_dict(f'tab-{tab_id}')
await self._tabs[tab_id].initialize()
if Storage.redis_url:
self._tabs[tab_id] = Storage._create_persistent_dict(f'tab-{tab_id}')
await self._tabs[tab_id].initialize()
else:
self._tabs[tab_id] = ObservableDict()

def copy_tab(self, old_tab_id: str, tab_id: str) -> None:
"""Copy the tab storage to a new tab. (For internal use only.)"""
if old_tab_id in self._tabs:
self._tabs[tab_id] = Storage._create_persistent_dict(f'tab-{tab_id}')
if Storage.redis_url:
self._tabs[tab_id] = Storage._create_persistent_dict(f'tab-{tab_id}')
else:
self._tabs[tab_id] = ObservableDict()
self._tabs[tab_id].update(self._tabs[old_tab_id])

async def prune_tab_storage(self) -> None:
Expand Down

0 comments on commit d568896

Please sign in to comment.