diff --git a/.changes/core-once-event-return-event-id.md b/.changes/core-once-event-return-event-id.md new file mode 100644 index 000000000..aa9e13b31 --- /dev/null +++ b/.changes/core-once-event-return-event-id.md @@ -0,0 +1,5 @@ +--- +'tauri': 'patch:enhance' +--- + +Return an id when using from `Manager::once_any`, `App::once`, `Window::once`, `Webview::once`, `WebviewWindow::once` and `fs::Scope::once`. diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 6222a4017..f2e82e155 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -845,7 +845,7 @@ macro_rules! shared_app_impl { /// Listen to an event on this app only once. /// /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) + pub fn once(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, { diff --git a/core/tauri/src/event/listener.rs b/core/tauri/src/event/listener.rs index f342483d7..1ae128fd5 100644 --- a/core/tauri/src/event/listener.rs +++ b/core/tauri/src/event/listener.rs @@ -159,7 +159,7 @@ impl Listeners { event: String, target: EventTarget, handler: F, - ) { + ) -> EventId { let self_ = self.clone(); let handler = Cell::new(Some(handler)); @@ -170,7 +170,7 @@ impl Listeners { .expect("attempted to call handler more than once"); handler(event); self_.unlisten(id); - }); + }) } /// Removes an event listener. diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index 15ed37742..b4b341d6e 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -655,7 +655,7 @@ pub trait Manager: sealed::ManagerBase { /// Listens once to an emitted event to any [target](EventTarget) . /// /// See [`Self::listen_any`] for more information. - fn once_any(&self, event: impl Into, handler: F) + fn once_any(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, { diff --git a/core/tauri/src/manager/mod.rs b/core/tauri/src/manager/mod.rs index 23009ae46..026638dd3 100644 --- a/core/tauri/src/manager/mod.rs +++ b/core/tauri/src/manager/mod.rs @@ -469,7 +469,7 @@ impl AppManager { event: String, target: EventTarget, handler: F, - ) { + ) -> EventId { assert_event_name_is_valid(&event); self.listeners().once(event, target, handler) } diff --git a/core/tauri/src/scope/fs.rs b/core/tauri/src/scope/fs.rs index 934f0a584..f9ad18a29 100644 --- a/core/tauri/src/scope/fs.rs +++ b/core/tauri/src/scope/fs.rs @@ -201,7 +201,7 @@ impl Scope { } /// Listen to an event on this scope and immediately unlisten. - pub fn once(&self, f: F) { + pub fn once(&self, f: F) -> ScopeEventId { let listerners = self.event_listeners.clone(); let handler = std::cell::Cell::new(Some(f)); let id = self.next_event_id(); @@ -212,6 +212,7 @@ impl Scope { .expect("attempted to call handler more than once"); handler(event) }); + id } /// Removes an event listener on this scope. diff --git a/core/tauri/src/webview/mod.rs b/core/tauri/src/webview/mod.rs index 545e66fc7..7a20d675b 100644 --- a/core/tauri/src/webview/mod.rs +++ b/core/tauri/src/webview/mod.rs @@ -1460,7 +1460,7 @@ tauri::Builder::default() /// Listen to an event on this webview only once. /// /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) + pub fn once(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, { diff --git a/core/tauri/src/webview/webview_window.rs b/core/tauri/src/webview/webview_window.rs index 8ca8432f8..bb9082b99 100644 --- a/core/tauri/src/webview/webview_window.rs +++ b/core/tauri/src/webview/webview_window.rs @@ -1766,7 +1766,7 @@ tauri::Builder::default() /// Listen to an event on this window webview only once. /// /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) + pub fn once(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, { diff --git a/core/tauri/src/window/mod.rs b/core/tauri/src/window/mod.rs index 60c3813db..6274f41eb 100644 --- a/core/tauri/src/window/mod.rs +++ b/core/tauri/src/window/mod.rs @@ -1992,7 +1992,7 @@ tauri::Builder::default() /// Listen to an event on this window only once. /// /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) + pub fn once(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, {