async def apply( self, connection: ConnectionAPI) -> AsyncIterator[asyncio.Task[Any]]: """ See LogicAPI.apply() The future returned here will be done when the first of the futures obtained from applying all behaviors of this application is done. """ self.connection = connection async with contextlib.AsyncExitStack() as stack: futures: List[asyncio.Task[Any]] = [] # First apply all the child behaviors for behavior in self._behaviors: if behavior.should_apply_to(connection): fut = await stack.enter_async_context( behavior.apply(connection)) futures.append(fut) # If none of our behaviors were applied, use a never-ending task so that callsites # can wait on it like when behaviors are applied. if not futures: futures.append( create_task(_never_ending_coro(), f'Application/{self.name}/no-behaviors-fut')) # Now register ourselves with the connection. with connection.add_logic(self.name, self): name = f'Application/{self.name}/apply/{connection.remote}' yield create_task(wait_first(futures), name=name)
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]: self.connection = connection async with contextlib.AsyncExitStack() as stack: # First apply all the child behaviors for behavior in self._behaviors: if behavior.should_apply_to(connection): await stack.enter_async_context(behavior.apply(connection)) # Now register ourselves with the connection. with connection.add_logic(self.name, self): yield