Skip to content

Commit

Permalink
feat: add signals
Browse files Browse the repository at this point in the history
  • Loading branch information
katopz committed Sep 23, 2024
1 parent bfb45d6 commit f5353a0
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/r1/hello-rust-signals/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
286 changes: 286 additions & 0 deletions examples/r1/hello-rust-signals/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/r1/hello-rust-signals/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello-rust-signals"
version = "0.1.0"
edition = "2021"

[dependencies]
futures = "0.3.30"
futures-signals = "0.3.34"
58 changes: 58 additions & 0 deletions examples/r1/hello-rust-signals/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use futures::executor::block_on;
use futures_signals::signal::{Mutable, SignalExt};
use std::thread::{sleep, spawn};
use std::time::Duration;

async fn do_some_async_calculation(value: u32) -> u32 {
// Simulate an async calculation by adding a delay
println!("Starting async calculation (x2) for value: {}", value);
sleep(Duration::from_secs(1));
let result = value * 2;
println!(
"Async calculation (x2) completed for value: {}, result: {}",
value, result
);
result
}

fn main() {
// We create a Mutable with an initial value of 5.
let my_state = Mutable::new(5);
println!("Start with: {:?}", my_state);

// We create a signal chain that:
// 1. Adds 5 to the current value of my_state.
// 2. Performs an async calculation that doubles the value.
// 3. Deduplicates the result to avoid unnecessary updates.
let output = my_state
.signal()
.map(|value| {
let new_value = value + 5;
println!("After map: {}", new_value);
new_value
})
.map_future(|value| {
println!("Before async calculation: {}", value);
do_some_async_calculation(value)
})
.dedupe();

// Watch for signal dispatch and print the new value.
// The closure will be called with the current value of the signal chain.
let future = output.for_each(|value| {
println!("New value: {:?}", value);
async {}
});

// Spawn the future to start watching for changes in a separate thread.
spawn(move || {
block_on(future);
});

// Change the state of my_state to 10 to trigger the signal.
println!("Changing my_state to 10");
my_state.set(10);

// Keep the main thread alive to allow async tasks to run.
sleep(Duration::from_secs(3));
}
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- [Marker Types](rust/r1/marker-types.md)
- [Mutex](rust/r1/mutex.md)
- [Smol](rust/r1/smol.md)
- [Signals](rust/r1/signals.md)
- [Wasm](wasm/mod.md)
- [Setup](wasm/setup.md)
- [Enjoy](wasm/enjoy.md)
Expand Down
Loading

0 comments on commit f5353a0

Please sign in to comment.