Skip to content

Commit

Permalink
refactor(core): drop Option payload type on event::emit (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 10, 2021
1 parent 34b6032 commit 4687538
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .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>`.
4 changes: 2 additions & 2 deletions core/tauri/src/lib.rs
Expand Up @@ -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>,
Expand All @@ -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>,
Expand Down
13 changes: 4 additions & 9 deletions core/tauri/src/manager.rs
Expand Up @@ -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(()),
),
Expand Down Expand Up @@ -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>,
Expand Down
27 changes: 4 additions & 23 deletions core/tauri/src/window.rs
Expand Up @@ -24,7 +24,6 @@ use crate::{
};

use serde::Serialize;
use serde_json::Value as JsonValue;

use std::{
borrow::Borrow,
Expand Down Expand Up @@ -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>,
Expand Down

0 comments on commit 4687538

Please sign in to comment.