From 4687538987af7638e8625342f2e3d065771c12c7 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sun, 9 May 2021 22:50:08 -0300 Subject: [PATCH] refactor(core): drop `Option` payload type on `event::emit` (#1760) --- .changes/event-refactor.md | 5 +++++ core/tauri/src/lib.rs | 4 ++-- core/tauri/src/manager.rs | 13 ++++--------- core/tauri/src/window.rs | 27 ++++----------------------- 4 files changed, 15 insertions(+), 34 deletions(-) create mode 100644 .changes/event-refactor.md diff --git a/.changes/event-refactor.md b/.changes/event-refactor.md new file mode 100644 index 000000000..c950f5114 --- /dev/null +++ b/.changes/event-refactor.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +The event `emit` function payload type is now `impl Serialize` instead of `Option`. diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index e05f5fa5d..0fadf07bf 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -143,7 +143,7 @@ pub trait Manager: sealed::ManagerBase

{ } /// Emits a event to all windows. - fn emit_all(&self, event: &E, payload: Option) -> Result<()> + fn emit_all(&self, event: &E, payload: S) -> Result<()> where P::Event: Borrow, E: TagRef, @@ -157,7 +157,7 @@ pub trait Manager: sealed::ManagerBase

{ &self, label: &L, event: &E, - payload: Option, + payload: S, ) -> Result<()> where P::Label: Borrow, diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index e68a26ae7..6b3ef2d73 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -368,13 +368,13 @@ impl WindowManager

{ let window = Window::new(manager.clone(), window); let _ = match event { FileDropEvent::Hovered(paths) => { - window.emit_internal(&tauri_event::("tauri://file-drop"), Some(paths)) + window.emit(&tauri_event::("tauri://file-drop"), Some(paths)) } - FileDropEvent::Dropped(paths) => window.emit_internal( + FileDropEvent::Dropped(paths) => window.emit( &tauri_event::("tauri://file-drop-hover"), Some(paths), ), - FileDropEvent::Cancelled => window.emit_internal( + FileDropEvent::Cancelled => window.emit( &tauri_event::("tauri://file-drop-cancelled"), Some(()), ), @@ -596,12 +596,7 @@ impl WindowManager

{ window } - pub fn emit_filter( - &self, - event: &E, - payload: Option, - filter: F, - ) -> crate::Result<()> + pub fn emit_filter(&self, event: &E, payload: S, filter: F) -> crate::Result<()> where P::Event: Borrow, E: TagRef, diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 433d6eeb1..cd5203e6a 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -24,7 +24,6 @@ use crate::{ }; use serde::Serialize; -use serde_json::Value as JsonValue; use std::{ borrow::Borrow, @@ -221,44 +220,26 @@ impl Window

{ &self.window.label } - pub(crate) fn emit_internal( - &self, - event: &E, - payload: Option, - ) -> crate::Result<()> + /// Emits an event to the current window. + pub fn emit(&self, event: &E, payload: S) -> crate::Result<()> where P::Event: Borrow, E: TagRef, S: Serialize, { - let js_payload = match payload { - Some(payload_value) => serde_json::to_value(payload_value)?, - None => JsonValue::Null, - }; - self.eval(&format!( "window['{}']({{event: {}, payload: {}}}, '{}')", self.manager.event_emit_function_name(), event.to_js_string()?, - js_payload, + serde_json::to_value(payload)?, self.manager.generate_salt(), ))?; Ok(()) } - /// Emits an event to the current window. - pub fn emit(&self, event: &E, payload: Option) -> crate::Result<()> - where - P::Event: Borrow, - E: TagRef, - S: Serialize, - { - self.emit_internal(event, payload) - } - /// Emits an event on all windows except this one. - pub fn emit_others(&self, event: &E, payload: Option) -> crate::Result<()> + pub fn emit_others(&self, event: &E, payload: S) -> crate::Result<()> where P::Event: Borrow, E: TagRef,