Sync state (param passing)
This commit is contained in:
+18
-2
@@ -2,6 +2,8 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use osui::{prelude::*, rsx};
|
use osui::{prelude::*, rsx};
|
||||||
|
|
||||||
|
pub struct Count(pub State<u16>);
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let console = Console::new();
|
let console = Console::new();
|
||||||
console.run(app);
|
console.run(app);
|
||||||
@@ -9,7 +11,7 @@ fn main() {
|
|||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
fn app(cx: &Arc<Context>) -> View {
|
||||||
let count = use_state(0);
|
let count = use_state(0);
|
||||||
let mount = use_mount();
|
let mount = use_mount_manual();
|
||||||
|
|
||||||
use_effect(
|
use_effect(
|
||||||
{
|
{
|
||||||
@@ -27,6 +29,18 @@ fn app(cx: &Arc<Context>) -> View {
|
|||||||
&[&mount],
|
&[&mount],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Pass the count to the children
|
||||||
|
use_effect(
|
||||||
|
{
|
||||||
|
let count = count.clone();
|
||||||
|
let cx = cx.clone();
|
||||||
|
move || {
|
||||||
|
cx.emit_event(&Count(count.clone()));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&[&mount],
|
||||||
|
);
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
// Static scope
|
// Static scope
|
||||||
my_component (ctx, view) {
|
my_component (ctx, view) {
|
||||||
@@ -46,13 +60,15 @@ fn app(cx: &Arc<Context>) -> View {
|
|||||||
for %count (i in 0..count.get_dl()) {
|
for %count (i in 0..count.get_dl()) {
|
||||||
"{i}" @Point { x: 0, y: i };
|
"{i}" @Point { x: 0, y: i };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
!mount mount
|
||||||
}
|
}
|
||||||
.view(cx.clone())
|
.view(cx.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn my_component(cx: &Arc<Context>) -> View {
|
fn my_component(cx: &Arc<Context>) -> View {
|
||||||
let count = use_state(0);
|
|
||||||
let mount = use_mount();
|
let mount = use_mount();
|
||||||
|
let count = use_sync_state(cx, 0, |Count(v)| v.get_dl());
|
||||||
|
|
||||||
use_effect(
|
use_effect(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -106,6 +106,14 @@ macro_rules! rsx_scope {
|
|||||||
$crate::rsx_scope!($rsx, $($rest)*);
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
($rsx:expr, !mount $mount:ident $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
$mount.mount();
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
($rsx:expr,) => {};
|
($rsx:expr,) => {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+54
-4
@@ -1,13 +1,16 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
any::Any,
|
||||||
fmt::{Debug, Display, Formatter, Result as FmtResult},
|
fmt::{Debug, Display, Formatter, Result as FmtResult},
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::prelude::Context;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct HookEffect(Arc<Mutex<dyn FnMut() + Send + Sync>>);
|
pub struct HookEffect(Arc<Mutex<dyn FnMut() + Send + Sync>>);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)]
|
||||||
pub struct State<T> {
|
pub struct State<T> {
|
||||||
value: Arc<Mutex<T>>,
|
value: Arc<Mutex<T>>,
|
||||||
dependents: Arc<Mutex<Vec<HookEffect>>>,
|
dependents: Arc<Mutex<Vec<HookEffect>>>,
|
||||||
@@ -19,8 +22,8 @@ pub struct Inner<'a, T> {
|
|||||||
updated: bool,
|
updated: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Mount;
|
pub struct Mount(Arc<Mutex<bool>>, Arc<Mutex<Vec<HookEffect>>>);
|
||||||
|
|
||||||
pub fn use_state<T>(v: T) -> State<T> {
|
pub fn use_state<T>(v: T) -> State<T> {
|
||||||
State {
|
State {
|
||||||
@@ -57,6 +60,13 @@ impl<T> State<T> {
|
|||||||
d.call();
|
d.call();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
dependents: self.dependents.clone(),
|
||||||
|
value: self.value.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Display> Display for State<T> {
|
impl<T: Display> Display for State<T> {
|
||||||
@@ -117,7 +127,21 @@ impl<T> HookDependency for State<T> {
|
|||||||
|
|
||||||
impl HookDependency for Mount {
|
impl HookDependency for Mount {
|
||||||
fn on_update(&self, hook: HookEffect) {
|
fn on_update(&self, hook: HookEffect) {
|
||||||
|
if *self.0.lock().unwrap() {
|
||||||
hook.call();
|
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 {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user