dioxus/examples/streams.rs

34 lines
743 B
Rust
Raw Normal View History

2024-01-08 19:25:20 +00:00
use dioxus::prelude::*;
use dioxus_signals::use_signal;
use futures_util::{future, stream, Stream, StreamExt};
use std::time::Duration;
fn main() {
launch(app);
2024-01-08 19:25:20 +00:00
}
fn app() -> Element {
let mut count = use_signal(|| 10);
2024-01-08 19:25:20 +00:00
use_future(|| async move {
2024-01-08 19:25:20 +00:00
let mut stream = some_stream();
while let Some(second) = stream.next().await {
count.set(second);
}
});
2024-01-14 05:12:21 +00:00
rsx! {
2024-01-08 19:25:20 +00:00
h1 { "High-Five counter: {count}" }
2024-01-14 05:12:21 +00:00
}
2024-01-08 19:25:20 +00:00
}
fn some_stream() -> std::pin::Pin<Box<dyn Stream<Item = i32>>> {
Box::pin(
stream::once(future::ready(0)).chain(stream::iter(1..).then(|second| async move {
tokio::time::sleep(Duration::from_secs(1)).await;
second
})),
)
}