Skip to content

Commit

Permalink
need a general get publisher_notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Apr 1, 2024
1 parent ff79a96 commit a00015c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a00015c

Please sign in to comment.