Make future not hold strong handles

This commit is contained in:
hodasemi 2024-04-04 14:58:54 +02:00
parent 3ecada3b47
commit d1b59e008f

View file

@ -317,10 +317,11 @@ impl States {
&self, &self,
id: impl Into<Option<&'b str>>, id: impl Into<Option<&'b str>>,
) -> Result<Box<dyn FutureStateChange>> { ) -> Result<Box<dyn FutureStateChange>> {
let state = id.into().map(|id| self.get_state(id)).transpose()?; let state: Option<Arc<State>> = id.into().map(|id| self.get_state(id)).transpose()?;
let current_state = self.current_state.clone(); let weak_state = state.map(|s| Arc::downgrade(&s));
let weak_current_state = Arc::downgrade(&self.current_state);
let gui_handler = if self.control_top_gui { let gui_handler = if self.control_top_gui {
Some(self.gui_handler.clone()) Some(Arc::downgrade(&self.gui_handler))
} else { } else {
None None
}; };
@ -328,9 +329,9 @@ impl States {
Ok(Box::new(move || { Ok(Box::new(move || {
Self::_set_state( Self::_set_state(
state.clone(), weak_state.as_ref().map(|w| w.upgrade()).flatten(),
&mut *current_state.lock().unwrap(), &mut *weak_current_state.upgrade().unwrap().lock().unwrap(),
gui_handler.clone(), gui_handler.as_ref().map(|h| h.upgrade()).flatten(),
logging, logging,
) )
})) }))