Merge pull request #32 from osui-rs/use-effect
Added a `run!` macro for useEffect behavior
This commit is contained in:
+6
-9
@@ -10,22 +10,19 @@ fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
pub fn app() -> Rsx {
|
pub fn app() -> Rsx {
|
||||||
let items: State<Vec<i32>> = use_state(vec![1, 2]);
|
let items: State<Vec<i32>> = use_state(vec![1, 2]);
|
||||||
|
let count = use_state(3);
|
||||||
|
|
||||||
std::thread::spawn({
|
run! {
|
||||||
let items = items.clone();
|
use count ref items {
|
||||||
move || {
|
items.get().push(count.get_dl());
|
||||||
std::thread::sleep(std::time::Duration::from_millis(1000));
|
**count.get() += 1;
|
||||||
items.get().push(3);
|
}
|
||||||
std::thread::sleep(std::time::Duration::from_millis(1000));
|
|
||||||
items.get().remove(0);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
FlexRow {
|
FlexRow {
|
||||||
for item in items {
|
for item in items {
|
||||||
static "{item}"
|
static "{item}"
|
||||||
static "Idk {item}"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,6 +189,54 @@ macro_rules! transform {
|
|||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Spawns a thread with referencing dependencies.
|
||||||
|
///
|
||||||
|
/// This macro spawns a thread and clones states and either references them or uses them as a dependency.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```rust
|
||||||
|
/// let count = use_state(0);
|
||||||
|
/// run! {
|
||||||
|
/// use count // implement count as a dependency, running this again if it updates
|
||||||
|
/// ref count // ref means that it will only reference the count (clone)
|
||||||
|
/// {
|
||||||
|
/// // do something with the count
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! run {
|
||||||
|
(use $($d:ident),+ $(ref $($r:ident),+)? $code:block) => {
|
||||||
|
std::thread::spawn({
|
||||||
|
$(let $d = $d.clone();)+
|
||||||
|
$($(let $r = $r.clone();)+)?
|
||||||
|
move || {
|
||||||
|
$($d.add();)+
|
||||||
|
$code
|
||||||
|
loop {
|
||||||
|
$(
|
||||||
|
if $d.check() $code
|
||||||
|
)+
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
(ref $($r:ident),+ $code:block) => {
|
||||||
|
std::thread::spawn({
|
||||||
|
$(let $r = $r.clone();)+
|
||||||
|
move || $code
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
($($code:tt)*) => {
|
||||||
|
std::thread::spawn({
|
||||||
|
move || {$($code)*}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Constructs an `Rsx` tree using declarative syntax.
|
/// Constructs an `Rsx` tree using declarative syntax.
|
||||||
///
|
///
|
||||||
/// This macro is similar to JSX in UI frameworks, allowing you to nest widgets and assign components or dependencies.
|
/// This macro is similar to JSX in UI frameworks, allowing you to nest widgets and assign components or dependencies.
|
||||||
|
|||||||
Reference in New Issue
Block a user