Remove the ability to defer websocket message construction (#96734)

This was added in #71364 but all use cases of it were refactored
away so it can now be removed
pull/96775/head
J. Nick Koston 2023-07-16 20:41:39 -10:00 committed by GitHub
parent d553a749a0
commit d242eaa375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 8 deletions

View File

@ -57,7 +57,7 @@ class AuthPhase:
self, self,
logger: WebSocketAdapter, logger: WebSocketAdapter,
hass: HomeAssistant, hass: HomeAssistant,
send_message: Callable[[str | dict[str, Any] | Callable[[], str]], None], send_message: Callable[[str | dict[str, Any]], None],
cancel_ws: CALLBACK_TYPE, cancel_ws: CALLBACK_TYPE,
request: Request, request: Request,
) -> None: ) -> None:

View File

@ -51,7 +51,7 @@ class ActiveConnection:
self, self,
logger: WebSocketAdapter, logger: WebSocketAdapter,
hass: HomeAssistant, hass: HomeAssistant,
send_message: Callable[[str | dict[str, Any] | Callable[[], str]], None], send_message: Callable[[str | dict[str, Any]], None],
user: User, user: User,
refresh_token: RefreshToken, refresh_token: RefreshToken,
) -> None: ) -> None:

View File

@ -95,7 +95,7 @@ class WebSocketHandler:
# to where messages are queued. This allows the implementation # to where messages are queued. This allows the implementation
# to use a deque and an asyncio.Future to avoid the overhead of # to use a deque and an asyncio.Future to avoid the overhead of
# an asyncio.Queue. # an asyncio.Queue.
self._message_queue: deque[str | Callable[[], str] | None] = deque() self._message_queue: deque[str | None] = deque()
self._ready_future: asyncio.Future[None] | None = None self._ready_future: asyncio.Future[None] | None = None
def __repr__(self) -> str: def __repr__(self) -> str:
@ -136,12 +136,11 @@ class WebSocketHandler:
messages_remaining = len(message_queue) messages_remaining = len(message_queue)
# A None message is used to signal the end of the connection # A None message is used to signal the end of the connection
if (process := message_queue.popleft()) is None: if (message := message_queue.popleft()) is None:
return return
debug_enabled = is_enabled_for(logging_debug) debug_enabled = is_enabled_for(logging_debug)
messages_remaining -= 1 messages_remaining -= 1
message = process if isinstance(process, str) else process()
if ( if (
not messages_remaining not messages_remaining
@ -156,9 +155,9 @@ class WebSocketHandler:
messages: list[str] = [message] messages: list[str] = [message]
while messages_remaining: while messages_remaining:
# A None message is used to signal the end of the connection # A None message is used to signal the end of the connection
if (process := message_queue.popleft()) is None: if (message := message_queue.popleft()) is None:
return return
messages.append(process if isinstance(process, str) else process()) messages.append(message)
messages_remaining -= 1 messages_remaining -= 1
joined_messages = ",".join(messages) joined_messages = ",".join(messages)
@ -184,7 +183,7 @@ class WebSocketHandler:
self._peak_checker_unsub = None self._peak_checker_unsub = None
@callback @callback
def _send_message(self, message: str | dict[str, Any] | Callable[[], str]) -> None: def _send_message(self, message: str | dict[str, Any]) -> None:
"""Send a message to the client. """Send a message to the client.
Closes connection if the client is not reading the messages. Closes connection if the client is not reading the messages.