miri_native.rs 4.2 KB

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