1- from typing import Callable , Dict , Union
1+ from typing import Callable , Union
22
33from .http_request import AsgiHttpRequest
44from .http_response import AsgiHttpResponse
@@ -47,15 +47,13 @@ async def _get_http_response(self, method: str, path: str, request: AsgiHttpRequ
4747 return AsgiHttpResponse (status = bolt_response .status , headers = bolt_response .headers , body = bolt_response .body )
4848 return AsgiHttpResponse (status = 404 , headers = {"content-type" : ["text/plain;charset=utf-8" ]}, body = "Not Found" )
4949
50- async def _handle_lifespan (self , receive : Callable ) -> Dict [str , str ]:
51- while True :
52- lifespan = await receive ()
53- if lifespan ["type" ] == "lifespan.startup" :
54- """Do something before startup"""
55- return {"type" : "lifespan.startup.complete" }
56- if lifespan ["type" ] == "lifespan.shutdown" :
57- """Do something before shutdown"""
58- return {"type" : "lifespan.shutdown.complete" }
50+ async def _handle_lifespan (self , receive : Callable , send : Callable ) -> None :
51+ message = await receive ()
52+ if message ["type" ] == "lifespan.startup" :
53+ await send ({"type" : "lifespan.startup.complete" })
54+ message = await receive ()
55+ if message ["type" ] == "lifespan.shutdown" :
56+ await send ({"type" : "lifespan.shutdown.complete" })
5957
6058 async def __call__ (self , scope : scope_type , receive : Callable , send : Callable ) -> None :
6159 if scope ["type" ] == "http" :
@@ -66,6 +64,6 @@ async def __call__(self, scope: scope_type, receive: Callable, send: Callable) -
6664 await send (response .get_response_body ())
6765 return
6866 if scope ["type" ] == "lifespan" :
69- await send ( await self ._handle_lifespan (receive ) )
67+ await self ._handle_lifespan (receive , send )
7068 return
7169 raise TypeError (f"Unsupported scope type: { scope ['type' ]!r} " )
0 commit comments