Skip to content

Commit

Permalink
keep only trait implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed May 1, 2024
1 parent 19d3226 commit 5f00e44
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 728 deletions.
137 changes: 6 additions & 131 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,131 +784,6 @@ macro_rules! shared_app_impl {
}
}

/// Event system APIs.
impl<R: Runtime> $app {
/// Listen to an event on this app.
///
/// # Examples
///
/// ```
/// tauri::Builder::default()
/// .setup(|app| {
/// app.listen("component-loaded", move |event| {
/// println!("window just loaded a component");
/// });
///
/// Ok(())
/// });
/// ```
pub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
where
F: Fn(Event) + Send + 'static,
{
self.manager.listen(event.into(), EventTarget::App, handler)
}

/// Listen to an event on this app only once.
///
/// See [`Self::listen`] for more information.
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where
F: FnOnce(Event) + Send + 'static,
{
self.manager.once(event.into(), EventTarget::App, handler)
}

/// Unlisten to an event on this app.
///
/// # Examples
///
/// ```
/// tauri::Builder::default()
/// .setup(|app| {
/// let handler = app.listen("component-loaded", move |event| {
/// println!("app just loaded a component");
/// });
///
/// // stop listening to the event when you do not need it anymore
/// app.unlisten(handler);
///
/// Ok(())
/// });
/// ```
pub fn unlisten(&self, id: EventId) {
self.manager.unlisten(id)
}

/// Emits an event to all [targets](EventTarget).
///
/// # Examples
/// ```
/// #[tauri::command]
/// fn synchronize(app: tauri::AppHandle) {
/// // emits the synchronized event to all webviews
/// app.emit("synchronized", ());
/// }
/// ```
pub fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
self.manager.emit(event, payload)
}

/// Emits an event to all [targets](EventTarget) matching the given target.
///
/// # Examples
/// ```
/// use tauri::EventTarget;
///
/// #[tauri::command]
/// fn download(app: tauri::AppHandle) {
/// for i in 1..100 {
/// std::thread::sleep(std::time::Duration::from_millis(150));
/// // emit a download progress event to all listeners
/// app.emit_to(EventTarget::any(), "download-progress", i);
/// // emit an event to listeners that used App::listen or AppHandle::listen
/// app.emit_to(EventTarget::app(), "download-progress", i);
/// // emit an event to any webview/window/webviewWindow matching the given label
/// app.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled
/// app.emit_to(EventTarget::labeled("updater"), "download-progress", i);
/// // emit an event to listeners that used WebviewWindow::listen
/// app.emit_to(EventTarget::webview_window("updater"), "download-progress", i);
/// }
/// }
/// ```
pub fn emit_to<I, S>(&self, target: I, event: &str, payload: S) -> Result<()>
where
I: Into<EventTarget>,
S: Serialize + Clone,
{
self.manager.emit_to(target, event, payload)
}

/// Emits an event to all [targets](EventTarget) based on the given filter.
///
/// # Examples
/// ```
/// use tauri::EventTarget;
///
/// #[tauri::command]
/// fn download(app: tauri::AppHandle) {
/// for i in 1..100 {
/// std::thread::sleep(std::time::Duration::from_millis(150));
/// // emit a download progress event to the updater window
/// app.emit_filter("download-progress", i, |t| match t {
/// EventTarget::WebviewWindow { label } => label == "main",
/// _ => false,
/// });
/// }
/// }
/// ```
pub fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> Result<()>
where
S: Serialize + Clone,
F: Fn(&EventTarget) -> bool,
{
self.manager.emit_filter(event, payload, filter)
}
}

impl<R: Runtime> Listener<R> for $app {
/// Listen to an event on this app.
///
Expand All @@ -930,7 +805,7 @@ macro_rules! shared_app_impl {
where
F: Fn(Event) + Send + 'static,
{
self.listen(event.into(), handler)
self.manager.listen(event.into(), EventTarget::App, handler)
}

/// Listen to an event on this app only once.
Expand All @@ -940,7 +815,7 @@ macro_rules! shared_app_impl {
where
F: FnOnce(Event) + Send + 'static,
{
self.once(event, handler)
self.manager.once(event.into(), EventTarget::App, handler)
}

/// Unlisten to an event on this app.
Expand All @@ -963,7 +838,7 @@ macro_rules! shared_app_impl {
/// });
/// ```
fn unlisten(&self, id: EventId) {
self.unlisten(id)
self.manager.unlisten(id)
}
}

Expand All @@ -981,7 +856,7 @@ macro_rules! shared_app_impl {
/// }
/// ```
fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
self.emit(event, payload)
self.manager.emit(event, payload)
}

/// Emits an event to all [targets](EventTarget) matching the given target.
Expand Down Expand Up @@ -1011,7 +886,7 @@ macro_rules! shared_app_impl {
I: Into<EventTarget>,
S: Serialize + Clone,
{
self.emit_to(target, event, payload)
self.manager.emit_to(target, event, payload)
}

/// Emits an event to all [targets](EventTarget) based on the given filter.
Expand All @@ -1037,7 +912,7 @@ macro_rules! shared_app_impl {
S: Serialize + Clone,
F: Fn(&EventTarget) -> bool,
{
self.emit_filter(event, payload, filter)
self.manager.emit_filter(event, payload, filter)
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/event/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tauri_runtime::window::is_label_valid;

use crate::plugin::{Builder, TauriPlugin};
use crate::{command, ipc::CallbackFn, EventId, Result, Runtime};
use crate::{AppHandle, Webview};
use crate::{AppHandle, Emitter, Webview};

use super::{is_event_name_valid, EventTarget};

Expand Down
66 changes: 33 additions & 33 deletions core/tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,6 @@ impl<R: Runtime> AppManager<R> {
self.listeners().listen(event, target, handler)
}

pub fn unlisten(&self, id: EventId) {
self.listeners().unlisten(id)
}

pub fn once<F: FnOnce(Event) + Send + 'static>(
&self,
event: String,
Expand All @@ -480,6 +476,10 @@ impl<R: Runtime> AppManager<R> {
self.listeners().once(event, target, handler)
}

pub fn unlisten(&self, id: EventId) {
self.listeners().unlisten(id)
}

#[cfg_attr(
feature = "tracing",
tracing::instrument("app::emit", skip(self, payload))
Expand All @@ -499,6 +499,35 @@ impl<R: Runtime> AppManager<R> {
Ok(())
}

#[cfg_attr(
feature = "tracing",
tracing::instrument("app::emit::filter", skip(self, payload, filter))
)]
pub fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> crate::Result<()>
where
S: Serialize + Clone,
F: Fn(&EventTarget) -> bool,
{
assert_event_name_is_valid(event);

#[cfg(feature = "tracing")]
let _span = tracing::debug_span!("emit::run").entered();
let emit_args = EmitArgs::new(event, payload)?;

let listeners = self.listeners();

listeners.emit_js_filter(
self.webview.webviews_lock().values(),
event,
&emit_args,
Some(&filter),
)?;

listeners.emit_filter(emit_args, Some(filter))?;

Ok(())
}

#[cfg_attr(
feature = "tracing",
tracing::instrument("app::emit::to", skip(self, target, payload), fields(target))
Expand Down Expand Up @@ -531,35 +560,6 @@ impl<R: Runtime> AppManager<R> {
}
}

#[cfg_attr(
feature = "tracing",
tracing::instrument("app::emit::filter", skip(self, payload, filter))
)]
pub fn emit_filter<S, F>(&self, event: &str, payload: S, filter: F) -> crate::Result<()>
where
S: Serialize + Clone,
F: Fn(&EventTarget) -> bool,
{
assert_event_name_is_valid(event);

#[cfg(feature = "tracing")]
let _span = tracing::debug_span!("emit::run").entered();
let emit_args = EmitArgs::new(event, payload)?;

let listeners = self.listeners();

listeners.emit_js_filter(
self.webview.webviews_lock().values(),
event,
&emit_args,
Some(&filter),
)?;

listeners.emit_filter(emit_args, Some(filter))?;

Ok(())
}

pub fn get_window(&self, label: &str) -> Option<Window<R>> {
self.window.windows_lock().get(label).cloned()
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/manager/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
pattern::PatternJavascript,
sealed::ManagerBase,
webview::PageLoadPayload,
AppHandle, EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Webview, Window,
AppHandle, Emitter, EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Webview, Window,
};

use super::{
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/manager/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use tauri_runtime::{
};

use crate::{
app::GlobalWindowEventListener, image::Image, sealed::ManagerBase, AppHandle, EventLoopMessage,
EventTarget, Manager, Runtime, Scopes, Window, WindowEvent,
app::GlobalWindowEventListener, image::Image, sealed::ManagerBase, AppHandle, Emitter,
EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Window, WindowEvent,
};

const WINDOW_RESIZED_EVENT: &str = "tauri://resize";
Expand Down
Loading

0 comments on commit 5f00e44

Please sign in to comment.