tauri_sys/
event.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! The event system allows you to emit events to the backend and listen to events from it.

use futures::{
    channel::{mpsc, oneshot},
    Future, FutureExt, Stream, StreamExt,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::fmt::Debug;
use wasm_bindgen::{prelude::Closure, JsValue};

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Event<T> {
    /// Event name
    pub event: String,
    /// Event identifier used to unlisten
    pub id: f32,
    /// Event payload
    pub payload: T,
    /// The label of the window that emitted this event
    pub window_label: Option<String>,
}

/// Emits an event to the backend.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_api::event::emit;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Payload {
///     logged_in: bool,
///     token: String
/// }
///
/// emit("frontend-loaded", &Payload { logged_in: true, token: "authToken" }).await;
/// ```
///
/// @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
#[inline(always)]
pub async fn emit<T: Serialize>(event: &str, payload: &T) -> crate::Result<()> {
    inner::emit(event, serde_wasm_bindgen::to_value(payload)?).await?;

    Ok(())
}

/// Listen to an event from the backend.
/// 
/// The returned Future will automatically clean up it's underlying event listener when dropped, so no manual unlisten function needs to be called.
/// See [Differences to the JavaScript API](../index.html#differences-to-the-javascript-api) for details.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_api::event::listen;
/// use web_sys::console;
///
/// let events = listen::<String>("error");
///
/// while let Some(event) = events.next().await {
///     console::log_1(&format!("Got error in window {}, payload: {}", event.window_label, event.payload).into());
/// }
/// ```
#[inline(always)]
pub async fn listen<T>(event: &str) -> crate::Result<impl Stream<Item = Event<T>>>
where
    T: DeserializeOwned + 'static,
{
    let (tx, rx) = mpsc::unbounded::<Event<T>>();

    let closure = Closure::<dyn FnMut(JsValue)>::new(move |raw| {
        let _ = tx.unbounded_send(serde_wasm_bindgen::from_value(raw).unwrap());
    });
    let unlisten = inner::listen(event, &closure).await?;
    closure.forget();

    Ok(Listen {
        rx,
        unlisten: js_sys::Function::from(unlisten),
    })
}

pub(crate) struct Listen<T> {
    pub rx: mpsc::UnboundedReceiver<T>,
    pub unlisten: js_sys::Function,
}

impl<T> Drop for Listen<T> {
    fn drop(&mut self) {
        log::debug!("Calling unlisten for listen callback");
        self.unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
    }
}

impl<T> Stream for Listen<T> {
    type Item = T;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        self.rx.poll_next_unpin(cx)
    }
}

/// Listen to an one-off event from the backend.
///
/// The returned Future will automatically clean up it's underlying event listener when dropped, so no manual unlisten function needs to be called.
/// See [Differences to the JavaScript API](../index.html#differences-to-the-javascript-api) for details.
/// 
/// # Example
///
/// ```rust,no_run
/// use tauri_api::event::once;
/// use serde::Deserialize;
/// use web_sys::console;
///
/// #[derive(Deserialize)]
/// interface LoadedPayload {
///   logged_in: bool,
///   token: String
/// }
/// 
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// const event = once::<LoadedPayload>("loaded").await?;
/// 
/// console::log_1!(&format!("App is loaded, loggedIn: {}, token: {}", event.payload.logged_in, event.payload.token).into());
/// # Ok(())
/// # }
/// ```
#[inline(always)]
pub async fn once<T>(event: &str) -> crate::Result<Event<T>>
where
    T: DeserializeOwned + 'static,
{
    let (tx, rx) = oneshot::channel::<Event<T>>();

    let closure: Closure<dyn FnMut(JsValue)> = Closure::once(move |raw| {
        let _ = tx.send(serde_wasm_bindgen::from_value(raw).unwrap());
    });
    let unlisten = inner::once(event, &closure).await?;
    closure.forget();

    let fut = Once {
        rx,
        unlisten: js_sys::Function::from(unlisten),
    };

    fut.await
}

pub(crate) struct Once<T> {
    pub rx: oneshot::Receiver<Event<T>>,
    pub unlisten: js_sys::Function,
}

impl<T> Drop for Once<T> {
    fn drop(&mut self) {
        self.rx.close();
        log::debug!("Calling unlisten for once callback");
        self.unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
    }
}

impl<T> Future for Once<T> {
    type Output = crate::Result<Event<T>>;

    fn poll(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        self.rx.poll_unpin(cx).map_err(Into::into)
    }
}

mod inner {
    use wasm_bindgen::{
        prelude::{wasm_bindgen, Closure},
        JsValue,
    };

    #[wasm_bindgen(module = "/src/event.js")]
    extern "C" {
        #[wasm_bindgen(catch)]
        pub async fn emit(event: &str, payload: JsValue) -> Result<(), JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn listen(
            event: &str,
            handler: &Closure<dyn FnMut(JsValue)>,
        ) -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn once(
            event: &str,
            handler: &Closure<dyn FnMut(JsValue)>,
        ) -> Result<JsValue, JsValue>;
    }
}