merge upstream
This commit is contained in:
commit
f4af297777
127 files changed
+5668
-1277
No files matched your search
@@ -33,6 +33,7 @@ serde = { version = "1", features = ["derive"], optional = true }
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["full"] }
|
||||
dioxus = { workspace = true }
|
||||
dioxus-html = { workspace = true, features = ["serialize"] }
|
||||
pretty_assertions = "1.3.0"
|
||||
rand = "0.8.5"
|
||||
dioxus-ssr = { workspace = true }
|
||||
|
||||
@@ -28,6 +28,27 @@ pub struct Event<T: 'static + ?Sized> {
|
||||
}
|
||||
|
||||
impl<T> Event<T> {
|
||||
/// Map the event data to a new type
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// onclick: move |evt: Event<FormData>| {
|
||||
/// let data = evt.map(|data| data.value());
|
||||
/// assert_eq!(data.inner(), "hello world");
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn map<U: 'static, F: FnOnce(&T) -> U>(&self, f: F) -> Event<U> {
|
||||
Event {
|
||||
data: Rc::new(f(&self.data)),
|
||||
propagates: self.propagates.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Prevent this event from continuing to bubble up the tree to parent elements.
|
||||
///
|
||||
/// # Example
|
||||
|
||||
@@ -6,11 +6,13 @@ static CLICKS: Mutex<usize> = Mutex::new(0);
|
||||
|
||||
#[test]
|
||||
fn events_propagate() {
|
||||
set_event_converter(Box::new(dioxus_html::SerializedHtmlEventConverter));
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
_ = dom.rebuild();
|
||||
|
||||
// Top-level click is registered
|
||||
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(1), true);
|
||||
dom.handle_event("click", Rc::new(PlatformEventData::new(Box::<SerializedMouseData>::default())), ElementId(1), true);
|
||||
assert_eq!(*CLICKS.lock().unwrap(), 1);
|
||||
|
||||
// break reference....
|
||||
@@ -20,7 +22,7 @@ fn events_propagate() {
|
||||
}
|
||||
|
||||
// Lower click is registered
|
||||
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(2), true);
|
||||
dom.handle_event("click", Rc::new(PlatformEventData::new(Box::<SerializedMouseData>::default())), ElementId(2), true);
|
||||
assert_eq!(*CLICKS.lock().unwrap(), 3);
|
||||
|
||||
// break reference....
|
||||
@@ -30,14 +32,13 @@ fn events_propagate() {
|
||||
}
|
||||
|
||||
// Stop propagation occurs
|
||||
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(2), true);
|
||||
dom.handle_event("click", Rc::new(PlatformEventData::new(Box::<SerializedMouseData>::default())), ElementId(2), true);
|
||||
assert_eq!(*CLICKS.lock().unwrap(), 3);
|
||||
}
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
render! {
|
||||
div {
|
||||
onclick: move |_| {
|
||||
div { onclick: move |_| {
|
||||
println!("top clicked");
|
||||
*CLICKS.lock().unwrap() += 1;
|
||||
},
|
||||
@@ -53,17 +54,14 @@ fn app(cx: Scope) -> Element {
|
||||
|
||||
fn problematic_child(cx: Scope) -> Element {
|
||||
render! {
|
||||
button {
|
||||
onclick: move |evt| {
|
||||
button { onclick: move |evt| {
|
||||
println!("bottom clicked");
|
||||
let mut clicks = CLICKS.lock().unwrap();
|
||||
|
||||
if *clicks == 3 {
|
||||
evt.stop_propagation();
|
||||
} else {
|
||||
*clicks += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
} }
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
//! Tests for the lifecycle of components.
|
||||
use dioxus::core::{ElementId, Mutation::*};
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_html::SerializedHtmlEventConverter;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
@@ -39,6 +40,7 @@ fn manual_diffing() {
|
||||
|
||||
#[test]
|
||||
fn events_generate() {
|
||||
set_event_converter(Box::new(SerializedHtmlEventConverter));
|
||||
fn app(cx: Scope) -> Element {
|
||||
let count = cx.use_hook(|| 0);
|
||||
|
||||
@@ -56,7 +58,12 @@ fn events_generate() {
|
||||
let mut dom = VirtualDom::new(app);
|
||||
_ = dom.rebuild();
|
||||
|
||||
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(1), true);
|
||||
dom.handle_event(
|
||||
"click",
|
||||
Rc::new(PlatformEventData::new(Box::<SerializedMouseData>::default())),
|
||||
ElementId(1),
|
||||
true,
|
||||
);
|
||||
|
||||
dom.mark_dirty(ScopeId::ROOT);
|
||||
let edits = dom.render_immediate();
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
use crate::dioxus_elements::SerializedMouseData;
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core::ElementId;
|
||||
use dioxus_elements::SerializedHtmlEventConverter;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[test]
|
||||
fn miri_rollover() {
|
||||
set_event_converter(Box::new(SerializedHtmlEventConverter));
|
||||
let mut dom = VirtualDom::new(App);
|
||||
|
||||
_ = dom.rebuild();
|
||||
|
||||
for _ in 0..3 {
|
||||
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(2), true);
|
||||
dom.handle_event(
|
||||
"click",
|
||||
Rc::new(PlatformEventData::new(Box::<SerializedMouseData>::default())),
|
||||
ElementId(2),
|
||||
true,
|
||||
);
|
||||
dom.process_events();
|
||||
_ = dom.render_immediate();
|
||||
}
|
||||
|
||||
+25
-25
@@ -1,13 +1,33 @@
|
||||
//! Verify that tasks get polled by the virtualdom properly, and that we escape wait_for_work safely
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use std::{sync::atomic::AtomicUsize, time::Duration};
|
||||
|
||||
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[cfg(not(miri))]
|
||||
#[tokio::test]
|
||||
async fn it_works() {
|
||||
use dioxus::prelude::*;
|
||||
use std::{sync::atomic::AtomicUsize, time::Duration};
|
||||
|
||||
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
cx.use_hook(|| {
|
||||
cx.spawn(async {
|
||||
for x in 0..10 {
|
||||
tokio::time::sleep(Duration::from_micros(50)).await;
|
||||
POLL_COUNT.fetch_add(x, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
});
|
||||
|
||||
cx.spawn(async {
|
||||
for x in 0..10 {
|
||||
tokio::time::sleep(Duration::from_micros(25)).await;
|
||||
POLL_COUNT.fetch_add(x * 2, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
cx.render(rsx!(()))
|
||||
}
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
|
||||
let _ = dom.rebuild();
|
||||
@@ -24,23 +44,3 @@ async fn it_works() {
|
||||
135
|
||||
);
|
||||
}
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
cx.use_hook(|| {
|
||||
cx.spawn(async {
|
||||
for x in 0..10 {
|
||||
tokio::time::sleep(Duration::from_micros(50)).await;
|
||||
POLL_COUNT.fetch_add(x, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
});
|
||||
|
||||
cx.spawn(async {
|
||||
for x in 0..10 {
|
||||
tokio::time::sleep(Duration::from_micros(25)).await;
|
||||
POLL_COUNT.fetch_add(x * 2, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
cx.render(rsx!(()))
|
||||
}
|
||||
Reference in new issue
Block a user