Skip to content

Commit

Permalink
move window emit/emit_to/emit_filter to the manager implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Oct 10, 2023
1 parent b1dcf8e commit 45c6e6a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
13 changes: 12 additions & 1 deletion core/tauri/src/event/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,18 @@ impl<R: Runtime> Listeners<R> {
if let Some(handlers) = lock.get(event) {
let handlers = handlers
.iter()
.filter(|h| h.1.window.as_ref().map(|w| filter(w)).unwrap_or(true))
.filter(|h| {
h.1
.window
.as_ref()
.map(|w| {
// clippy sees this as redundant closure but
// fixing it will result in a compiler error
#[allow(clippy::redundant_closure)]
filter(w)
})
.unwrap_or(true)
})
.collect::<Vec<_>>();

if !handlers.is_empty() {
Expand Down
11 changes: 4 additions & 7 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
self.manager().package_info()
}

/// Listen to a event triggered on any window ([`Window::trigger`] or [`Window::emit_and_trigger`]) or with [`Self::trigger_global`].
/// Listen to an event emitted on any window.
///
/// # Examples
/// ```
Expand All @@ -561,7 +561,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// #[tauri::command]
/// fn synchronize(window: tauri::Window) {
/// // emits the synchronized event to all windows
/// window.emit_and_trigger("synchronized", ());
/// window.emit("synchronized", ());
/// }
///
/// tauri::Builder::default()
Expand Down Expand Up @@ -618,10 +618,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
self.manager().once(event.into(), None, handler)
}

/// Emits a event to all windows.
///
/// Only the webviews receives this event.
/// To trigger Rust listeners, use [`Self::trigger_global`], [`Window::trigger`] or [`Window::emit_and_trigger`].
/// Emits an event to all windows.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -658,7 +655,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
.emit_filter(event, None, payload, |w| label == w.label())
}

/// Emits an event to the windows based on a filter.
/// Emits an event to specific windows based on a filter.
///
/// # Examples
/// ```
Expand Down
62 changes: 32 additions & 30 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,37 @@ impl<R: Runtime> PartialEq for Window<R> {
}
}

impl<R: Runtime> Manager<R> for Window<R> {}
impl<R: Runtime> Manager<R> for Window<R> {
fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> crate::Result<()> {
self
.manager()
.emit_filter(event, Some(self.label()), payload, |w| {
w.has_js_listener(None, event) || w.has_js_listener(Some(self.label().into()), event)
})?;
Ok(())
}

fn emit_to<S: Serialize + Clone>(
&self,
label: &str,
event: &str,
payload: S,
) -> crate::Result<()> {
self
.manager()
.emit_filter(event, Some(self.label()), payload, |w| label == w.label())
}

fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> crate::Result<()>
where
S: Serialize + Clone,
F: Fn(&Window<R>) -> bool,
{
self
.manager()
.emit_filter(event, Some(self.label()), payload, filter)
}
}
impl<R: Runtime> ManagerBase<R> for Window<R> {
fn manager(&self) -> &WindowManager<R> {
&self.manager
Expand Down Expand Up @@ -2387,36 +2417,8 @@ impl<R: Runtime> Window<R> {

/// Event system APIs.
impl<R: Runtime> Window<R> {
/// Emits an event to the JavaScript listeners on the current window or globally.
///
/// # Examples
/// ```
/// use tauri::Manager;
///
/// #[tauri::command]
/// fn download(window: tauri::Window) {
/// for i in 1..100 {
/// std::thread::sleep(std::time::Duration::from_millis(150));
/// // emit a download progress event to all listeners registed in the webview
/// window.emit("download-progress", i);
/// }
/// }
/// ```
pub fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> crate::Result<()> {
self
.manager()
.emit_filter(event, Some(self.label()), payload, |w| {
w.has_js_listener(None, event) || w.has_js_listener(Some(self.label().into()), event)
})?;
Ok(())
}

/// Listen to an event on this window.
///
/// This listener only receives events that are triggered using the
/// [`trigger`](Window#method.trigger) and [`emit_and_trigger`](Window#method.emit_and_trigger) methods or
/// the `emit` function from the window plugin (`@tauri-apps/plugin-window` package).
///
/// # Examples
/// ```
/// use tauri::Manager;
Expand Down Expand Up @@ -2468,7 +2470,7 @@ impl<R: Runtime> Window<R> {
self.manager.unlisten(handler_id)
}

/// Listen to an event on this window a single time.
/// Listen to an event on this window only once.
///
/// See [`Self::listen`] for more information.
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
Expand Down

0 comments on commit 45c6e6a

Please sign in to comment.