From 455801a641889116a0f3c1f02f142c9a91c63136 Mon Sep 17 00:00:00 2001 From: miruka Date: Sat, 7 Dec 2019 18:45:03 -0400 Subject: [PATCH] Use new way to cancel current login for signin btn --- TODO.md | 2 +- src/python/backend.py | 1 - src/python/utils.py | 39 +---------------------------- src/qml/Base/HBox.qml | 2 ++ src/qml/Pages/AddAccount/SignIn.qml | 30 ++++++++++++---------- 5 files changed, 21 insertions(+), 53 deletions(-) diff --git a/TODO.md b/TODO.md index 56826d71..1bc33dad 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ -- cancel sign in, sign out key icon +- better cancel for all boxes - Media - Confirmation box after picking file to upload - Handle set avatar upload errors diff --git a/src/python/backend.py b/src/python/backend.py index 27e98d02..b2c5c4d0 100644 --- a/src/python/backend.py +++ b/src/python/backend.py @@ -50,7 +50,6 @@ class Backend: # Clients management - @utils.cancel_previous async def login_client(self, user: str, password: str, diff --git a/src/python/utils.py b/src/python/utils.py index 4db2d21a..d5a68ffe 100644 --- a/src/python/utils.py +++ b/src/python/utils.py @@ -1,19 +1,16 @@ """Contains various utilities that are used throughout the package.""" -import asyncio import collections import html import inspect import io -import logging as log import xml.etree.cElementTree as xml_etree # FIXME: bandit warning from datetime import timedelta from enum import Enum from enum import auto as autostr -from functools import wraps from pathlib import Path from types import ModuleType -from typing import Any, Callable, Dict, Tuple, Type +from typing import Any, Dict, Tuple, Type from uuid import UUID import filetype @@ -25,8 +22,6 @@ from nio.crypto import async_generator_from_data Size = Tuple[int, int] auto = autostr -CANCELLABLE_FUTURES: Dict[Tuple[Any, Callable], asyncio.Future] = {} - class AutoStrEnum(Enum): """An Enum where auto() assigns the member's name instead of an int. @@ -159,35 +154,3 @@ def classes_defined_in(module: ModuleType) -> Dict[str, Type]: if not m[0].startswith("_") and m[1].__module__.startswith(module.__name__) } - - -def cancel_previous(async_func): - """When the wrapped coroutine is called, cancel any previous instance - of that coroutine that may still be running. - """ - - @wraps(async_func) - async def wrapper(*args, **kwargs): - try: - arg0_is_self = inspect.getfullargspec(async_func).args[0] == "self" - except IndexError: - parent_obj = None - else: - parent_obj = args[0] if arg0_is_self else None - - previous = CANCELLABLE_FUTURES.get((parent_obj, async_func)) - if previous: - previous.cancel() - log.info("Cancelled previous coro: %s", previous) - - future = asyncio.ensure_future(async_func(*args, **kwargs)) - CANCELLABLE_FUTURES[parent_obj, async_func] = future - - try: - result = await future - return result - finally: - # Make sure to do this even if an exception happens - del CANCELLABLE_FUTURES[parent_obj, async_func] - - return wrapper diff --git a/src/qml/Base/HBox.qml b/src/qml/Base/HBox.qml index d8ad2faa..8aeb4289 100644 --- a/src/qml/Base/HBox.qml +++ b/src/qml/Base/HBox.qml @@ -74,6 +74,8 @@ Rectangle { modelData.enabled === undefined ? true : modelData.enabled + loading: modelData.loading || false + disableWhileLoading: modelData.disableWhileLoading === undefined ? true : modelData.disableWhileLoading diff --git a/src/qml/Pages/AddAccount/SignIn.qml b/src/qml/Pages/AddAccount/SignIn.qml index a9ef31d2..fdad3a81 100644 --- a/src/qml/Pages/AddAccount/SignIn.qml +++ b/src/qml/Pages/AddAccount/SignIn.qml @@ -14,14 +14,18 @@ HBox { text: qsTr("Sign in"), enabled: canSignIn, iconName: "sign-in", - disableWhileLoading: false + loading: loginFuture !== null, + disableWhileLoading: false, }, { name: "cancel", text: qsTr("Cancel"), iconName: "cancel"}, ] buttonCallbacks: ({ apply: button => { - button.loading = true + if (loginFuture) loginFuture.cancel() + + signInTimeout.restart() + errorMessage.text = "" let args = [ @@ -29,12 +33,10 @@ HBox { undefined, serverField.text, ] - signInTimeout.restart() - loginFuture = py.callCoro("login_client", args, userId => { signInTimeout.stop() errorMessage.text = "" - button.loading = false + loginFuture = null py.callCoro( rememberAccount.checked ? @@ -48,13 +50,10 @@ HBox { ) }, type => { - signInTimeout.stop() + if (type === "CancelledError") return - if (type === "CancelledError") { - loginFuture = null - button.loading = false - return - } + loginFuture = null + signInTimeout.stop() let txt = qsTr("Invalid request or login type") @@ -65,11 +64,16 @@ HBox { txt = qsTr("This account was deactivated") errorMessage.text = txt - button.loading = false }) }, - cancel: button => { if (loginFuture) loginFuture.cancel() } + cancel: button => { + if (! loginFuture) return + + signInTimeout.stop() + loginFuture.cancel() + loginFuture = null + } })