From c1a2b03871058067cda13681c87b24979f484913 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 16 Oct 2024 03:02:22 +1000 Subject: [PATCH] Revert "NF: Improve typing of AnkiWebView action (#3475)" (#3504) This reverts commit 18889239d2e72bc5247d73eb957b964372527c79. --- qt/aqt/webview.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index 55b7f3971b5..50c4fdfe7e7 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -8,9 +8,9 @@ import os import re import sys -from collections.abc import Callable +from collections.abc import Callable, Sequence from enum import Enum -from typing import TYPE_CHECKING, Any, Optional, cast +from typing import TYPE_CHECKING, Any, cast import anki import anki.lang @@ -285,7 +285,7 @@ def __init__( self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd self._domDone = True - self._pendingActions: list[Callable[[], None]] = [] + self._pendingActions: list[tuple[str, Sequence[Any]]] = [] self.requiresCol = True self.setPage(self._page) self._disable_zoom = False @@ -395,13 +395,14 @@ def dropEvent(self, evt: QDropEvent) -> None: def setHtml( # type: ignore[override] self, html: str, context: PageContext | None = None ) -> None: + from aqt.mediasrv import PageContext # discard any previous pending actions self._pendingActions = [] self._domDone = True if context is None: context = PageContext.UNKNOWN - self._queueAction(lambda: self._setHtml(html, context)) + self._queueAction("setHtml", html, context) self.set_open_links_externally(True) self.allow_drops = False self.show() @@ -630,10 +631,10 @@ def bundledCSS(self, fname: str) -> str: def eval(self, js: str) -> None: self.evalWithCallback(js, None) - def evalWithCallback(self, js: str, cb: Optional[Callable]) -> None: - self._queueAction(lambda: self._evalWithCallback(js, cb)) + def evalWithCallback(self, js: str, cb: Callable) -> None: + self._queueAction("eval", js, cb) - def _evalWithCallback(self, js: str, cb: Optional[Callable[[Any], Any]]) -> None: + def _evalWithCallback(self, js: str, cb: Callable[[Any], Any]) -> None: if cb: def handler(val: Any) -> None: @@ -646,16 +647,22 @@ def handler(val: Any) -> None: else: self.page().runJavaScript(js) - def _queueAction(self, action: Callable[[], None]) -> None: - self._pendingActions.append(action) + def _queueAction(self, name: str, *args: Any) -> None: + self._pendingActions.append((name, args)) self._maybeRunActions() def _maybeRunActions(self) -> None: if sip.isdeleted(self): return while self._pendingActions and self._domDone: - action = self._pendingActions.pop(0) - action() + name, args = self._pendingActions.pop(0) + + if name == "eval": + self._evalWithCallback(*args) + elif name == "setHtml": + self._setHtml(*args) + else: + raise Exception(f"unknown action: {name}") def _openLinksExternally(self, url: str) -> None: openLink(url)