fix(event): let once event return EventId, close #8912 (#8914)

* fix(event): let once event return EventId

* Update .changes/core-once-event-return-event-id.md
This commit is contained in:
Jason Tsai 2024-02-20 23:36:15 +08:00 committed by GitHub
parent 18ff84fc81
commit 3fb414b61a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 15 additions and 9 deletions

View File

@ -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`.

View File

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

View File

@ -159,7 +159,7 @@ impl Listeners {
event: String, event: String,
target: EventTarget, target: EventTarget,
handler: F, handler: F,
) { ) -> EventId {
let self_ = self.clone(); let self_ = self.clone();
let handler = Cell::new(Some(handler)); let handler = Cell::new(Some(handler));
@ -170,7 +170,7 @@ impl Listeners {
.expect("attempted to call handler more than once"); .expect("attempted to call handler more than once");
handler(event); handler(event);
self_.unlisten(id); self_.unlisten(id);
}); })
} }
/// Removes an event listener. /// Removes an event listener.

View File

@ -655,7 +655,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// Listens once to an emitted event to any [target](EventTarget) . /// Listens once to an emitted event to any [target](EventTarget) .
/// ///
/// See [`Self::listen_any`] for more information. /// See [`Self::listen_any`] for more information.
fn once_any<F>(&self, event: impl Into<String>, handler: F) fn once_any<F>(&self, event: impl Into<String>, handler: F) -> EventId
where where
F: FnOnce(Event) + Send + 'static, F: FnOnce(Event) + Send + 'static,
{ {

View File

@ -469,7 +469,7 @@ impl<R: Runtime> AppManager<R> {
event: String, event: String,
target: EventTarget, target: EventTarget,
handler: F, handler: F,
) { ) -> EventId {
assert_event_name_is_valid(&event); assert_event_name_is_valid(&event);
self.listeners().once(event, target, handler) self.listeners().once(event, target, handler)
} }

View File

@ -201,7 +201,7 @@ impl Scope {
} }
/// Listen to an event on this scope and immediately unlisten. /// Listen to an event on this scope and immediately unlisten.
pub fn once<F: FnOnce(&Event) + Send + 'static>(&self, f: F) { pub fn once<F: FnOnce(&Event) + Send + 'static>(&self, f: F) -> ScopeEventId {
let listerners = self.event_listeners.clone(); let listerners = self.event_listeners.clone();
let handler = std::cell::Cell::new(Some(f)); let handler = std::cell::Cell::new(Some(f));
let id = self.next_event_id(); let id = self.next_event_id();
@ -212,6 +212,7 @@ impl Scope {
.expect("attempted to call handler more than once"); .expect("attempted to call handler more than once");
handler(event) handler(event)
}); });
id
} }
/// Removes an event listener on this scope. /// Removes an event listener on this scope.

View File

@ -1460,7 +1460,7 @@ tauri::Builder::default()
/// Listen to an event on this webview only once. /// Listen to an event on this webview only once.
/// ///
/// See [`Self::listen`] for more information. /// See [`Self::listen`] for more information.
pub fn once<F>(&self, event: impl Into<String>, handler: F) pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where where
F: FnOnce(Event) + Send + 'static, F: FnOnce(Event) + Send + 'static,
{ {

View File

@ -1766,7 +1766,7 @@ tauri::Builder::default()
/// Listen to an event on this window webview only once. /// Listen to an event on this window webview only once.
/// ///
/// See [`Self::listen`] for more information. /// See [`Self::listen`] for more information.
pub fn once<F>(&self, event: impl Into<String>, handler: F) pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where where
F: FnOnce(Event) + Send + 'static, F: FnOnce(Event) + Send + 'static,
{ {

View File

@ -1992,7 +1992,7 @@ tauri::Builder::default()
/// Listen to an event on this window only once. /// Listen to an event on this window only once.
/// ///
/// See [`Self::listen`] for more information. /// See [`Self::listen`] for more information.
pub fn once<F>(&self, event: impl Into<String>, handler: F) pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where where
F: FnOnce(Event) + Send + 'static, F: FnOnce(Event) + Send + 'static,
{ {