Skip to content

Commit

Permalink
fix(event): let once event return EventId, close #8912 (#8914)
Browse files Browse the repository at this point in the history
* fix(event): let once event return EventId

* Update .changes/core-once-event-return-event-id.md
  • Loading branch information
pewsheen committed Feb 20, 2024
1 parent 18ff84f commit 3fb414b
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changes/core-once-event-return-event-id.md
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(&self, event: impl Into<String>, handler: F)
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where
F: FnOnce(Event) + Send + 'static,
{
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/event/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Listeners {
event: String,
target: EventTarget,
handler: F,
) {
) -> EventId {
let self_ = self.clone();
let handler = Cell::new(Some(handler));

Expand All @@ -170,7 +170,7 @@ impl Listeners {
.expect("attempted to call handler more than once");
handler(event);
self_.unlisten(id);
});
})
}

/// Removes an event listener.
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// Listens once to an emitted event to any [target](EventTarget) .
///
/// 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
F: FnOnce(Event) + Send + 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl<R: Runtime> AppManager<R> {
event: String,
target: EventTarget,
handler: F,
) {
) -> EventId {
assert_event_name_is_valid(&event);
self.listeners().once(event, target, handler)
}
Expand Down
3 changes: 2 additions & 1 deletion core/tauri/src/scope/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Scope {
}

/// 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 handler = std::cell::Cell::new(Some(f));
let id = self.next_event_id();
Expand All @@ -212,6 +212,7 @@ impl Scope {
.expect("attempted to call handler more than once");
handler(event)
});
id
}

/// Removes an event listener on this scope.
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(&self, event: impl Into<String>, handler: F)
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where
F: FnOnce(Event) + Send + 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(&self, event: impl Into<String>, handler: F)
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where
F: FnOnce(Event) + Send + 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(&self, event: impl Into<String>, handler: F)
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where
F: FnOnce(Event) + Send + 'static,
{
Expand Down

0 comments on commit 3fb414b

Please sign in to comment.