From 6100023a2ad7ceface0065485b48f49b70991a30 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Thu, 14 Aug 2025 05:01:03 +0200 Subject: [PATCH 1/3] Added a run macro with useEffect like properties #30 --- examples/iter.rs | 13 +++++-------- src/macros.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/examples/iter.rs b/examples/iter.rs index 8da4782..230f030 100644 --- a/examples/iter.rs +++ b/examples/iter.rs @@ -11,21 +11,18 @@ fn main() -> std::io::Result<()> { pub fn app() -> Rsx { let items: State> = use_state(vec![1, 2]); - std::thread::spawn({ - let items = items.clone(); - move || { - std::thread::sleep(std::time::Duration::from_millis(1000)); + run! { + use items + { + std::thread::sleep(std::time::Duration::from_millis(300)); items.get().push(3); - std::thread::sleep(std::time::Duration::from_millis(1000)); - items.get().remove(0); } - }); + } rsx! { FlexRow { for item in items { static "{item}" - static "Idk {item}" } } } diff --git a/src/macros.rs b/src/macros.rs index 384a3ab..d47c0ad 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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. From f51182b2b15d3425dda55d8799df4dc4a0866080 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Thu, 14 Aug 2025 15:39:30 +0200 Subject: [PATCH 2/3] Updated the run macro #30 #31 --- examples/iter.rs | 8 ++++---- src/macros.rs | 25 ++++++++++++++----------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/examples/iter.rs b/examples/iter.rs index 230f030..c61589b 100644 --- a/examples/iter.rs +++ b/examples/iter.rs @@ -10,12 +10,12 @@ fn main() -> std::io::Result<()> { pub fn app() -> Rsx { let items: State> = use_state(vec![1, 2]); + let count = use_state(3); run! { - use items - { - std::thread::sleep(std::time::Duration::from_millis(300)); - items.get().push(3); + use count ref items { + items.get().push(count.get_dl()); + **count.get() += 1; } } diff --git a/src/macros.rs b/src/macros.rs index d47c0ad..1978443 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -206,32 +206,35 @@ macro_rules! transform { /// ``` #[macro_export] macro_rules! run { - ($(ref $r:ident)* $(use $d:ident)* $code:block) => { + (use $($d:ident),+ $(ref $($r:ident),+)? $code:block) => { std::thread::spawn({ - $(let $r = $r.clone();)* $( - let $d = $d.clone(); - $d.add(); - )* + $(let $d = $d.clone();)+ + $($(let $r = $r.clone();)+)? move || { + $($d.add();)+ $code loop { $( - if $d.check() { - $code - } - )* + if $d.check() $code + )+ std::thread::sleep(std::time::Duration::from_millis(50)); } } }); }; - ($(ref $r:ident)* $code:block) => { + (ref $($r:ident),+ $code:block) => { std::thread::spawn({ - $(let $r = $r.clone();)* $(let $d = $d.clone();)* + $(let $r = $r.clone();)+ move || $code }); }; + + ($($code:tt)*) => { + std::thread::spawn({ + move || {$($code)*} + }); + }; } /// Constructs an `Rsx` tree using declarative syntax. From c460e073a71b9b8ec2305c84b585f28b85bcae25 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Thu, 14 Aug 2025 15:42:21 +0200 Subject: [PATCH 3/3] Updated the run macro #31 #30 --- src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 1978443..b07ec9b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -197,8 +197,8 @@ macro_rules! transform { /// ```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 +/// ref count // ref means that it will only reference the count (clone) /// { /// // do something with the count /// }