File tree Expand file tree Collapse file tree 6 files changed +16
-6
lines changed Expand file tree Collapse file tree 6 files changed +16
-6
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " tauri " : patch
3
+ ---
4
+
5
+ ` Manager::once_global ` and ` Window::once ` allow ` FnOnce ` callbacks.
Original file line number Diff line number Diff line change 4
4
5
5
use std:: {
6
6
boxed:: Box ,
7
+ cell:: Cell ,
7
8
collections:: HashMap ,
8
9
fmt,
9
10
hash:: Hash ,
@@ -170,16 +171,21 @@ impl Listeners {
170
171
}
171
172
172
173
/// 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 > (
174
175
& self ,
175
176
event : String ,
176
177
window : Option < String > ,
177
178
handler : F ,
178
179
) -> EventHandler {
179
180
let self_ = self . clone ( ) ;
181
+ let handler = Cell :: new ( Some ( handler) ) ;
182
+
180
183
self . listen ( event, window, move |event| {
181
184
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)
183
189
} )
184
190
}
185
191
Original file line number Diff line number Diff line change @@ -427,7 +427,7 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
427
427
/// Listen to a global event only once.
428
428
fn once_global < F > ( & self , event : impl Into < String > , handler : F ) -> EventHandler
429
429
where
430
- F : Fn ( Event ) + Send + ' static ,
430
+ F : FnOnce ( Event ) + Send + ' static ,
431
431
{
432
432
self . manager ( ) . once ( event. into ( ) , None , handler)
433
433
}
Original file line number Diff line number Diff line change @@ -1176,7 +1176,7 @@ impl<R: Runtime> WindowManager<R> {
1176
1176
self . inner . listeners . listen ( event, window, handler)
1177
1177
}
1178
1178
1179
- pub fn once < F : Fn ( Event ) + Send + ' static > (
1179
+ pub fn once < F : FnOnce ( Event ) + Send + ' static > (
1180
1180
& self ,
1181
1181
event : String ,
1182
1182
window : Option < String > ,
Original file line number Diff line number Diff line change @@ -480,7 +480,6 @@ pub(crate) fn listener<R: Runtime>(
480
480
window. once ( EVENT_INSTALL_UPDATE , move |_msg| {
481
481
let window = window_isolation. clone ( ) ;
482
482
let updater = updater. clone ( ) ;
483
- let pubkey = pubkey. clone ( ) ;
484
483
485
484
// Start installation
486
485
crate :: async_runtime:: spawn ( async move {
Original file line number Diff line number Diff line change @@ -321,7 +321,7 @@ impl<R: Runtime> Window<R> {
321
321
/// Listen to an event on this window a single time.
322
322
pub fn once < F > ( & self , event : impl Into < String > , handler : F ) -> EventHandler
323
323
where
324
- F : Fn ( Event ) + Send + ' static ,
324
+ F : FnOnce ( Event ) + Send + ' static ,
325
325
{
326
326
let label = self . window . label . clone ( ) ;
327
327
self . manager . once ( event. into ( ) , Some ( label) , handler)
You can’t perform that action at this time.
0 commit comments