Skip to content

Commit 4687538

Browse files
authored
refactor(core): drop Option payload type on event::emit (#1760)
1 parent 34b6032 commit 4687538

File tree

4 files changed

+15
-34
lines changed

4 files changed

+15
-34
lines changed

.changes/event-refactor.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
The event `emit` function payload type is now `impl Serialize` instead of `Option<impl Serialize>`.

core/tauri/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
143143
}
144144

145145
/// Emits a event to all windows.
146-
fn emit_all<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> Result<()>
146+
fn emit_all<E: ?Sized, S>(&self, event: &E, payload: S) -> Result<()>
147147
where
148148
P::Event: Borrow<E>,
149149
E: TagRef<P::Event>,
@@ -157,7 +157,7 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
157157
&self,
158158
label: &L,
159159
event: &E,
160-
payload: Option<S>,
160+
payload: S,
161161
) -> Result<()>
162162
where
163163
P::Label: Borrow<L>,

core/tauri/src/manager.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,13 @@ impl<P: Params> WindowManager<P> {
368368
let window = Window::new(manager.clone(), window);
369369
let _ = match event {
370370
FileDropEvent::Hovered(paths) => {
371-
window.emit_internal(&tauri_event::<P::Event>("tauri://file-drop"), Some(paths))
371+
window.emit(&tauri_event::<P::Event>("tauri://file-drop"), Some(paths))
372372
}
373-
FileDropEvent::Dropped(paths) => window.emit_internal(
373+
FileDropEvent::Dropped(paths) => window.emit(
374374
&tauri_event::<P::Event>("tauri://file-drop-hover"),
375375
Some(paths),
376376
),
377-
FileDropEvent::Cancelled => window.emit_internal(
377+
FileDropEvent::Cancelled => window.emit(
378378
&tauri_event::<P::Event>("tauri://file-drop-cancelled"),
379379
Some(()),
380380
),
@@ -596,12 +596,7 @@ impl<P: Params> WindowManager<P> {
596596
window
597597
}
598598

599-
pub fn emit_filter<E: ?Sized, S, F>(
600-
&self,
601-
event: &E,
602-
payload: Option<S>,
603-
filter: F,
604-
) -> crate::Result<()>
599+
pub fn emit_filter<E: ?Sized, S, F>(&self, event: &E, payload: S, filter: F) -> crate::Result<()>
605600
where
606601
P::Event: Borrow<E>,
607602
E: TagRef<P::Event>,

core/tauri/src/window.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::{
2424
};
2525

2626
use serde::Serialize;
27-
use serde_json::Value as JsonValue;
2827

2928
use std::{
3029
borrow::Borrow,
@@ -221,44 +220,26 @@ impl<P: Params> Window<P> {
221220
&self.window.label
222221
}
223222

224-
pub(crate) fn emit_internal<E: ?Sized, S>(
225-
&self,
226-
event: &E,
227-
payload: Option<S>,
228-
) -> crate::Result<()>
223+
/// Emits an event to the current window.
224+
pub fn emit<E: ?Sized, S>(&self, event: &E, payload: S) -> crate::Result<()>
229225
where
230226
P::Event: Borrow<E>,
231227
E: TagRef<P::Event>,
232228
S: Serialize,
233229
{
234-
let js_payload = match payload {
235-
Some(payload_value) => serde_json::to_value(payload_value)?,
236-
None => JsonValue::Null,
237-
};
238-
239230
self.eval(&format!(
240231
"window['{}']({{event: {}, payload: {}}}, '{}')",
241232
self.manager.event_emit_function_name(),
242233
event.to_js_string()?,
243-
js_payload,
234+
serde_json::to_value(payload)?,
244235
self.manager.generate_salt(),
245236
))?;
246237

247238
Ok(())
248239
}
249240

250-
/// Emits an event to the current window.
251-
pub fn emit<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
252-
where
253-
P::Event: Borrow<E>,
254-
E: TagRef<P::Event>,
255-
S: Serialize,
256-
{
257-
self.emit_internal(event, payload)
258-
}
259-
260241
/// Emits an event on all windows except this one.
261-
pub fn emit_others<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
242+
pub fn emit_others<E: ?Sized, S>(&self, event: &E, payload: S) -> crate::Result<()>
262243
where
263244
P::Event: Borrow<E>,
264245
E: TagRef<P::Event>,

0 commit comments

Comments
 (0)