Selaa lähdekoodia

refactor(core): drop `Option` payload type on `event::emit` (#1760)

Lucas Fernandes Nogueira 4 vuotta sitten
vanhempi
sitoutus
4687538987
4 muutettua tiedostoa jossa 15 lisäystä ja 34 poistoa
  1. 5 0
      .changes/event-refactor.md
  2. 2 2
      core/tauri/src/lib.rs
  3. 4 9
      core/tauri/src/manager.rs
  4. 4 23
      core/tauri/src/window.rs

+ 5 - 0
.changes/event-refactor.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+The event `emit` function payload type is now `impl Serialize` instead of `Option<impl Serialize>`. 

+ 2 - 2
core/tauri/src/lib.rs

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

+ 4 - 9
core/tauri/src/manager.rs

@@ -368,13 +368,13 @@ impl<P: Params> WindowManager<P> {
         let window = Window::new(manager.clone(), window);
         let _ = match event {
           FileDropEvent::Hovered(paths) => {
-            window.emit_internal(&tauri_event::<P::Event>("tauri://file-drop"), Some(paths))
+            window.emit(&tauri_event::<P::Event>("tauri://file-drop"), Some(paths))
           }
-          FileDropEvent::Dropped(paths) => window.emit_internal(
+          FileDropEvent::Dropped(paths) => window.emit(
             &tauri_event::<P::Event>("tauri://file-drop-hover"),
             Some(paths),
           ),
-          FileDropEvent::Cancelled => window.emit_internal(
+          FileDropEvent::Cancelled => window.emit(
             &tauri_event::<P::Event>("tauri://file-drop-cancelled"),
             Some(()),
           ),
@@ -596,12 +596,7 @@ impl<P: Params> WindowManager<P> {
     window
   }
 
-  pub fn emit_filter<E: ?Sized, S, F>(
-    &self,
-    event: &E,
-    payload: Option<S>,
-    filter: F,
-  ) -> crate::Result<()>
+  pub fn emit_filter<E: ?Sized, S, F>(&self, event: &E, payload: S, filter: F) -> crate::Result<()>
   where
     P::Event: Borrow<E>,
     E: TagRef<P::Event>,

+ 4 - 23
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<P: Params> Window<P> {
     &self.window.label
   }
 
-  pub(crate) fn emit_internal<E: ?Sized, S>(
-    &self,
-    event: &E,
-    payload: Option<S>,
-  ) -> crate::Result<()>
+  /// Emits an event to the current window.
+  pub fn emit<E: ?Sized, S>(&self, event: &E, payload: S) -> crate::Result<()>
   where
     P::Event: Borrow<E>,
     E: TagRef<P::Event>,
     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<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
-  where
-    P::Event: Borrow<E>,
-    E: TagRef<P::Event>,
-    S: Serialize,
-  {
-    self.emit_internal(event, payload)
-  }
-
   /// Emits an event on all windows except this one.
-  pub fn emit_others<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
+  pub fn emit_others<E: ?Sized, S>(&self, event: &E, payload: S) -> crate::Result<()>
   where
     P::Event: Borrow<E>,
     E: TagRef<P::Event>,