Skip to content

Commit

Permalink
once_global and once accept FnOnce callbacks (#3383)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Feb 10, 2022
1 parent 7918584 commit d5400a3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-once-fnonce.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

`Manager::once_global` and `Window::once` allow `FnOnce` callbacks.
10 changes: 8 additions & 2 deletions core/tauri/src/event.rs
Expand Up @@ -4,6 +4,7 @@

use std::{
boxed::Box,
cell::Cell,
collections::HashMap,
fmt,
hash::Hash,
Expand Down Expand Up @@ -170,16 +171,21 @@ impl Listeners {
}

/// Listen to a JS event and immediately unlisten.
pub(crate) fn once<F: Fn(Event) + Send + 'static>(
pub(crate) fn once<F: FnOnce(Event) + Send + 'static>(
&self,
event: String,
window: Option<String>,
handler: F,
) -> EventHandler {
let self_ = self.clone();
let handler = Cell::new(Some(handler));

self.listen(event, window, move |event| {
self_.unlisten(event.id);
handler(event);
let handler = handler
.take()
.expect("attempted to call handler more than once");
handler(event)
})
}

Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/lib.rs
Expand Up @@ -427,7 +427,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// Listen to a global event only once.
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
where
F: Fn(Event) + Send + 'static,
F: FnOnce(Event) + Send + 'static,
{
self.manager().once(event.into(), None, handler)
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/manager.rs
Expand Up @@ -1176,7 +1176,7 @@ impl<R: Runtime> WindowManager<R> {
self.inner.listeners.listen(event, window, handler)
}

pub fn once<F: Fn(Event) + Send + 'static>(
pub fn once<F: FnOnce(Event) + Send + 'static>(
&self,
event: String,
window: Option<String>,
Expand Down
1 change: 0 additions & 1 deletion core/tauri/src/updater/mod.rs
Expand Up @@ -480,7 +480,6 @@ pub(crate) fn listener<R: Runtime>(
window.once(EVENT_INSTALL_UPDATE, move |_msg| {
let window = window_isolation.clone();
let updater = updater.clone();
let pubkey = pubkey.clone();

// Start installation
crate::async_runtime::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/window.rs
Expand Up @@ -321,7 +321,7 @@ impl<R: Runtime> Window<R> {
/// Listen to an event on this window a single time.
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
where
F: Fn(Event) + Send + 'static,
F: FnOnce(Event) + Send + 'static,
{
let label = self.window.label.clone();
self.manager.once(event.into(), Some(label), handler)
Expand Down

0 comments on commit d5400a3

Please sign in to comment.