From 33bb7130943b5f74b3b0f08c5e6d8f7c5e54d4c0 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Sun, 1 Jun 2025 20:17:57 +0100 Subject: feat: switch to leptos-fetch for state management --- src/state_store.rs | 237 ----------------------------------------------------- 1 file changed, 237 deletions(-) (limited to 'src/state_store.rs') 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 { - store: Arc>>, -} - -impl PartialEq for ArcStateStore { - fn eq(&self, other: &Self) -> bool { - Arc::ptr_eq(&self.store, &other.store) - } -} - -impl Clone for ArcStateStore { - fn clone(&self) -> Self { - Self { - store: Arc::clone(&self.store), - } - } -} - -impl Eq for ArcStateStore {} - -impl ArcStateStore { - pub fn new() -> Self { - Self { - store: Arc::new(RwLock::new(HashMap::new())), - } - } -} - -#[derive(Debug)] -pub struct StateStore { - inner: ArenaItem, S>, -} - -impl Dispose for StateStore { - fn dispose(self) { - self.inner.dispose() - } -} - -impl StateStore -where - K: Send + Sync + 'static, - V: Send + Sync + 'static, -{ - pub fn new() -> Self { - Self::new_with_storage() - } -} - -impl StateStore -where - K: 'static, - V: 'static, - S: Storage>, -{ - pub fn new_with_storage() -> Self { - Self { - inner: ArenaItem::new_with_storage(ArcStateStore::new()), - } - } -} - -impl StateStore -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> for StateStore -{ - fn from(value: ArcStateStore) -> Self { - Self { - inner: ArenaItem::new_with_storage(value), - } - } -} - -impl FromLocal> for StateStore { - fn from_local(value: ArcStateStore) -> Self { - Self { - inner: ArenaItem::new_with_storage(value), - } - } -} - -impl Copy for StateStore {} - -impl Clone for StateStore { - fn clone(&self) -> Self { - *self - } -} - -impl StateStore -where - K: Send + Sync + 'static, - V: Send + Sync + 'static, -{ - pub fn store(&self, key: K, value: V) -> StateListener { - { - 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 StateStore -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 -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, -} - -impl< - K: std::cmp::Eq + std::hash::Hash + std::marker::Send + std::marker::Sync, - V: std::marker::Send + std::marker::Sync, -> Deref for StateListener -{ - type Target = V; - - fn deref(&self) -> &Self::Target { - &self.value - } -} - -impl DerefMut - for StateListener -{ - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.value - } -} - -struct ArcStateCleaner { - key: K, - state_store: ArcStateStore, -} - -struct StateCleaner -where - K: Eq + std::hash::Hash + Send + Sync + 'static, - V: Send + Sync + 'static, -{ - key: K, - state_store: StateStore, -} - -impl Clone for StateCleaner -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 Drop - for StateCleaner -{ - fn drop(&mut self) { - self.state_store.remove(&self.key); - } -} -- cgit