miri_native.rs 4.4 KB

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