tauri-sys
tauri-sys copied to clipboard
JS Binding: JsValue("Command tauri not found") error when trying to emit or listen to events on Tauri v2.0.0-alpha.9
As the title says listening or emitting with tauri 2.0.0-alpha.9 event does not work with error: JS Binding: JsValue("Command tauri not found").
For example with such code:
async fn listen_to_event() -> Result<(), Error> {
let s = listen::<String>("event-name").await?; // Fail here
Ok(())
}
Invoking commands works fine and I have not tested other stuff. I know it's alpha and things can get broken but is there at least some workaround?
I managed to do workaround by manually implementing listen using wasm_bindgen with something like that:
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "event"])]
async fn listen(event: &str, listener: &js_sys::Function) -> JsValue;
}
pub async fn do_something_on_event() -> Result<(), Error> {
let closure = Closure::wrap(Box::new(move |event: JsValue| {
let event: tauri_sys::event::Event<String> = match serde_wasm_bindgen::from_value(event) {
Ok(event) => event,
Err(e) => {
// Some error handling...
return;
}
};
let payload = event.payload;
// Do something with payload
}) as Box<dyn FnMut(JsValue)>);
tauri_sys::tauri::invoke(
"command",
&Payload {
// Payload params
},
)
.await?;
let _ = listen("event", closure.as_ref().unchecked_ref()).await;
closure.forget();
Ok(())
}
I actually end up better for me as I could more adjust event handling. I leave issue opened as lib's function does not work
Please see if Issue #54 and PR #56 resolve this issue.