File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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 44
55use 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
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 > ,
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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)
You can’t perform that action at this time.
0 commit comments