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

This commit is contained in:
Lucas Fernandes Nogueira 2021-05-09 22:50:08 -03:00 committed by GitHub
parent 34b6032df7
commit 4687538987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 34 deletions

View File

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

View File

@ -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>,

View File

@ -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>,

View File

@ -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>,