This commit is contained in:
2025-04-09 16:54:34 +08:00
parent 7cf1c7741d
commit 1d9ab14b01
11 changed files with 650 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
from typing import TypeGuard, override
from app.types_ import AnyScope, LifespanScope, Receive, ReceiveLifespan, Send
from .base import Component as _Component
class LifespanComponent(_Component[LifespanScope, ReceiveLifespan]):
@override
async def condition(self, scope: AnyScope) -> TypeGuard[LifespanScope]:
return scope["type"] == "lifespan"
@override
async def handle(self, scope: LifespanScope, receive: Receive[ReceiveLifespan], send: Send) -> None:
while True:
message = await receive()
if message['type'] == 'lifespan.startup':
... # Do some startup here!
print("Startup...")
await send({'type': 'lifespan.startup.complete'})
elif message['type'] == 'lifespan.shutdown':
... # Do some shutdown here!
print("Shutdown...")
await send({'type': 'lifespan.shutdown.complete'})
return