API: async signal integration migration

This commit is contained in:
Johannes Kirschbauer
2024-07-16 11:28:20 +02:00
parent ce387482bb
commit cc238ecc60
4 changed files with 15 additions and 12 deletions

View File

@@ -29,8 +29,7 @@ class ImplFunc(GObject.Object, Generic[P, B]):
"returns": (GObject.SignalFlags.RUN_FIRST, None, [GResult]),
}
def returns(self, result: B) -> None:
method_name = self.__class__.__name__
def returns(self, method_name: str, result: B) -> None:
if self.op_key is None:
raise ValueError(f"op_key is not set for the function {method_name}")
self.emit("returns", GResult(result, method_name, self.op_key))
@@ -109,7 +108,7 @@ class GObjApi:
def async_run(self, *args: Any, **kwargs: dict[str, Any]) -> bool:
assert plain_method is not None
result = plain_method(*args, **kwargs)
self.returns(result)
self.returns(method_name=name, result=result)
return GLib.SOURCE_REMOVE
return cast(type[ImplFunc], GenericFnRuntime)

View File

@@ -116,7 +116,6 @@ class WebExecutor(GObject.Object):
result = dict()
result["result"] = dataclass_to_dict(data.result)
result["op_key"] = data.op_key
serialized = json.dumps(result, indent=4)
log.debug(f"Result: {serialized}")
# Use idle_add to queue the response call to js on the main GTK thread

View File

@@ -22,14 +22,12 @@ class ApiError:
class SuccessDataClass(Generic[ResponseDataType]):
status: Annotated[Literal["success"], "The status of the response."]
data: ResponseDataType
op_key: str | None
@dataclass
class ErrorDataClass:
status: Literal["error"]
errors: list[ApiError]
op_key: str | None
ApiResponse = SuccessDataClass[ResponseDataType] | ErrorDataClass
@@ -109,11 +107,10 @@ API.register(open_file)
) -> ApiResponse[T]:
try:
data: T = fn(*args, **kwargs)
return SuccessDataClass(status="success", data=data, op_key=op_key)
return SuccessDataClass(status="success", data=data)
except ClanError as e:
return ErrorDataClass(
status="error",
op_key=op_key,
errors=[
ApiError(
message=e.msg,

View File

@@ -20,6 +20,12 @@ export type ErrorData<T extends OperationNames> = Extract<
export type ClanOperations = {
[K in OperationNames]: (str: string) => void;
};
export interface GtkResponse<T> {
result: T;
op_key: string;
}
declare global {
interface Window {
clan: ClanOperations;
@@ -74,9 +80,9 @@ function createFunctions<K extends OperationNames>(
registry[operationName][id] = fn;
window.clan[operationName] = (s: string) => {
const f = (response: OperationResponse<K>) => {
const f = (response: GtkResponse<OperationResponse<K>>) => {
if (response.op_key === id) {
registry[operationName][id](response);
registry[operationName][id](response.result);
}
};
deserialize(f)(s);
@@ -126,10 +132,12 @@ export const callApi = <K extends OperationNames>(
};
const deserialize =
<T>(fn: (response: T) => void) =>
<T>(fn: (response: GtkResponse<T>) => void) =>
(str: string) => {
try {
fn(JSON.parse(str) as T);
const r = JSON.parse(str) as GtkResponse<T>;
console.log("Received: ", r);
fn(r);
} catch (e) {
console.log("Error parsing JSON: ", e);
console.log({ download: () => download("error.json", str) });