|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""A receiver that will wait indefinitely if there is no underlying receiver. |
| 5 | +
|
| 6 | +The `OptionalReceiver` is useful when the underlying receiver is not set initially. |
| 7 | +Instead of making `if-else` branches to check if the receiver is set, you can use |
| 8 | +this receiver to wait indefinitely if it is not set. |
| 9 | +""" |
| 10 | + |
| 11 | +import asyncio |
| 12 | + |
| 13 | +from typing_extensions import override |
| 14 | + |
| 15 | +from frequenz.channels import Receiver, ReceiverError, ReceiverMessageT_co |
| 16 | + |
| 17 | + |
| 18 | +class OptionalReceiver(Receiver[ReceiverMessageT_co]): |
| 19 | + """A receiver that will wait indefinitely if there is no underlying receiver. |
| 20 | +
|
| 21 | + This receiver is useful when the underlying receiver is not set initially. |
| 22 | + Instead of making `if-else` branches to check if the receiver is set, you can use |
| 23 | + this receiver to wait indefinitely if it is not set. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, receiver: Receiver[ReceiverMessageT_co] | None): |
| 27 | + """Initialize this instance. |
| 28 | +
|
| 29 | + Args: |
| 30 | + receiver: The underlying receiver, or `None` if there is no receiver. |
| 31 | + """ |
| 32 | + self._receiver: Receiver[ReceiverMessageT_co] | None = receiver |
| 33 | + |
| 34 | + @override |
| 35 | + async def ready(self) -> bool: |
| 36 | + """Wait until the receiver is ready with a message or an error. |
| 37 | +
|
| 38 | + Once a call to `ready()` has finished, the message should be read with |
| 39 | + a call to `consume()` (`receive()` or iterated over). The receiver will |
| 40 | + remain ready (this method will return immediately) until it is |
| 41 | + consumed. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Whether the receiver is still active. |
| 45 | + """ |
| 46 | + if self._receiver is not None: |
| 47 | + return await self._receiver.ready() |
| 48 | + |
| 49 | + # If there's no receiver, wait forever |
| 50 | + await asyncio.Event().wait() |
| 51 | + return False |
| 52 | + |
| 53 | + @override |
| 54 | + def consume(self) -> ReceiverMessageT_co: # noqa: DOC503 (raised indirectly) |
| 55 | + """Return the latest from the underlying receiver message once `ready()` is complete. |
| 56 | +
|
| 57 | + `ready()` must be called before each call to `consume()`. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + The next message received. |
| 61 | +
|
| 62 | + Raises: |
| 63 | + ReceiverStoppedError: If the receiver stopped producing messages. |
| 64 | + ReceiverError: If there is some problem with the underlying receiver. |
| 65 | + """ |
| 66 | + if self._receiver is None: |
| 67 | + raise ReceiverError( |
| 68 | + "`consume()` must be preceded by a call to `ready()`", self |
| 69 | + ) |
| 70 | + return self._receiver.consume() |
| 71 | + |
| 72 | + def close(self) -> None: |
| 73 | + """Stop the receiver.""" |
| 74 | + if self._receiver is not None: |
| 75 | + self._receiver.close() |
0 commit comments