From a00015c8dd8adfac912869aee70b7cf04830012f Mon Sep 17 00:00:00 2001 From: Jamey Huffnagle Date: Mon, 1 Apr 2024 12:52:56 -0400 Subject: [PATCH] need a general get publisher_notifier --- .../notifications/publisher_notifier.py | 45 ++++++++++++------- .../notifications/test_publisher_notifier.py | 6 +-- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/robot-server/robot_server/service/notifications/publisher_notifier.py b/robot-server/robot_server/service/notifications/publisher_notifier.py index 02b36ed9601..d1769ac4379 100644 --- a/robot-server/robot_server/service/notifications/publisher_notifier.py +++ b/robot-server/robot_server/service/notifications/publisher_notifier.py @@ -23,20 +23,20 @@ def __init__( self._pe_notifier: Optional[asyncio.Task[None]] = None self._callbacks: List[Callable[[], Awaitable[None]]] = [] - async def initialize(self) -> None: - """Initializes an instance of PublisherNotifier. This method should only be called once.""" - self._pe_notifier = asyncio.create_task(self._wait_for_event()) - - def notify_publishers(self) -> None: - """A generic notifier, alerting all `waiters` of a change.""" - self._change_notifier.notify() - def register_publish_callbacks( self, callbacks: List[Callable[[], Awaitable[None]]] ): """Extend the list of callbacks with a given list of callbacks.""" self._callbacks.extend(callbacks) + async def _initialize(self) -> None: + """Initializes an instance of PublisherNotifier. This method should only be called once.""" + self._pe_notifier = asyncio.create_task(self._wait_for_event()) + + def _notify_publishers(self) -> None: + """A generic notifier, alerting all `waiters` of a change.""" + self._change_notifier.notify() + async def _wait_for_event(self) -> None: """Indefinitely wait for an event to occur, then invoke each callback.""" while True: @@ -50,15 +50,14 @@ async def _wait_for_event(self) -> None: ]("publisher_notifier") -async def initialize_publisher_notifier(app_state: AppState) -> None: - """Create a new `NotificationClient` and store it on `app_state`. - - Intended to be called just once, when the server starts up. - """ - publisher_notifier: PublisherNotifier = PublisherNotifier() - _publisher_notifier_accessor.set_on(app_state, publisher_notifier) +def get_publisher_notifier( + app_state: AppState = Depends(get_app_state), +) -> PublisherNotifier: + """Intended for use by various publishers only.""" + publisher_notifier = _publisher_notifier_accessor.get_from(app_state) + assert publisher_notifier is not None - await publisher_notifier.initialize() + return publisher_notifier def get_notify_publishers( @@ -67,4 +66,16 @@ def get_notify_publishers( """Provides access to the callback used to notify publishers of changes.""" publisher_notifier = _publisher_notifier_accessor.get_from(app_state) assert isinstance(publisher_notifier, PublisherNotifier) - return publisher_notifier.notify_publishers + + return publisher_notifier._notify_publishers + + +async def initialize_publisher_notifier(app_state: AppState) -> None: + """Create a new `NotificationClient` and store it on `app_state`. + + Intended to be called just once, when the server starts up. + """ + publisher_notifier: PublisherNotifier = PublisherNotifier() + _publisher_notifier_accessor.set_on(app_state, publisher_notifier) + + await publisher_notifier._initialize() diff --git a/robot-server/tests/service/notifications/test_publisher_notifier.py b/robot-server/tests/service/notifications/test_publisher_notifier.py index 43b00d15fd9..125cfdd1806 100644 --- a/robot-server/tests/service/notifications/test_publisher_notifier.py +++ b/robot-server/tests/service/notifications/test_publisher_notifier.py @@ -11,7 +11,7 @@ async def test_initialize() -> None: """It should create a new task.""" publisher_notifier = PublisherNotifier() - await publisher_notifier.initialize() + await publisher_notifier._initialize() assert asyncio.get_running_loop() @@ -21,7 +21,7 @@ def test_notify_publishers() -> None: change_notifier = MagicMock() publisher_notifier = PublisherNotifier(change_notifier) - publisher_notifier.notify_publishers() + publisher_notifier._notify_publishers() change_notifier.notify.assert_called_once() @@ -64,7 +64,7 @@ async def trigger_callbacks() -> None: await asyncio.sleep(0.1) change_notifier.notify() - task = asyncio.create_task(publisher_notifier.initialize()) + task = asyncio.create_task(publisher_notifier._initialize()) await asyncio.gather(trigger_callbacks(), task)