miri_native.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(&mut self, _node: NodeView, _parent: Option<(&Self,)>, _ctx: &Self::Ctx) -> bool {
  20. false
  21. }
  22. }
  23. #[derive(Clone, State, Default, Debug)]
  24. pub struct NodeState {
  25. #[parent_dep_state(blabla)]
  26. blabla: BlablaState,
  27. }
  28. mod dioxus_elements {
  29. macro_rules! builder_constructors {
  30. (
  31. $(
  32. $(#[$attr:meta])*
  33. $name:ident {
  34. $(
  35. $(#[$attr_method:meta])*
  36. $fil:ident: $vil:ident,
  37. )*
  38. };
  39. )*
  40. ) => {
  41. $(
  42. #[allow(non_camel_case_types)]
  43. $(#[$attr])*
  44. pub struct $name;
  45. impl $name {
  46. pub const TAG_NAME: &'static str = stringify!($name);
  47. pub const NAME_SPACE: Option<&'static str> = None;
  48. $(
  49. pub const $fil: (&'static str, Option<&'static str>, bool) = (stringify!($fil), None, false);
  50. )*
  51. }
  52. impl GlobalAttributes for $name {}
  53. )*
  54. }
  55. }
  56. pub trait GlobalAttributes {}
  57. pub trait SvgAttributes {}
  58. builder_constructors! {
  59. blabla {
  60. };
  61. }
  62. }
  63. #[test]
  64. fn native_core_is_okay() {
  65. use std::time::Duration;
  66. fn app(cx: Scope) -> Element {
  67. let colors = use_state(cx, || vec!["green", "blue", "red"]);
  68. let padding = use_state(cx, || 10);
  69. use_effect(cx, colors, |colors| async move {
  70. sleep(Duration::from_millis(1000)).await;
  71. colors.with_mut(|colors| colors.reverse());
  72. });
  73. use_effect(cx, padding, |padding| async move {
  74. sleep(Duration::from_millis(10)).await;
  75. padding.with_mut(|padding| {
  76. if *padding < 65 {
  77. *padding += 1;
  78. } else {
  79. *padding = 5;
  80. }
  81. });
  82. });
  83. let _big = colors[0];
  84. let _mid = colors[1];
  85. let _small = colors[2];
  86. cx.render(rsx! {
  87. blabla {}
  88. })
  89. }
  90. let rt = tokio::runtime::Builder::new_current_thread()
  91. .enable_time()
  92. .build()
  93. .unwrap();
  94. rt.block_on(async {
  95. let rdom = Arc::new(Mutex::new(RealDom::<NodeState>::new()));
  96. let mut dom = VirtualDom::new(app);
  97. let muts = dom.rebuild();
  98. let (to_update, _diff) = rdom.lock().unwrap().apply_mutations(muts);
  99. let ctx = SendAnyMap::new();
  100. rdom.lock().unwrap().update_state(to_update, ctx);
  101. for _ in 0..10 {
  102. dom.wait_for_work().await;
  103. let mutations = dom.render_immediate();
  104. let (to_update, _diff) = rdom.lock().unwrap().apply_mutations(mutations);
  105. let ctx = SendAnyMap::new();
  106. rdom.lock().unwrap().update_state(to_update, ctx);
  107. }
  108. });
  109. }