miri_native.rs 4.3 KB

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