Fixing LaTeX tests, adding new sexy natural language method spy, started testing Promises.

This commit is contained in:
Adsooi 2024-10-16 05:38:49 +02:00
parent a85a4721e3
commit 34caf20593
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
12 changed files with 511 additions and 35 deletions
runtime-pyside6/LogarithmPlotter/util

View file

@ -75,6 +75,7 @@ class PyJSValue:
return value
def type(self) -> any:
ret = None
matcher = [
(lambda: self.qjs_value.isArray(), list),
(lambda: self.qjs_value.isBool(), bool),
@ -93,8 +94,9 @@ class PyJSValue:
]
for (test, value) in matcher:
if test():
return value
return None
ret = value
break
return ret
def primitive(self):
"""

View file

@ -31,7 +31,7 @@ def check_callable(function: Callable|QJSValue) -> Callable|None:
"""
if isinstance(function, QJSValue) and function.isCallable():
return PyJSValue(function)
elif isinstance(function, Callable):
elif callable(function):
return function
return None
@ -51,9 +51,7 @@ class PyPromiseRunner(QRunnable):
def run(self):
try:
data = self.runner(*self.args)
if isinstance(data, QObject):
data = data
elif type(data) in [int, str, float, bool, bytes]:
if type(data) in [int, str, float, bool]:
data = QJSValue(data)
elif data is None:
data = QJSValue.SpecialValue.UndefinedValue
@ -62,7 +60,7 @@ class PyPromiseRunner(QRunnable):
elif isinstance(data, PyJSValue):
data = data.qjs_value
else:
raise InvalidReturnValue("Must return either a primitive, a valid QObject, JS Value, or None.")
raise InvalidReturnValue("Must return either a primitive, a JS Value, or None.")
self.promise.fulfilled.emit(data)
except Exception as e:
try:
@ -79,7 +77,7 @@ class PyPromise(QObject):
Runs to_run in another thread, and calls fulfilled (populated by then) with its return value.
"""
fulfilled = Signal((QJSValue,), (QObject,))
rejected = Signal(Exception)
rejected = Signal(str)
def __init__(self, to_run: Callable|QJSValue, args=[], start_automatically=True):
QObject.__init__(self)
@ -94,7 +92,7 @@ class PyPromise(QObject):
raise ValueError("New PyPromise created with invalid function")
self._runner = PyPromiseRunner(to_run, self, args)
if start_automatically:
self._start()
self.start()
@Slot()
def start(self, *args, **kwargs):
@ -122,6 +120,35 @@ class PyPromise(QObject):
if on_reject is not None:
self._rejects.append(on_reject)
return self
def calls_upon_fulfillment(self, function: Callable | QJSValue) -> bool:
"""
Returns True if the given function will be callback upon the promise fulfillment.
False otherwise.
"""
return self._calls_in(function, self._fulfills)
def calls_upon_rejection(self, function: Callable | QJSValue) -> bool:
"""
Returns True if the given function will be callback upon the promise rejection.
False otherwise.
"""
return self._calls_in(function, self._rejects)
def _calls_in(self, function: Callable | QJSValue, within: list) -> bool:
"""
Returns True if the given function resides in the given within list, False otherwise.
Internal method of calls_upon_fulfill
"""
function = check_callable(function)
ret = False
if isinstance(function, PyJSValue):
found = next((f for f in within if f.qjs_value == function.qjs_value), None)
ret = found is not None
elif callable(function):
found = next((f for f in within if f == function), None)
ret = found is not None
return ret
@Slot(QJSValue)
@Slot(QObject)