streams.rs 751 B

123456789101112131415161718192021222324252627282930313233
  1. use dioxus::prelude::*;
  2. use dioxus_signals::use_signal;
  3. use futures_util::{future, stream, Stream, StreamExt};
  4. use std::time::Duration;
  5. fn main() {
  6. launch_desktop(app);
  7. }
  8. fn app() -> Element {
  9. let mut count = use_signal(|| 10);
  10. use_future(|| async move {
  11. let mut stream = some_stream();
  12. while let Some(second) = stream.next().await {
  13. count.set(second);
  14. }
  15. });
  16. rsx! {
  17. h1 { "High-Five counter: {count}" }
  18. }
  19. }
  20. fn some_stream() -> std::pin::Pin<Box<dyn Stream<Item = i32>>> {
  21. Box::pin(
  22. stream::once(future::ready(0)).chain(stream::iter(1..).then(|second| async move {
  23. tokio::time::sleep(Duration::from_secs(1)).await;
  24. second
  25. })),
  26. )
  27. }