miri_native.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. use dioxus::prelude::*;
  2. use dioxus_native_core::*;
  3. use dioxus_native_core::{
  4. node_ref::{AttributeMask, NodeView},
  5. real_dom::RealDom,
  6. state::{ParentDepState, State},
  7. NodeMask, SendAnyMap,
  8. };
  9. use dioxus_native_core_macro::{sorted_str_slice, State};
  10. use std::{
  11. sync::{Arc, Mutex},
  12. time::Duration,
  13. };
  14. use tokio::time::sleep;
  15. #[derive(Debug, Clone, PartialEq, Default)]
  16. pub struct BlablaState {}
  17. /// Font style are inherited by default if not specified otherwise by some of the supported attributes.
  18. impl ParentDepState for BlablaState {
  19. type Ctx = ();
  20. type DepState = (Self,);
  21. const NODE_MASK: NodeMask =
  22. NodeMask::new_with_attrs(AttributeMask::Static(&sorted_str_slice!(["blabla",])));
  23. fn reduce<'a>(
  24. &mut self,
  25. _node: NodeView,
  26. _parent: Option<(&'a Self,)>,
  27. _ctx: &Self::Ctx,
  28. ) -> bool {
  29. false
  30. }
  31. }
  32. #[derive(Clone, State, Default, Debug)]
  33. pub struct NodeState {
  34. #[parent_dep_state(blabla)]
  35. blabla: BlablaState,
  36. }
  37. mod dioxus_elements {
  38. macro_rules! builder_constructors {
  39. (
  40. $(
  41. $(#[$attr:meta])*
  42. $name:ident {
  43. $(
  44. $(#[$attr_method:meta])*
  45. $fil:ident: $vil:ident,
  46. )*
  47. };
  48. )*
  49. ) => {
  50. $(
  51. #[allow(non_camel_case_types)]
  52. $(#[$attr])*
  53. pub struct $name;
  54. impl $name {
  55. pub const TAG_NAME: &'static str = stringify!($name);
  56. pub const NAME_SPACE: Option<&'static str> = None;
  57. $(
  58. pub const $fil: (&'static str, Option<&'static str>, bool) = (stringify!($fil), None, false);
  59. )*
  60. }
  61. impl GlobalAttributes for $name {}
  62. )*
  63. }
  64. }
  65. pub trait GlobalAttributes {}
  66. pub trait SvgAttributes {}
  67. builder_constructors! {
  68. blabla {
  69. };
  70. }
  71. }
  72. #[test]
  73. fn native_core_is_okay() {
  74. use std::time::Duration;
  75. fn app(cx: Scope) -> Element {
  76. let colors = use_state(&cx, || vec!["green", "blue", "red"]);
  77. let padding = use_state(&cx, || 10);
  78. use_effect(&cx, colors, |colors| async move {
  79. sleep(Duration::from_millis(1000)).await;
  80. colors.with_mut(|colors| colors.reverse());
  81. });
  82. use_effect(&cx, padding, |padding| async move {
  83. sleep(Duration::from_millis(10)).await;
  84. padding.with_mut(|padding| {
  85. if *padding < 65 {
  86. *padding += 1;
  87. } else {
  88. *padding = 5;
  89. }
  90. });
  91. });
  92. let big = colors[0];
  93. let mid = colors[1];
  94. let small = colors[2];
  95. cx.render(rsx! {
  96. blabla {}
  97. })
  98. }
  99. let rt = tokio::runtime::Builder::new_current_thread()
  100. .enable_time()
  101. .build()
  102. .unwrap();
  103. rt.block_on(async {
  104. let rdom = Arc::new(Mutex::new(RealDom::<NodeState>::new()));
  105. let mut dom = VirtualDom::new(app);
  106. let muts = dom.rebuild();
  107. let (to_update, _diff) = rdom.lock().unwrap().apply_mutations(muts);
  108. let ctx = SendAnyMap::new();
  109. let ctx = SendAnyMap::new();
  110. rdom.lock().unwrap().update_state(to_update, ctx);
  111. for x in 0..10 {
  112. dom.wait_for_work().await;
  113. let mutations = dom.render_immediate();
  114. let (to_update, _diff) = rdom.lock().unwrap().apply_mutations(mutations);
  115. let ctx = SendAnyMap::new();
  116. let ctx = SendAnyMap::new();
  117. rdom.lock().unwrap().update_state(to_update, ctx);
  118. }
  119. });
  120. }