use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pytauri_core::ext_mod::ipc::Invoke;
use pytauri_core::tauri_runtime::Runtime as PyTauriRuntime;
use tauri::ipc;
type IpcInvoke = ipc::Invoke<PyTauriRuntime>;
use crate::gil_runtime::task_with_gil;
use crate::PyInvokeHandlerExt as _;
fn pyfunc(invoke: IpcInvoke) {
task_with_gil(move |py| {
let py_invoke_handler = invoke
.message
.webview_ref()
.try_py_invoke_handler()
.unwrap()
.bind(py)
.clone();
let invoke = match Invoke::new(py, invoke) {
Some(invoke) => invoke,
None => return, };
if let Err(e) = py_invoke_handler.call1((invoke,)) {
let new_err = PyRuntimeError::new_err("`py_invoke_handler` raised an exception");
new_err.set_cause(py, Some(e));
new_err.write_unraisable(py, Some(&py_invoke_handler));
panic!("`py_invoke_handler` shouldn't raise exception");
}
});
}
pub(crate) fn invoke_handler(invoke: IpcInvoke) -> bool {
match invoke.message.command() {
"pyfunc" => {
pyfunc(invoke);
true
}
_ => false,
}
}