Skip to content

Commit

Permalink
Revert "NF: Improve typing of AnkiWebView action (#3475)" (#3504)
Browse files Browse the repository at this point in the history
This reverts commit 1888923.
  • Loading branch information
dae authored Oct 15, 2024
1 parent 25070c5 commit c1a2b03
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions qt/aqt/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit c1a2b03

Please sign in to comment.