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