diff options
author | 2025-06-01 20:17:57 +0100 | |
---|---|---|
committer | 2025-06-01 20:17:57 +0100 | |
commit | 33bb7130943b5f74b3b0f08c5e6d8f7c5e54d4c0 (patch) | |
tree | 0617888d360550508ede95aa12c00375cde4e70b /src/state_store.rs | |
parent | 6ee4190a26f32bfa953302ee363ad3bb6c384ebb (diff) | |
download | macaw-web-33bb7130943b5f74b3b0f08c5e6d8f7c5e54d4c0.tar.gz macaw-web-33bb7130943b5f74b3b0f08c5e6d8f7c5e54d4c0.tar.bz2 macaw-web-33bb7130943b5f74b3b0f08c5e6d8f7c5e54d4c0.zip |
feat: switch to leptos-fetch for state management
Diffstat (limited to 'src/state_store.rs')
-rw-r--r-- | src/state_store.rs | 237 |
1 files changed, 0 insertions, 237 deletions
diff --git a/src/state_store.rs b/src/state_store.rs index 2536cda..8b13789 100644 --- a/src/state_store.rs +++ b/src/state_store.rs @@ -1,238 +1 @@ -use std::{collections::HashMap, ops::{Deref, DerefMut}, sync::{Arc, RwLock}}; -use leptos::prelude::*; - -// TODO: get rid of this -// V has to be an arc signal -#[derive(Debug)] -pub struct ArcStateStore<K, V> { - store: Arc<RwLock<HashMap<K, (V, usize)>>>, -} - -impl<K, V> PartialEq for ArcStateStore<K, V> { - fn eq(&self, other: &Self) -> bool { - Arc::ptr_eq(&self.store, &other.store) - } -} - -impl<K, V> Clone for ArcStateStore<K, V> { - fn clone(&self) -> Self { - Self { - store: Arc::clone(&self.store), - } - } -} - -impl<K, V> Eq for ArcStateStore<K, V> {} - -impl<K, V> ArcStateStore<K, V> { - pub fn new() -> Self { - Self { - store: Arc::new(RwLock::new(HashMap::new())), - } - } -} - -#[derive(Debug)] -pub struct StateStore<K, V, S = SyncStorage> { - inner: ArenaItem<ArcStateStore<K, V>, S>, -} - -impl<K, V, S> Dispose for StateStore<K, V, S> { - fn dispose(self) { - self.inner.dispose() - } -} - -impl<K, V> StateStore<K, V> -where - K: Send + Sync + 'static, - V: Send + Sync + 'static, -{ - pub fn new() -> Self { - Self::new_with_storage() - } -} - -impl<K, V, S> StateStore<K, V, S> -where - K: 'static, - V: 'static, - S: Storage<ArcStateStore<K, V>>, -{ - pub fn new_with_storage() -> Self { - Self { - inner: ArenaItem::new_with_storage(ArcStateStore::new()), - } - } -} - -impl<K, V> StateStore<K, V, LocalStorage> -where - K: 'static, - V: 'static, -{ - pub fn new_local() -> Self { - Self::new_with_storage() - } -} - -impl< - K: std::marker::Send + std::marker::Sync + 'static, - V: std::marker::Send + std::marker::Sync + 'static, -> From<ArcStateStore<K, V>> for StateStore<K, V> -{ - fn from(value: ArcStateStore<K, V>) -> Self { - Self { - inner: ArenaItem::new_with_storage(value), - } - } -} - -impl<K: 'static, V: 'static> FromLocal<ArcStateStore<K, V>> for StateStore<K, V, LocalStorage> { - fn from_local(value: ArcStateStore<K, V>) -> Self { - Self { - inner: ArenaItem::new_with_storage(value), - } - } -} - -impl<K, V, S> Copy for StateStore<K, V, S> {} - -impl<K, V, S> Clone for StateStore<K, V, S> { - fn clone(&self) -> Self { - *self - } -} - -impl<K: Eq + std::hash::Hash + Clone, V: Clone> StateStore<K, V> -where - K: Send + Sync + 'static, - V: Send + Sync + 'static, -{ - pub fn store(&self, key: K, value: V) -> StateListener<K, V> { - { - let store = self.inner.try_get_value().unwrap(); - let mut store = store.store.write().unwrap(); - if let Some((v, count)) = store.get_mut(&key) { - *v = value.clone(); - *count += 1; - } else { - store.insert(key.clone(), (value.clone(), 1)); - } - }; - StateListener { - value, - cleaner: StateCleaner { - key, - state_store: self.clone(), - }, - } - } -} - -impl<K, V> StateStore<K, V> -where - K: Eq + std::hash::Hash + Send + Sync + 'static, - V: Send + Sync + 'static, -{ - pub fn update(&self, key: &K, value: V) { - let store = self.inner.try_get_value().unwrap(); - let mut store = store.store.write().unwrap(); - if let Some((v, _)) = store.get_mut(key) { - *v = value; - } - } - - pub fn modify(&self, key: &K, modify: impl Fn(&mut V)) { - let store = self.inner.try_get_value().unwrap(); - let mut store = store.store.write().unwrap(); - if let Some((v, _)) = store.get_mut(key) { - modify(v); - } - } - - fn remove(&self, key: &K) { - // let store = self.inner.try_get_value().unwrap(); - // let mut store = store.store.write().unwrap(); - // if let Some((_v, count)) = store.get_mut(key) { - // *count -= 1; - // if *count == 0 { - // store.remove(key); - // debug!("dropped item from store"); - // } - // } - } -} - -#[derive(Clone)] -pub struct StateListener<K, V> -where - K: Eq + std::hash::Hash + 'static + std::marker::Send + std::marker::Sync, - V: 'static + std::marker::Send + std::marker::Sync, -{ - value: V, - cleaner: StateCleaner<K, V>, -} - -impl< - K: std::cmp::Eq + std::hash::Hash + std::marker::Send + std::marker::Sync, - V: std::marker::Send + std::marker::Sync, -> Deref for StateListener<K, V> -{ - type Target = V; - - fn deref(&self) -> &Self::Target { - &self.value - } -} - -impl<K: std::cmp::Eq + std::hash::Hash + Send + Sync, V: Send + Sync> DerefMut - for StateListener<K, V> -{ - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.value - } -} - -struct ArcStateCleaner<K, V> { - key: K, - state_store: ArcStateStore<K, V>, -} - -struct StateCleaner<K, V> -where - K: Eq + std::hash::Hash + Send + Sync + 'static, - V: Send + Sync + 'static, -{ - key: K, - state_store: StateStore<K, V>, -} - -impl<K, V> Clone for StateCleaner<K, V> -where - K: Eq + std::hash::Hash + Clone + Send + Sync, - V: Send + Sync, -{ - fn clone(&self) -> Self { - { - let store = self.state_store.inner.try_get_value().unwrap(); - let mut store = store.store.write().unwrap(); - if let Some((_v, count)) = store.get_mut(&self.key) { - *count += 1; - } - } - Self { - key: self.key.clone(), - state_store: self.state_store.clone(), - } - } -} - -impl<K: Eq + std::hash::Hash + Send + Sync + 'static, V: Send + Sync + 'static> Drop - for StateCleaner<K, V> -{ - fn drop(&mut self) { - self.state_store.remove(&self.key); - } -} |