We should be able to support multiple lifespans just using an AsyncExitStack to accumulate them.
This would be nice in conjunction with #32.
Basically, when we run App._setup as we traverse the routing tree we'd grab lifespans from any other Xpresso App that is mounted and add it to some list of lifespans. Then in App._lifespan we'd put them all into an AsyncExitStack. One change we may want to make is to do a BFS instead of the current DFS, I think it may make the order in which lifespans are entered more reasonable, but not sure it matters, and it would be an implementation detail anyway.
The main reason why this makes sense here and not in Starlette itself is that we are already searching / traversing the routing tree from the root (App) down. Starlette never does this, so adding this functionality to Starlette would be a lot of added complexity, here it is relatively little complexity. A smaller reason is that Starlette's handling of lifespans (putting them in routers and always wrapping them for backwards compatibility) would make it hard to detect when the user actually supplied a lifespan.
We should be able to support multiple lifespans just using an
AsyncExitStack
to accumulate them.This would be nice in conjunction with #32.
Basically, when we run
App._setup
as we traverse the routing tree we'd grab lifespans from any other Xpresso App that is mounted and add it to some list of lifespans. Then inApp._lifespan
we'd put them all into an AsyncExitStack. One change we may want to make is to do a BFS instead of the current DFS, I think it may make the order in which lifespans are entered more reasonable, but not sure it matters, and it would be an implementation detail anyway.The main reason why this makes sense here and not in Starlette itself is that we are already searching / traversing the routing tree from the root (App) down. Starlette never does this, so adding this functionality to Starlette would be a lot of added complexity, here it is relatively little complexity. A smaller reason is that Starlette's handling of lifespans (putting them in routers and always wrapping them for backwards compatibility) would make it hard to detect when the user actually supplied a lifespan.