Skip to content

Commit

Permalink
fix ws streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut committed Dec 12, 2024
1 parent b66277b commit 6dd1e6e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 52 deletions.
21 changes: 9 additions & 12 deletions litestar/handlers/websocket_handlers/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import anyio
from msgspec.json import Encoder as JsonEncoder
from typing_extensions import Self

from litestar.exceptions import ImproperlyConfiguredException, LitestarWarning, WebSocketDisconnect
from litestar.handlers.websocket_handlers.route_handler import WebsocketRouteHandler
Expand All @@ -19,6 +18,7 @@
if TYPE_CHECKING:
from litestar import Litestar, WebSocket
from litestar.dto import AbstractDTO
from litestar.routes import BaseRoute
from litestar.types import Dependencies, EmptyType, ExceptionHandler, Guard, Middleware, TypeEncodersMap
from litestar.types.asgi_types import WebSocketMode

Expand Down Expand Up @@ -183,6 +183,7 @@ async def send_time() -> AsyncGenerator[str, None]:

def decorator(fn: Callable[..., AsyncGenerator[Any, Any]]) -> WebsocketRouteHandler:
return WebSocketStreamHandler(
fn=fn, # type: ignore[arg-type]
path=path,
dependencies=dependencies,
exception_handlers=exception_handlers,
Expand All @@ -194,14 +195,13 @@ def decorator(fn: Callable[..., AsyncGenerator[Any, Any]]) -> WebsocketRouteHand
websocket_class=websocket_class,
return_dto=return_dto,
type_encoders=type_encoders,
**kwargs,
)(
_WebSocketStreamOptions(
stream_options=_WebSocketStreamOptions(
generator_fn=fn,
send_mode=mode,
listen_for_disconnect=listen_for_disconnect,
warn_on_data_discard=warn_on_data_discard,
)
),
**kwargs,
)

return decorator
Expand All @@ -211,12 +211,9 @@ class WebSocketStreamHandler(WebsocketRouteHandler):
__slots__ = ("_ws_stream_options",)
_ws_stream_options: _WebSocketStreamOptions

def __call__(self, fn: _WebSocketStreamOptions) -> Self: # type: ignore[override]
self._ws_stream_options = fn
self._fn = self._ws_stream_options.generator_fn # type: ignore[assignment]
return self
def on_registration(self, app: Litestar, route: BaseRoute) -> None:
self._ws_stream_options = self.opt["stream_options"]

def on_registration(self, app: Litestar) -> None:
parsed_handler_signature = parsed_stream_fn_signature = ParsedSignature.from_fn(
self.fn, self.resolve_signature_namespace()
)
Expand Down Expand Up @@ -293,9 +290,9 @@ async def handler_fn(*args: Any, socket: WebSocket, **kw: Any) -> None:
send_handler=send_handler,
)

self._fn = handler_fn
self.fn = handler_fn

super().on_registration(app)
super().on_registration(app, route)


class _WebSocketStreamOptions:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ docs = [
"sphinx-paramlinks>=0.6.0",
# "litestar-sphinx-theme @ {root:uri}/../litestar-sphinx-theme", # only needed when working on the theme
"litestar-sphinx-theme @ git+https://github.com/litestar-org/litestar-sphinx-theme.git@v3",
"litestar[full] @ {root:uri}/",
"litestar[full]",
"asyncpg",
"psycopg",
]
Expand Down
146 changes: 107 additions & 39 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6dd1e6e

Please sign in to comment.