effect.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. use std::cell::RefCell;
  3. use std::collections::HashMap;
  4. use std::rc::Rc;
  5. use std::time::Duration;
  6. use dioxus::prelude::*;
  7. use dioxus_core::ElementId;
  8. use dioxus_signals::*;
  9. #[tokio::test]
  10. async fn effects_rerun() {
  11. #[derive(Default)]
  12. struct RunCounter {
  13. component: usize,
  14. effect: usize,
  15. }
  16. let counter = Rc::new(RefCell::new(RunCounter::default()));
  17. let mut dom = VirtualDom::new_with_props(
  18. |counter: Rc<RefCell<RunCounter>>| {
  19. counter.borrow_mut().component += 1;
  20. let mut signal = use_signal(|| 0);
  21. use_effect({
  22. to_owned![counter];
  23. move || {
  24. counter.borrow_mut().effect += 1;
  25. // This will subscribe the effect to the signal
  26. println!("Signal: {:?}", signal);
  27. // Stop the wait for work manually
  28. needs_update();
  29. }
  30. });
  31. signal += 1;
  32. rsx! {
  33. div {}
  34. }
  35. },
  36. counter.clone(),
  37. );
  38. dom.rebuild_in_place();
  39. tokio::select! {
  40. _ = dom.wait_for_work() => {}
  41. _ = tokio::time::sleep(Duration::from_millis(500)) => panic!("timed out")
  42. };
  43. let current_counter = counter.borrow();
  44. assert_eq!(current_counter.component, 1);
  45. assert_eq!(current_counter.effect, 1);
  46. }
  47. // https://github.com/DioxusLabs/dioxus/issues/2347
  48. // Effects should rerun when the signal changes if there are no changes to the component
  49. #[tokio::test]
  50. async fn effects_rerun_without_rerender() {
  51. #[derive(Default)]
  52. struct RunCounter {
  53. component: usize,
  54. effect: usize,
  55. }
  56. let counter = Rc::new(RefCell::new(RunCounter::default()));
  57. let mut dom = VirtualDom::new_with_props(
  58. |counter: Rc<RefCell<RunCounter>>| {
  59. counter.borrow_mut().component += 1;
  60. println!("component {}", counter.borrow().component);
  61. let mut signal = use_signal(|| 0);
  62. use_effect({
  63. to_owned![counter];
  64. move || {
  65. counter.borrow_mut().effect += 1;
  66. // This will subscribe the effect to the signal
  67. println!("Signal: {}", signal);
  68. }
  69. });
  70. use_future(move || async move {
  71. for i in 0..10 {
  72. tokio::time::sleep(std::time::Duration::from_millis(10)).await;
  73. println!("future {}", i);
  74. signal += 1;
  75. }
  76. });
  77. rsx! {
  78. div {}
  79. }
  80. },
  81. counter.clone(),
  82. );
  83. dom.rebuild_in_place();
  84. tokio::select! {
  85. _ = dom.wait_for_work() => {}
  86. _ = tokio::time::sleep(Duration::from_millis(500)) => {}
  87. };
  88. let current_counter = counter.borrow();
  89. assert_eq!(current_counter.component, 1);
  90. assert_eq!(current_counter.effect, 11);
  91. }