coroutine.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //! Example: Coroutines!
  2. //! --------------------
  3. //!
  4. //! Coroutines are an awesome way to write concurrent code. Dioxus heavily leverages coroutines to make sense of complex
  5. //! ongoing asynchronous tasks. The async scheduler of Dioxus supports both single-threaded and multi-threaded coroutines,
  6. //! so you can drop in code to run across multiple threads without blocking the main thread.
  7. //!
  8. //! Dioxus cannot simply abstract away the threading model for the web, unfortunately. If you want to use "web threads"
  9. //! you either need to limit support for Chrome, or you need to use a Web Workers and message passing. This is easy enough
  10. //! to do in your own code, and doesn't require 1st-party support from Dioxus itself.
  11. //!
  12. //! UseState and friends work fine with coroutines, but coroutines might be easier to use with the Dirac global state
  13. //! management API. This lets you easily drive global state from a coroutine without having to subscribe to the state.
  14. //!
  15. //! For now, this example shows how to use coroutines used with use_state.
  16. //!
  17. //!
  18. //! ## What is a Coroutine?
  19. //!
  20. //! A coroutine is a function that can be paused and resumed. It can be paused internally through "await" or externally
  21. //! using the `TaskHandle` API. Within a coroutine, you may execute asynchronous code, that modifies values captured when
  22. //! the coroutine was initiated. `use_state` always returns the same setter, so you don't need to worry about
  23. fn main() {
  24. dioxus::desktop::launch(app);
  25. }
  26. use dioxus::prelude::*;
  27. fn app(cx: Scope) -> Element {
  28. let p1 = use_state(&cx, || 0);
  29. let p2 = use_state(&cx, || 0);
  30. let (mut p1_async, mut p2_async) = (p1.for_async(), p2.for_async());
  31. let (p1_handle, _) = use_coroutine(cx, || async move {
  32. loop {
  33. *p1_async.get_mut() += 1;
  34. async_std::task::sleep(std::time::Duration::from_millis(75)).await;
  35. }
  36. });
  37. let (p2_handle, _) = use_coroutine(cx, || async move {
  38. loop {
  39. *p2_async.get_mut() += 1;
  40. async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  41. }
  42. });
  43. cx.render(rsx! {
  44. div {
  45. width: "400px", height: "400px", position: "relative", background: "yellow"
  46. button { "reset", onclick: move |_| {} }
  47. Horsey { pos: *p1, "horsey 1" }
  48. Horsey { pos: *p2, "horsey 2" }
  49. }
  50. })
  51. }
  52. #[derive(Props)]
  53. struct HorseyProps<'a> {
  54. pos: i32,
  55. children: Element<'a>,
  56. }
  57. fn Horsey<'a>(cx: Scope<'a, HorseyProps<'a>>) -> Element {
  58. cx.render(rsx! {
  59. div {
  60. button { "pause" }
  61. div {
  62. {&cx.props.children}
  63. }
  64. }
  65. })
  66. }