Skip to content

Commit

Permalink
feat: implement local storage
Browse files Browse the repository at this point in the history
* feat: propagate local storage into session to keep it into sync
  • Loading branch information
FabienArcellier committed Dec 11, 2024
1 parent 70cddab commit 896cb5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/writer/app_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def _handle_event(self, session: WriterSession, event: WriterEvent) -> EventResp
import traceback as tb

result = session.event_handler.handle(event)
session.process_local_storage_changes()

mutations = {}

Expand Down
16 changes: 16 additions & 0 deletions src/writer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class WriterSession:
"""

def __init__(self, session_id: str, cookies: Optional[Dict[str, str]], headers: Optional[Dict[str, str]], local_storage: Optional[Dict[str, Any]]) -> None:
if local_storage is None:
local_storage = {}

self.session_id = session_id
self.cookies = cookies
self.headers = headers
Expand All @@ -154,6 +157,19 @@ def __init__(self, session_id: str, cookies: Optional[Dict[str, str]], headers:
def update_last_active_timestamp(self) -> None:
self.last_active_timestamp = int(time.time())

def process_local_storage_changes(self) -> None:
"""
Processes the local storage changes.
Changes to local storage are propagated by the mail protocol.
It process the mail messages to keep the representation of local storage in a consistent session
"""
for m in self.session_state.mail:
if m["type"] == "localStorageSetItem":
self.local_storage[m['payload']['key']] = m['payload']['value']
elif m["type"] == "localStorageRemoveItem":
self.local_storage.pop(m['payload']['key'], None)


@dataclasses.dataclass
class MutationSubscription:
Expand Down
1 change: 1 addition & 0 deletions tests/backend/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ def test_get_new_session_proposed(self) -> None:
self.sm.get_new_session(
{"testCookie": "yes"},
{"origin": "example.com"},
{},
self.proposed_session_id
)
self.sm.get_session(self.proposed_session_id)
Expand Down

0 comments on commit 896cb5e

Please sign in to comment.