async.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //! Example: README.md showcase
  2. //!
  3. //! The example from the README.md
  4. use std::{pin::Pin, time::Duration};
  5. use dioxus::prelude::*;
  6. use futures::Future;
  7. fn main() {
  8. env_logger::init();
  9. log::info!("hello world");
  10. dioxus::desktop::launch(App, |c| c).expect("faield to launch");
  11. }
  12. #[derive(serde::Deserialize)]
  13. struct DogApi {
  14. message: String,
  15. }
  16. const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
  17. static App: FC<()> = |cx| {
  18. let mut count = use_state(cx, || 0);
  19. let mut fut = cx.use_hook(
  20. move || {
  21. Box::pin(async {
  22. //
  23. let mut tick = 0;
  24. loop {
  25. async_std::task::sleep(Duration::from_millis(250)).await;
  26. log::debug!("ticking forward... {}", tick);
  27. tick += 1;
  28. // match surf::get(ENDPOINT).recv_json::<DogApi>().await {
  29. // Ok(_) => (),
  30. // Err(_) => (),
  31. // }
  32. }
  33. }) as Pin<Box<dyn Future<Output = ()> + 'static>>
  34. },
  35. |h| h,
  36. |_| {},
  37. );
  38. cx.submit_task(fut);
  39. cx.render(rsx! {
  40. div {
  41. h1 {"it's working somewhat properly"}
  42. }
  43. })
  44. };