miri_native.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. use dioxus::prelude::*;
  2. use dioxus_native_core::{
  3. dioxus::DioxusState,
  4. node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView},
  5. real_dom::RealDom,
  6. Dependancy, Pass, SendAnyMap,
  7. };
  8. use std::{
  9. borrow::BorrowMut,
  10. sync::{Arc, Mutex},
  11. };
  12. use tokio::time::sleep;
  13. #[derive(Debug, Clone, PartialEq, Eq, Default)]
  14. pub struct BlablaState {
  15. count: usize,
  16. }
  17. impl Pass for BlablaState {
  18. type ParentDependencies = (Self,);
  19. type ChildDependencies = ();
  20. type NodeDependencies = ();
  21. const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new()
  22. .with_attrs(AttributeMaskBuilder::Some(&["blabla"]))
  23. .with_element();
  24. fn pass<'a>(
  25. &mut self,
  26. _: NodeView,
  27. _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  28. parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  29. _: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
  30. _: &SendAnyMap,
  31. ) -> bool {
  32. if let Some((parent,)) = parent {
  33. if parent.count != 0 {
  34. self.count += 1;
  35. }
  36. }
  37. true
  38. }
  39. fn create<'a>(
  40. node_view: NodeView<()>,
  41. node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  42. parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  43. children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
  44. context: &SendAnyMap,
  45. ) -> Self {
  46. let mut myself = Self::default();
  47. myself.pass(node_view, node, parent, children, context);
  48. myself
  49. }
  50. }
  51. mod dioxus_elements {
  52. macro_rules! builder_constructors {
  53. (
  54. $(
  55. $(#[$attr:meta])*
  56. $name:ident {
  57. $(
  58. $(#[$attr_method:meta])*
  59. $fil:ident: $vil:ident,
  60. )*
  61. };
  62. )*
  63. ) => {
  64. $(
  65. #[allow(non_camel_case_types)]
  66. $(#[$attr])*
  67. pub struct $name;
  68. impl $name {
  69. pub const TAG_NAME: &'static str = stringify!($name);
  70. pub const NAME_SPACE: Option<&'static str> = None;
  71. $(
  72. pub const $fil: (&'static str, Option<&'static str>, bool) = (stringify!($fil), None, false);
  73. )*
  74. }
  75. impl GlobalAttributes for $name {}
  76. )*
  77. }
  78. }
  79. pub trait GlobalAttributes {}
  80. pub trait SvgAttributes {}
  81. builder_constructors! {
  82. blabla {
  83. };
  84. }
  85. }
  86. #[test]
  87. fn native_core_is_okay() {
  88. use std::time::Duration;
  89. fn app(cx: Scope) -> Element {
  90. let colors = use_state(cx, || vec!["green", "blue", "red"]);
  91. let padding = use_state(cx, || 10);
  92. use_effect(cx, colors, |colors| async move {
  93. sleep(Duration::from_millis(1000)).await;
  94. colors.with_mut(|colors| colors.reverse());
  95. });
  96. use_effect(cx, padding, |padding| async move {
  97. sleep(Duration::from_millis(10)).await;
  98. padding.with_mut(|padding| {
  99. if *padding < 65 {
  100. *padding += 1;
  101. } else {
  102. *padding = 5;
  103. }
  104. });
  105. });
  106. let _big = colors[0];
  107. let _mid = colors[1];
  108. let _small = colors[2];
  109. cx.render(rsx! {
  110. blabla {}
  111. })
  112. }
  113. let rt = tokio::runtime::Builder::new_current_thread()
  114. .enable_time()
  115. .build()
  116. .unwrap();
  117. rt.block_on(async {
  118. let rdom = Arc::new(Mutex::new(RealDom::new(Box::new([
  119. BlablaState::to_type_erased(),
  120. ]))));
  121. let mut dioxus_state = DioxusState::create(&mut *rdom.lock().unwrap());
  122. let mut dom = VirtualDom::new(app);
  123. let mutations = dom.rebuild();
  124. dioxus_state.apply_mutations(&mut *rdom.lock().unwrap(), mutations);
  125. let ctx = SendAnyMap::new();
  126. rdom.lock().unwrap().update_state(ctx, false);
  127. for _ in 0..10 {
  128. dom.wait_for_work().await;
  129. let mutations = dom.render_immediate();
  130. dioxus_state.apply_mutations(&mut *rdom.lock().unwrap(), mutations);
  131. let ctx = SendAnyMap::new();
  132. rdom.lock().unwrap().update_state(ctx, false);
  133. }
  134. });
  135. }