From 6ed038e9e5dc87fa3858cc9dbff34568cccf82fc Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 8 Jan 2024 11:25:20 -0800 Subject: [PATCH] Add example of working with streams --- examples/streams.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/streams.rs diff --git a/examples/streams.rs b/examples/streams.rs new file mode 100644 index 000000000..005f73f53 --- /dev/null +++ b/examples/streams.rs @@ -0,0 +1,33 @@ +use dioxus::prelude::*; +use dioxus_signals::use_signal; +use futures_util::{future, stream, Stream, StreamExt}; +use std::time::Duration; + +fn main() { + dioxus_desktop::launch(app); +} + +fn app(cx: Scope) -> Element { + let count = use_signal(cx, || 10); + + use_future(cx, (), |_| async move { + let mut stream = some_stream(); + + while let Some(second) = stream.next().await { + count.set(second); + } + }); + + cx.render(rsx! { + h1 { "High-Five counter: {count}" } + }) +} + +fn some_stream() -> std::pin::Pin>> { + Box::pin( + stream::once(future::ready(0)).chain(stream::iter(1..).then(|second| async move { + tokio::time::sleep(Duration::from_secs(1)).await; + second + })), + ) +}