miri_native.rs 3.6 KB

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