#use `listen` with a Rust Frontend

2 messages · Page 1 of 1 (latest)

winged belfry
#

I'm trying to write a Tauri app with a Dioxus Frontend which listens to events from the backend, but I can't seem to work out how to invoke the "listen" function.

In my test case, a Tauri app with a React Frontend, I have the following code

  useEffect(() => {

    let listener: UnlistenFn = () => { };

    const setupListener = async () => {
      listener = await listen("my_event", (event) => setEventText(JSON.stringify(event.payload)));
    };

    setupListener();

    return () => { listener() }

  });

I've attempted to write a wasm_bindgen binding as follows

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = ["window", "__TAURI__", "core"])]
    async fn invoke(cmd: &str, args: JsValue) -> JsValue;

    #[wasm_bindgen(js_namespace = ["window", "__TAURI__", "event"])]
    async fn listen(event: &str, handler: &(dyn Fn(JsValue))) -> JsValue;
}

However, when I call listen, I get the error "closure invoked after being dropped" even when I pass a function pointer to listen and mem::forget the result.
I've also no idea how to cancel the listen.

Alternatively, I'm happy to use a different IPC mechanism, but can't find a guide on how to do IPC with a Rust Frontend.

Any help is much appreciated 🙂

silver bough