Skip to content

Commit d5400a3

Browse files
once_global and once accept FnOnce callbacks (#3383)
1 parent 7918584 commit d5400a3

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

.changes/fix-once-fnonce.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
`Manager::once_global` and `Window::once` allow `FnOnce` callbacks.

core/tauri/src/event.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::{
66
boxed::Box,
7+
cell::Cell,
78
collections::HashMap,
89
fmt,
910
hash::Hash,
@@ -170,16 +171,21 @@ impl Listeners {
170171
}
171172

172173
/// Listen to a JS event and immediately unlisten.
173-
pub(crate) fn once<F: Fn(Event) + Send + 'static>(
174+
pub(crate) fn once<F: FnOnce(Event) + Send + 'static>(
174175
&self,
175176
event: String,
176177
window: Option<String>,
177178
handler: F,
178179
) -> EventHandler {
179180
let self_ = self.clone();
181+
let handler = Cell::new(Some(handler));
182+
180183
self.listen(event, window, move |event| {
181184
self_.unlisten(event.id);
182-
handler(event);
185+
let handler = handler
186+
.take()
187+
.expect("attempted to call handler more than once");
188+
handler(event)
183189
})
184190
}
185191

core/tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
427427
/// Listen to a global event only once.
428428
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
429429
where
430-
F: Fn(Event) + Send + 'static,
430+
F: FnOnce(Event) + Send + 'static,
431431
{
432432
self.manager().once(event.into(), None, handler)
433433
}

core/tauri/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ impl<R: Runtime> WindowManager<R> {
11761176
self.inner.listeners.listen(event, window, handler)
11771177
}
11781178

1179-
pub fn once<F: Fn(Event) + Send + 'static>(
1179+
pub fn once<F: FnOnce(Event) + Send + 'static>(
11801180
&self,
11811181
event: String,
11821182
window: Option<String>,

core/tauri/src/updater/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ pub(crate) fn listener<R: Runtime>(
480480
window.once(EVENT_INSTALL_UPDATE, move |_msg| {
481481
let window = window_isolation.clone();
482482
let updater = updater.clone();
483-
let pubkey = pubkey.clone();
484483

485484
// Start installation
486485
crate::async_runtime::spawn(async move {

core/tauri/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl<R: Runtime> Window<R> {
321321
/// Listen to an event on this window a single time.
322322
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
323323
where
324-
F: Fn(Event) + Send + 'static,
324+
F: FnOnce(Event) + Send + 'static,
325325
{
326326
let label = self.window.label.clone();
327327
self.manager.once(event.into(), Some(label), handler)

0 commit comments

Comments
 (0)