From 9e28804a38dfd9f622589e08500a6902d2987a24 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 5 Apr 2024 11:13:08 +0200 Subject: [PATCH] Make statehandle a weak reference --- src/states.rs | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/states.rs b/src/states.rs index 9ecbb1c..ff4e54b 100644 --- a/src/states.rs +++ b/src/states.rs @@ -5,7 +5,7 @@ use assetpath::AssetPath; use std::any::Any; use std::collections::HashMap; -use std::sync::{Arc, Mutex, RwLock}; +use std::sync::{Arc, Mutex, RwLock, Weak}; pub trait FutureStateChange: Fn() -> Result<()> + Send + Sync { fn clone_box<'a>(&self) -> Box @@ -45,74 +45,88 @@ struct State { /// only used for updating callbacks #[derive(Clone)] pub struct StateHandle { - state: Arc, + state: Weak, } impl StateHandle { pub fn update<'a>(&self, update_type: StateUpdateType<'a>) -> Result<()> { - self.state.update(update_type) + self.state.upgrade().unwrap().update(update_type) } } impl GetElement