dioxus/packages/native-core/tests/miri_native.rs

157 lines
4.3 KiB
Rust
Raw Normal View History

2023-02-02 20:26:59 +00:00
use dioxus::prelude::*;
2023-03-13 23:09:20 +00:00
use dioxus_native_core::prelude::*;
2023-03-12 20:35:48 +00:00
use dioxus_native_core_macro::partial_derive_state;
use shipyard::Component;
2023-02-02 20:26:59 +00:00
use tokio::time::sleep;
2023-03-12 20:35:48 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Default, Component)]
2023-02-02 20:26:59 +00:00
pub struct BlablaState {
count: usize,
}
2023-03-12 20:35:48 +00:00
#[partial_derive_state]
2023-02-11 23:43:08 +00:00
impl State for BlablaState {
2023-02-02 20:26:59 +00:00
type ParentDependencies = (Self,);
type ChildDependencies = ();
type NodeDependencies = ();
2023-02-05 02:45:29 +00:00
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new()
2023-02-02 20:26:59 +00:00
.with_attrs(AttributeMaskBuilder::Some(&["blabla"]))
.with_element();
2023-02-11 23:43:08 +00:00
fn update<'a>(
2023-02-02 20:26:59 +00:00
&mut self,
2023-02-02 20:48:02 +00:00
_: NodeView,
2023-02-02 20:26:59 +00:00
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
2023-03-12 20:35:48 +00:00
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
2023-02-02 20:26:59 +00:00
_: &SendAnyMap,
) -> bool {
if let Some((parent,)) = parent {
if parent.count != 0 {
self.count += 1;
}
}
true
}
fn create<'a>(
node_view: NodeView<()>,
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
2023-03-12 20:35:48 +00:00
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
2023-02-02 20:26:59 +00:00
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
2023-02-11 23:43:08 +00:00
myself.update(node_view, node, parent, children, context);
2023-02-02 20:26:59 +00:00
myself
}
}
mod dioxus_elements {
macro_rules! builder_constructors {
(
$(
$(#[$attr:meta])*
$name:ident {
$(
$(#[$attr_method:meta])*
$fil:ident: $vil:ident,
)*
};
)*
) => {
$(
#[allow(non_camel_case_types)]
$(#[$attr])*
pub struct $name;
impl $name {
pub const TAG_NAME: &'static str = stringify!($name);
pub const NAME_SPACE: Option<&'static str> = None;
$(
pub const $fil: (&'static str, Option<&'static str>, bool) = (stringify!($fil), None, false);
)*
}
impl GlobalAttributes for $name {}
)*
}
}
pub trait GlobalAttributes {}
pub trait SvgAttributes {}
builder_constructors! {
blabla {
};
}
}
#[test]
fn native_core_is_okay() {
2023-02-05 15:39:55 +00:00
use std::sync::{Arc, Mutex};
2023-02-02 20:26:59 +00:00
use std::time::Duration;
fn app(cx: Scope) -> Element {
let colors = use_state(cx, || vec!["green", "blue", "red"]);
let padding = use_state(cx, || 10);
use_effect(cx, colors, |colors| async move {
sleep(Duration::from_millis(1000)).await;
colors.with_mut(|colors| colors.reverse());
});
use_effect(cx, padding, |padding| async move {
sleep(Duration::from_millis(10)).await;
padding.with_mut(|padding| {
if *padding < 65 {
*padding += 1;
} else {
*padding = 5;
}
});
});
let _big = colors[0];
let _mid = colors[1];
let _small = colors[2];
cx.render(rsx! {
blabla {}
})
}
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
rt.block_on(async {
let rdom = Arc::new(Mutex::new(RealDom::new([
2023-02-02 20:26:59 +00:00
BlablaState::to_type_erased(),
])));
2023-02-05 02:45:29 +00:00
let mut dioxus_state = DioxusState::create(&mut *rdom.lock().unwrap());
2023-02-02 20:26:59 +00:00
let mut dom = VirtualDom::new(app);
2023-02-05 02:45:29 +00:00
let mutations = dom.rebuild();
dioxus_state.apply_mutations(&mut *rdom.lock().unwrap(), mutations);
2023-02-02 20:26:59 +00:00
let ctx = SendAnyMap::new();
2023-03-12 20:35:48 +00:00
rdom.lock().unwrap().update_state(ctx);
2023-02-02 20:26:59 +00:00
for _ in 0..10 {
dom.wait_for_work().await;
let mutations = dom.render_immediate();
2023-02-05 02:45:29 +00:00
dioxus_state.apply_mutations(&mut *rdom.lock().unwrap(), mutations);
2023-02-02 20:26:59 +00:00
let ctx = SendAnyMap::new();
2023-03-12 20:35:48 +00:00
rdom.lock().unwrap().update_state(ctx);
2023-02-02 20:26:59 +00:00
}
});
}