mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-01 03:02:28 +03:00
refactor(core): drop Option
payload type on event::emit
(#1760)
This commit is contained in:
parent
34b6032df7
commit
4687538987
5
.changes/event-refactor.md
Normal file
5
.changes/event-refactor.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
The event `emit` function payload type is now `impl Serialize` instead of `Option<impl Serialize>`.
|
@ -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>,
|
||||
|
@ -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>,
|
||||
|
@ -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>,
|
||||
|
Loading…
Reference in New Issue
Block a user