Skip to content

Commit

Permalink
Restore teardown_websocket methods
Browse files Browse the repository at this point in the history
These were mistakenly removed when basing Quart on Flask.
  • Loading branch information
pgjones committed Apr 1, 2024
1 parent 0da9e21 commit 153617a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/quart/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,26 @@ async def func(response):
self.after_websocket_funcs[None].append(func)
return func

@setupmethod
def teardown_websocket(
self,
func: T_teardown,
) -> T_teardown:
"""Add a teardown websocket function.
This is designed to be used as a decorator, if used to
decorate a synchronous function, the function will be wrapped
in :func:`~quart.utils.run_sync` and run in a thread executor
(with the wrapped function returned). An example usage,
.. code-block:: python
@app.teardown_websocket
async def func():
...
Arguments:
func: The teardown websocket function itself.
"""
self.teardown_websocket_funcs[None].append(func)
return func

async def handle_http_exception(
self, error: HTTPException
) -> HTTPException | ResponseReturnValue:
Expand Down
24 changes: 24 additions & 0 deletions src/quart/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
self.before_websocket_funcs: t.Dict[AppOrBlueprintKey, t.List[BeforeWebsocketCallable]] = (
defaultdict(list)
)
self.teardown_websocket_funcs: dict[AppOrBlueprintKey, list[TeardownCallable]] = (
defaultdict(list)
)

def get_send_file_max_age(self, filename: str | None) -> int | None:
"""Used by :func:`send_file` to determine the ``max_age`` cache
Expand Down Expand Up @@ -263,6 +266,27 @@ async def func(response):
self.after_websocket_funcs[None].append(func)
return func

@setupmethod
def teardown_websocket(
self,
func: T_teardown,
) -> T_teardown:
"""Add a teardown websocket function.
This is designed to be used as a decorator, if used to
decorate a synchronous function, the function will be wrapped
in :func:`~quart.utils.run_sync` and run in a thread executor
(with the wrapped function returned). An example usage,
.. code-block:: python
@app.teardown_websocket
async def func():
...
Arguments:
func: The teardown websocket function itself.
name: Optional blueprint key name.
"""
self.teardown_websocket_funcs[None].append(func)
return func

@setupmethod
def before_app_websocket(self, func: T_before_websocket) -> T_before_websocket:
"""Add a before websocket to the App.
Expand Down

0 comments on commit 153617a

Please sign in to comment.