coroutine.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, |c| c);
  25. }
  26. use dioxus::prelude::*;
  27. static App: FC<()> = |(cx, props)| {
  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_task(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_task(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 { style: { width: "400px", height: "400px", position: "relative", background: "yellow" }
  45. button { "reset", onclick: move |_| {} }
  46. Horsey { pos: *p1, "horsey 1" }
  47. Horsey { pos: *p2, "horsey 2" }
  48. }
  49. })
  50. };
  51. #[derive(PartialEq, Props)]
  52. struct HorseyProps {
  53. pos: i32,
  54. }
  55. static Horsey: FC<HorseyProps> = |(cx, props)| {
  56. cx.render(rsx! {
  57. div {
  58. button { "pause" }
  59. div {
  60. {cx.children()}
  61. }
  62. }
  63. })
  64. };