Skip to content

Commit

Permalink
Move all_sessions to broker:
Browse files Browse the repository at this point in the history
This also makes the protocol objects Session and Target take a reference
to the broker. We no longer manage an overall dictionary of sessions,
that's now that brokers job.
  • Loading branch information
ayjayt committed Jan 15, 2025
1 parent f0b1228 commit af5a6db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
3 changes: 1 addition & 2 deletions choreographer/browser_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def __init__(self, path=None, *, browser_cls=Chromium, channel_cls=Pipe, **kwarg
self._make_lock()
self.tabs = {}
self.targets = {}
self._all_sessions = {}
# Compose Resources
self._channel = channel_cls()
self._broker = BrokerSync(self, self._channel)
Expand All @@ -86,7 +85,7 @@ def open(self):
**self._browser_impl.get_popen_args(),
)
super().__init__("0", self)
self._add_session(self._session_type(self, ""))
self._add_session(self._session_type(self._broker, ""))

def __enter__(self):
"""Open browser as context to launch on entry and close on exit."""
Expand Down
23 changes: 14 additions & 9 deletions choreographer/protocol/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
class SessionSync:
"""A session is a single conversation with a single target."""

def __init__(self, browser, session_id):
def __init__(self, broker, session_id):
"""
Construct a session from the browser as an object.
A session is like an open conversation with a target.
All commands are sent on sessions.
Args:
browser: a reference to the main browser
broker: a reference to the browser's broker
session_id: the id given by the browser
"""
if not isinstance(session_id, str):
raise TypeError("session_id must be a string")
# Resources
self.browser = browser
self.broker = broker

# State
self.session_id = session_id
Expand Down Expand Up @@ -55,7 +55,7 @@ def send_command(self, command, params=None):
_logger.debug(
f"Sending {command} with {params} on session {self.session_id}",
)
return self.browser.broker.send_json(json_command)
return self.broker.send_json(json_command)


class TargetSync:
Expand All @@ -64,12 +64,19 @@ class TargetSync:
_session_type = SessionSync
"""Like generic typing<>. This is the session type associated with TargetSync."""

def __init__(self, target_id, browser):
"""Create a target after one ahs been created by the browser."""
def __init__(self, target_id, broker):
"""
Create a target after one ahs been created by the browser.
Args:
broker: a reference to the browser's broker
target_id: the id given by the browser
"""
if not isinstance(target_id, str):
raise TypeError("target_id must be string")
# Resources
self.browser = browser
self.broker = broker

# States
self.sessions = {}
Expand All @@ -80,13 +87,11 @@ def _add_session(self, session):
if not isinstance(session, self._session_type):
raise TypeError("session must be a session type class")
self.sessions[session.session_id] = session
self.browser.all_sessions[session.session_id] = session

def _remove_session(self, session_id):
if isinstance(session_id, self._session_type):
session_id = session_id.session_id
_ = self.sessions.pop(session_id, None)
_ = self.browser.all_sessions.pop(session_id, None)

def _get_first_session(self):
if not self.sessions.values():
Expand Down

0 comments on commit af5a6db

Please sign in to comment.