split_subscriptions.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_signals::Signal;
  4. fn main() {
  5. dioxus_desktop::launch(app);
  6. }
  7. #[derive(Clone, Copy, Default)]
  8. struct ApplicationData {
  9. first_data: Signal<i32>,
  10. second_data: Signal<i32>,
  11. many_signals: Signal<Vec<Signal<i32>>>,
  12. }
  13. fn use_app_data() -> ApplicationData {
  14. *use_context(cx).unwrap()
  15. }
  16. fn app() -> Element {
  17. use_context_provider(ApplicationData::default);
  18. rsx! {
  19. div { ReadsFirst {} }
  20. div { ReadsSecond {} }
  21. div { ReadsManySignals {} }
  22. }
  23. }
  24. fn ReadsFirst() -> Element {
  25. println!("running first");
  26. let data = use_app_data(cx);
  27. rsx! {
  28. button {
  29. onclick: move |_| {
  30. *data.first_data.write() += 1;
  31. },
  32. "Increase"
  33. }
  34. button {
  35. onclick: move |_| {
  36. *data.first_data.write() -= 1;
  37. },
  38. "Decrease"
  39. }
  40. button {
  41. onclick: move |_| {
  42. *data.first_data.write() = 0;
  43. },
  44. "Reset"
  45. }
  46. "{data.first_data}"
  47. }
  48. }
  49. fn ReadsSecond() -> Element {
  50. println!("running second");
  51. let data = use_app_data(cx);
  52. rsx! {
  53. button {
  54. onclick: move |_| {
  55. *data.second_data.write() += 1;
  56. },
  57. "Increase"
  58. }
  59. button {
  60. onclick: move |_| {
  61. *data.second_data.write() -= 1;
  62. },
  63. "Decrease"
  64. }
  65. button {
  66. onclick: move |_| {
  67. *data.second_data.write() = 0;
  68. },
  69. "Reset"
  70. }
  71. "{data.second_data}"
  72. }
  73. }
  74. fn ReadsManySignals() -> Element {
  75. println!("running many signals");
  76. let data = use_app_data(cx);
  77. rsx! {
  78. button {
  79. onclick: move |_| {
  80. data.many_signals.write().push(Signal::new(0));
  81. },
  82. "Create"
  83. }
  84. button {
  85. onclick: move |_| {
  86. data.many_signals.write().pop();
  87. },
  88. "Destroy"
  89. }
  90. button {
  91. onclick: move |_| {
  92. if let Some(first) = data.many_signals.read().first() {
  93. *first.write() += 1;
  94. }
  95. },
  96. "Increase First Item"
  97. }
  98. for signal in data.many_signals {
  99. Child { count: *signal }
  100. }
  101. }
  102. }
  103. #[derive(Props, PartialEq)]
  104. struct ChildProps {
  105. count: Signal<i32>,
  106. }
  107. fn Child(cx: Scope<ChildProps>) -> Element {
  108. println!("running child");
  109. let count = cx.props.count;
  110. rsx! {
  111. div {
  112. "Child: {count}"
  113. button {
  114. onclick: move |_| {
  115. *count.write() += 1;
  116. },
  117. "Increase"
  118. }
  119. button {
  120. onclick: move |_| {
  121. *count.write() -= 1;
  122. },
  123. "Decrease"
  124. }
  125. }
  126. }
  127. }