Sync state (param passing)

This commit is contained in:
2026-01-26 16:58:44 +01:00
parent 89b460697f
commit 86e3f4e684
3 changed files with 81 additions and 7 deletions
+55 -5
View File
@@ -1,13 +1,16 @@
use std::{
any::Any,
fmt::{Debug, Display, Formatter, Result as FmtResult},
ops::{Deref, DerefMut},
sync::{Arc, Mutex, MutexGuard},
};
use crate::prelude::Context;
#[derive(Clone)]
pub struct HookEffect(Arc<Mutex<dyn FnMut() + Send + Sync>>);
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct State<T> {
value: Arc<Mutex<T>>,
dependents: Arc<Mutex<Vec<HookEffect>>>,
@@ -19,8 +22,8 @@ pub struct Inner<'a, T> {
updated: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct Mount;
#[derive(Debug, Clone)]
pub struct Mount(Arc<Mutex<bool>>, Arc<Mutex<Vec<HookEffect>>>);
pub fn use_state<T>(v: T) -> State<T> {
State {
@@ -57,6 +60,13 @@ impl<T> State<T> {
d.call();
}
}
pub fn clone(&self) -> Self {
Self {
dependents: self.dependents.clone(),
value: self.value.clone(),
}
}
}
impl<T: Display> Display for State<T> {
@@ -117,7 +127,21 @@ impl<T> HookDependency for State<T> {
impl HookDependency for Mount {
fn on_update(&self, hook: HookEffect) {
hook.call();
if *self.0.lock().unwrap() {
hook.call();
} else {
self.1.lock().unwrap().push(hook);
}
}
}
impl Mount {
pub fn mount(&self) {
*self.0.lock().unwrap() = true;
for hook_effect in self.1.lock().unwrap().iter() {
hook_effect.call();
}
self.1.lock().unwrap().clear();
}
}
@@ -137,5 +161,31 @@ pub fn use_effect<F: FnMut() + Send + Sync + 'static>(f: F, dependencies: &[&dyn
}
pub fn use_mount() -> Mount {
Mount
Mount(Arc::new(Mutex::new(true)), Arc::new(Mutex::new(Vec::new())))
}
pub fn use_mount_manual() -> Mount {
Mount(
Arc::new(Mutex::new(false)),
Arc::new(Mutex::new(Vec::new())),
)
}
pub fn use_sync_state<
T: Send + Sync + 'static,
E: Any + 'static,
D: Fn(&E) -> T + Send + Sync + 'static,
>(
cx: &Arc<Context>,
v: T,
decoder: D,
) -> State<T> {
let state = use_state(v);
cx.on_event({
let state = state.clone();
move |_, v: &E| state.set(decoder(v))
});
state
}