sharedstate.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. use dioxus::{core_macro::rsx_without_templates, prelude::*};
  3. use dioxus_core::{DomEdit, Mutations, SchedulerMsg, ScopeId};
  4. use std::rc::Rc;
  5. use DomEdit::*;
  6. #[test]
  7. fn shared_state_test() {
  8. struct MySharedState(&'static str);
  9. static App: Component = |cx| {
  10. cx.provide_context(Rc::new(MySharedState("world!")));
  11. cx.render(rsx_without_templates!(Child {}))
  12. };
  13. static Child: Component = |cx| {
  14. let shared = cx.consume_context::<Rc<MySharedState>>()?;
  15. cx.render(rsx_without_templates!("Hello, {shared.0}"))
  16. };
  17. let mut dom = VirtualDom::new(App);
  18. let Mutations { edits, .. } = dom.rebuild();
  19. assert_eq!(
  20. edits,
  21. [
  22. CreateTextNode { root: 1, text: "Hello, world!" },
  23. AppendChildren { many: 1 },
  24. ]
  25. );
  26. }
  27. #[test]
  28. fn swap_test() {
  29. struct MySharedState(&'static str);
  30. fn app(cx: Scope) -> Element {
  31. let val = cx.use_hook(|| 0);
  32. *val += 1;
  33. cx.provide_context(Rc::new(MySharedState("world!")));
  34. let child = match *val % 2 {
  35. 0 => rsx_without_templates!(
  36. Child1 {
  37. Child1 { }
  38. Child2 { }
  39. }
  40. ),
  41. _ => rsx_without_templates!(
  42. Child2 {
  43. Child2 { }
  44. Child2 { }
  45. }
  46. ),
  47. };
  48. cx.render(rsx_without_templates!(
  49. Router {
  50. div { child }
  51. }
  52. ))
  53. }
  54. #[inline_props]
  55. fn Router<'a>(cx: Scope, children: Element<'a>) -> Element<'a> {
  56. cx.render(rsx_without_templates!(div { children }))
  57. }
  58. #[inline_props]
  59. fn Child1<'a>(cx: Scope, children: Element<'a>) -> Element {
  60. let shared = cx.consume_context::<Rc<MySharedState>>().unwrap();
  61. println!("Child1: {}", shared.0);
  62. cx.render(rsx_without_templates! {
  63. div {
  64. "{shared.0}",
  65. children
  66. }
  67. })
  68. }
  69. #[inline_props]
  70. fn Child2<'a>(cx: Scope, children: Element<'a>) -> Element {
  71. let shared = cx.consume_context::<Rc<MySharedState>>().unwrap();
  72. println!("Child2: {}", shared.0);
  73. cx.render(rsx_without_templates! {
  74. h1 {
  75. "{shared.0}",
  76. children
  77. }
  78. })
  79. }
  80. let mut dom = VirtualDom::new(app);
  81. let Mutations { edits, .. } = dom.rebuild();
  82. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  83. dom.work_with_deadline(|| false);
  84. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  85. dom.work_with_deadline(|| false);
  86. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  87. dom.work_with_deadline(|| false);
  88. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  89. dom.work_with_deadline(|| false);
  90. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  91. dom.work_with_deadline(|| false);
  92. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  93. dom.work_with_deadline(|| false);
  94. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  95. // dom.work_with_deadline(|| false);
  96. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
  97. // dom.work_with_deadline(|| false);
  98. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
  99. // dom.work_with_deadline(|| false);
  100. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  101. // dom.work_with_deadline(|| false);
  102. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  103. // dom.work_with_deadline(|| false);
  104. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  105. // dom.work_with_deadline(|| false);
  106. }