Added a run macro with useEffect like properties #30
This commit is contained in:
@@ -189,6 +189,51 @@ 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! {
|
||||
/// ref count // ref means that it will only reference the count (clone)
|
||||
/// use count // implement count as a dependency, running this again if it updates
|
||||
/// {
|
||||
/// // do something with the count
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! run {
|
||||
($(ref $r:ident)* $(use $d:ident)* $code:block) => {
|
||||
std::thread::spawn({
|
||||
$(let $r = $r.clone();)* $(
|
||||
let $d = $d.clone();
|
||||
$d.add();
|
||||
)*
|
||||
move || {
|
||||
$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();)* $(let $d = $d.clone();)*
|
||||
move || $code
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
||||
Reference in New Issue
Block a user