make lifespan into a dataclass

This commit is contained in:
Khaleel Al-Adhami 2024-10-30 15:58:34 -07:00
parent 948d35f539
commit ea8b32d4b7

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio import asyncio
import contextlib import contextlib
import dataclasses
import functools import functools
import inspect import inspect
from typing import Callable, Coroutine, Set, Union from typing import Callable, Coroutine, Set, Union
@ -16,11 +17,14 @@ from reflex.utils.exceptions import InvalidLifespanTaskType
from .mixin import AppMixin from .mixin import AppMixin
@dataclasses.dataclass
class LifespanMixin(AppMixin): class LifespanMixin(AppMixin):
"""A Mixin that allow tasks to run during the whole app lifespan.""" """A Mixin that allow tasks to run during the whole app lifespan."""
# Lifespan tasks that are planned to run. # Lifespan tasks that are planned to run.
lifespan_tasks: Set[Union[asyncio.Task, Callable]] = set() lifespan_tasks: Set[Union[asyncio.Task, Callable]] = dataclasses.field(
default_factory=set
)
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def _run_lifespan_tasks(self, app: FastAPI): async def _run_lifespan_tasks(self, app: FastAPI):