def typify(self, stack: Stack) -> List[Stack]: """ Appends a typing stack after the given stack, but only if required (aka don't have two typing layers following each other). """ if len(stack.layers) == 1 and isinstance(stack.layers[0], lyr.Typing): return [stack] return [stack, Stack([lyr.Typing()])]
async def pre_handle(self, request: Request, responder: 'Responder'): """ Start typing right when the message is received. """ responder.send([lyr.Typing()]) await responder.flush(request) responder.clear() await self.next(request, responder)
async def flush(self, request: Request, stacks: List[Stack]): """ Add a typing stack after each stack. """ ns: List[Stack] = [] for stack in stacks: ns.extend(self.typify(stack)) if len(ns) > 1 and ns[-1] == Stack([lyr.Typing()]): ns[-1].get_layer(lyr.Typing).active = False await self.next(request, ns)
def test_flush(): args = [] kwargs = {} async def do_flush(*a, **k): args.extend(a) kwargs.update(k) mm = MiddlewareManager.instance() mm.middlewares = [AutoType] flush = mm.get('flush', do_flush) run( flush(None, [ lyr.Stack([lyr.Text('hello')]), lyr.Stack([lyr.Text('wassup')]), ])) assert args == [ None, [ lyr.Stack([ lyr.Text('hello'), ]), lyr.Stack([ lyr.Typing(), ]), lyr.Stack([ lyr.Text('wassup'), ]), lyr.Stack([ lyr.Typing(False), ]), ] ] assert kwargs == {}
def test_typify(): at = AutoType(None) assert at.typify(lyr.stack(lyr.Typing())) == [lyr.stack(lyr.Typing())]