streams.rs 719 B

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