use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use auralis_signal::Signal;
fn drain_into<T: Clone + 'static>(sig: &Signal<T>, rx: mpsc::Receiver<T>) {
for msg in rx {
sig.set(msg);
}
}
fn main() {
println!("=== 1. inline sync bridge ===");
{
let counter = Signal::new(0i32);
let (tx, rx) = mpsc::channel::<i32>();
let handle = thread::spawn(move || {
for i in 1..=5 {
tx.send(i).unwrap();
}
});
for msg in rx {
counter.set(msg);
println!(" signal <- {msg}");
}
handle.join().unwrap();
assert_eq!(counter.read(), 5);
}
println!("\n=== 2. multiple producers ===");
{
let counter = Signal::new(0i32);
let (tx, rx) = mpsc::channel::<i32>();
let tx1 = tx.clone();
let tx2 = tx.clone();
let t1 = thread::spawn(move || {
for i in 1..=3 {
tx1.send(i).unwrap();
thread::sleep(Duration::from_millis(2));
}
});
let t2 = thread::spawn(move || {
for i in 4..=6 {
tx2.send(i).unwrap();
thread::sleep(Duration::from_millis(2));
}
});
drop(tx);
drain_into(&counter, rx);
t1.join().unwrap();
t2.join().unwrap();
println!(" final value: {}", counter.read());
}
println!("\n=== 3. oneshot result ===");
{
let result = Signal::new(String::new());
let (tx, rx) = mpsc::channel::<String>();
thread::spawn(move || {
let computed = format!("computed on {:?}", thread::current().id());
tx.send(computed).unwrap();
})
.join()
.unwrap();
drain_into(&result, rx);
println!(" result: {}", result.read());
}
println!("\nAll patterns completed — no extra crate needed.");
}