Skip to content

Commit 3fb414b

Browse files
authored
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
1 parent 18ff84f commit 3fb414b

File tree

9 files changed

+15
-9
lines changed

9 files changed

+15
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:enhance'
3+
---
4+
5+
Return an id when using from `Manager::once_any`, `App::once`, `Window::once`, `Webview::once`, `WebviewWindow::once` and `fs::Scope::once`.

core/tauri/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ macro_rules! shared_app_impl {
845845
/// Listen to an event on this app only once.
846846
///
847847
/// See [`Self::listen`] for more information.
848-
pub fn once<F>(&self, event: impl Into<String>, handler: F)
848+
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
849849
where
850850
F: FnOnce(Event) + Send + 'static,
851851
{

core/tauri/src/event/listener.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl Listeners {
159159
event: String,
160160
target: EventTarget,
161161
handler: F,
162-
) {
162+
) -> EventId {
163163
let self_ = self.clone();
164164
let handler = Cell::new(Some(handler));
165165

@@ -170,7 +170,7 @@ impl Listeners {
170170
.expect("attempted to call handler more than once");
171171
handler(event);
172172
self_.unlisten(id);
173-
});
173+
})
174174
}
175175

176176
/// Removes an event listener.

core/tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
655655
/// Listens once to an emitted event to any [target](EventTarget) .
656656
///
657657
/// See [`Self::listen_any`] for more information.
658-
fn once_any<F>(&self, event: impl Into<String>, handler: F)
658+
fn once_any<F>(&self, event: impl Into<String>, handler: F) -> EventId
659659
where
660660
F: FnOnce(Event) + Send + 'static,
661661
{

core/tauri/src/manager/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<R: Runtime> AppManager<R> {
469469
event: String,
470470
target: EventTarget,
471471
handler: F,
472-
) {
472+
) -> EventId {
473473
assert_event_name_is_valid(&event);
474474
self.listeners().once(event, target, handler)
475475
}

core/tauri/src/scope/fs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl Scope {
201201
}
202202

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

217218
/// Removes an event listener on this scope.

core/tauri/src/webview/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ tauri::Builder::default()
14601460
/// Listen to an event on this webview only once.
14611461
///
14621462
/// See [`Self::listen`] for more information.
1463-
pub fn once<F>(&self, event: impl Into<String>, handler: F)
1463+
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
14641464
where
14651465
F: FnOnce(Event) + Send + 'static,
14661466
{

core/tauri/src/webview/webview_window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ tauri::Builder::default()
17661766
/// Listen to an event on this window webview only once.
17671767
///
17681768
/// See [`Self::listen`] for more information.
1769-
pub fn once<F>(&self, event: impl Into<String>, handler: F)
1769+
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
17701770
where
17711771
F: FnOnce(Event) + Send + 'static,
17721772
{

core/tauri/src/window/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ tauri::Builder::default()
19921992
/// Listen to an event on this window only once.
19931993
///
19941994
/// See [`Self::listen`] for more information.
1995-
pub fn once<F>(&self, event: impl Into<String>, handler: F)
1995+
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
19961996
where
19971997
F: FnOnce(Event) + Send + 'static,
19981998
{

0 commit comments

Comments
 (0)