async def edit_loop(self): timeout = loop.sleep(1000 * 1000 * 1) touch = loop.wait(io.TOUCH) wait_timeout = loop.spawn(touch, timeout) wait_touch = loop.spawn(touch) content = None self.back.taint() self.input.taint() while content is None: self.render() if self.pbutton is not None: wait = wait_timeout else: wait = wait_touch result = await wait if touch in wait.finished: event, *pos = result content = self.touch(event, pos) else: if self.input.word: # just reset the pending state self.edit(self.input.content) else: # invalid character, backspace it self.edit(self.input.content[:-1]) return content
def wait(self, *tasks): ''' Wait until one of the passed tasks finishes, and return the result, while servicing the wire context. If a message comes until one of the tasks ends, `UnexpectedMessageError` is raised. ''' return loop.spawn(self.read(()), *tasks)
def wait(self, *tasks: Awaitable) -> Any: """ Wait until one of the passed tasks finishes, and return the result, while servicing the wire context. If a message comes until one of the tasks ends, `UnexpectedMessageError` is raised. """ return loop.spawn(self.read(None), *tasks)
async def __iter__(self): self.edit(self.input.content) # init button state while True: change = self.change_page() enter = self.enter_text() wait = loop.spawn(change, enter) result = await wait if enter in wait.finished: return result
async def handle_input(self): touch = loop.wait(io.TOUCH) timeout = loop.sleep(1000 * 1000 * 1) spawn_touch = loop.spawn(touch) spawn_timeout = loop.spawn(touch, timeout) while True: if self.pending_button is not None: spawn = spawn_timeout else: spawn = spawn_touch result = await spawn if touch in spawn.finished: event, x, y = result self.dispatch(event, x, y) else: self.on_timeout()
def spawn(workflow: loop.Task) -> loop.spawn: """Spawn a workflow task. Creates an instance of loop.spawn for the workflow and registers it into the workflow management system. """ task = loop.spawn(workflow) _on_start(task) task.set_finalizer(_on_close) return task
async def paginate(render_page, page_count, page=0, *args): while True: changer = change_page(page, page_count) renderer = render_page(page, page_count, *args) waiter = loop.spawn(changer, renderer) result = await waiter if changer in waiter.finished: page = result else: return result
async def enter_text(self): timeout = loop.sleep(1000 * 1000 * 1) touch = loop.wait(io.TOUCH) wait_timeout = loop.spawn(touch, timeout) wait_touch = loop.spawn(touch) content = None while content is None: self.render() if self.pbutton is not None: wait = wait_timeout else: wait = wait_touch result = await wait if touch in wait.finished: event, *pos = result content = self.touch(event, pos) else: # disable the pending buttons self.edit(self.input.content) return content
def start_default() -> None: """Start a default workflow. Use `set_default` to set the default workflow constructor. If a default task is already running, nothing will happen. """ global default_task global default_constructor assert default_constructor is not None if not default_task: default_task = loop.spawn(default_constructor()) if __debug__: log.debug(__name__, "start default: %s", default_task.task) default_task.set_finalizer(_finalize_default) else: if __debug__: log.debug(__name__, "default already started")