JonasKruckenberg / tauri-sys

Bindings to the Tauri API for projects using wasm-bindgen
Apache License 2.0
84 stars 21 forks source link

Help with `event::listener` #15

Closed bicarlsen closed 6 months ago

bicarlsen commented 1 year ago

I am trying to use event::listener to react to events coming from the tauri backend in the frontend, but am having trouble actually consuming the result of event::listen.

Would it be possible to provide a more in-depth example or explanation than the one provided in the docs to help me understand how to use it?

michalvavra commented 1 year ago

I can't provide in-depth explanation, but feel free to look at my tauri-leptos-example repo. I'm using there event::once and event::listen (to continuously subscribe to stream of events).

One thing I've noticed is that you can only consume window-specific events (emitted from Window). It seems to be like that because window_label is required for events in this crate. And it's only being send for window-specific events (by Tauri).

bicarlsen commented 1 year ago

A code snippet that I got running:


use wasm_bindgen_futures::spawn_local;
use futures::stream::StreamExt;
use yew::prelude::*;

#[function_component(MyComponent)]
pub fn my_component() -> Html
    use_effect_with_deps(
            move |_| {
                spawn_local(async move {
                    let mut events = tauri_sys::event::listen::<i32>("tauri://file-drop")
                        .await
                        .expect("could not create `tauri://file-drop` listener");

                    while let Some(event) = events.next().await {
                        web_sys::console::log_1(&event.payload.into());
                    }
            });
        }
    )

    html! {}
}
JonasKruckenberg commented 6 months ago

closing this as it seems to not be an issue anymore?

bicarlsen commented 6 months ago

Yeah, thanks!