create tests for signals
This commit is contained in:
1 parent
b0d499036a
commit
717c09c4a3
8 files changed
+234
-5
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, Ref, RefCell, RefMut},
|
cell::{Cell, Ref, RefCell, RefMut},
|
||||||
|
fmt::Debug,
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
@@ -151,6 +152,20 @@ pub struct CopyHandle<T> {
|
|||||||
_marker: PhantomData<T>,
|
_marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> Debug for CopyHandle<T> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
||||||
|
f.write_fmt(format_args!(
|
||||||
|
"{:?}@{:?}",
|
||||||
|
self.raw.data.as_ptr(),
|
||||||
|
self.generation
|
||||||
|
))?;
|
||||||
|
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
|
||||||
|
f.write_fmt(format_args!("{:?}", self.raw.data.as_ptr()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: 'static> CopyHandle<T> {
|
impl<T: 'static> CopyHandle<T> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn validate(&self) -> bool {
|
fn validate(&self) -> bool {
|
||||||
|
|||||||
@@ -9,3 +9,8 @@ edition = "2018"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
dioxus-core = { workspace = true }
|
dioxus-core = { workspace = true }
|
||||||
dioxus-copy = { workspace = true }
|
dioxus-copy = { workspace = true }
|
||||||
|
log.workspace = true
|
||||||
|
simple_logger = "4.2.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
dioxus = { workspace = true }
|
||||||
@@ -38,25 +38,25 @@ macro_rules! impls {
|
|||||||
|
|
||||||
impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for $ty<T> {
|
impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for $ty<T> {
|
||||||
fn add_assign(&mut self, rhs: T) {
|
fn add_assign(&mut self, rhs: T) {
|
||||||
self.set(self.value() + rhs);
|
self.with_mut(|v| *v = *v + rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for $ty<T> {
|
impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for $ty<T> {
|
||||||
fn sub_assign(&mut self, rhs: T) {
|
fn sub_assign(&mut self, rhs: T) {
|
||||||
self.set(self.value() - rhs);
|
self.with_mut(|v| *v = *v - rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for $ty<T> {
|
impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for $ty<T> {
|
||||||
fn mul_assign(&mut self, rhs: T) {
|
fn mul_assign(&mut self, rhs: T) {
|
||||||
self.set(self.value() * rhs);
|
self.with_mut(|v| *v = *v * rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for $ty<T> {
|
impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for $ty<T> {
|
||||||
fn div_assign(&mut self, rhs: T) {
|
fn div_assign(&mut self, rhs: T) {
|
||||||
self.set(self.value() / rhs);
|
self.with_mut(|v| *v = *v / rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,6 +77,11 @@ impl<T: 'static> Signal<T> {
|
|||||||
pub fn read(&self) -> Ref<T> {
|
pub fn read(&self) -> Ref<T> {
|
||||||
let inner = self.inner.read();
|
let inner = self.inner.read();
|
||||||
if let Some(current_scope_id) = current_scope_id() {
|
if let Some(current_scope_id) = current_scope_id() {
|
||||||
|
log::trace!(
|
||||||
|
"{:?} subscribed to {:?}",
|
||||||
|
self.inner.value,
|
||||||
|
current_scope_id
|
||||||
|
);
|
||||||
let mut subscribers = inner.subscribers.borrow_mut();
|
let mut subscribers = inner.subscribers.borrow_mut();
|
||||||
if !subscribers.contains(¤t_scope_id) {
|
if !subscribers.contains(¤t_scope_id) {
|
||||||
subscribers.push(current_scope_id);
|
subscribers.push(current_scope_id);
|
||||||
@@ -98,6 +103,11 @@ impl<T: 'static> Signal<T> {
|
|||||||
{
|
{
|
||||||
let inner = self.inner.read();
|
let inner = self.inner.read();
|
||||||
for &scope_id in &*inner.subscribers.borrow() {
|
for &scope_id in &*inner.subscribers.borrow() {
|
||||||
|
log::trace!(
|
||||||
|
"Write on {:?} triggered update on {:?}",
|
||||||
|
self.inner.value,
|
||||||
|
scope_id
|
||||||
|
);
|
||||||
(inner.update_any)(scope_id);
|
(inner.update_any)(scope_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct CopyValue<T: 'static> {
|
pub struct CopyValue<T: 'static> {
|
||||||
pub value: CopyHandle<T>,
|
pub(crate) value: CopyHandle<T>,
|
||||||
origin_scope: ScopeId,
|
origin_scope: ScopeId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_core::ElementId;
|
||||||
|
use dioxus_signals::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_signals_global() {
|
||||||
|
let mut dom = VirtualDom::new(|cx| {
|
||||||
|
render! {
|
||||||
|
for _ in 0..10 {
|
||||||
|
Child {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fn Child(cx: Scope) -> Element {
|
||||||
|
let signal = create_without_cx();
|
||||||
|
|
||||||
|
render! {
|
||||||
|
"{signal}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _edits = dom.rebuild().santize();
|
||||||
|
|
||||||
|
fn create_without_cx() -> Signal<String> {
|
||||||
|
Signal::new("hello world".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn drop_signals() {
|
||||||
|
let mut dom = VirtualDom::new(|cx| {
|
||||||
|
let generation = cx.generation();
|
||||||
|
|
||||||
|
let count = if generation % 2 == 0 { 10 } else { 0 };
|
||||||
|
render! {
|
||||||
|
for _ in 0..count {
|
||||||
|
Child {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fn Child(cx: Scope) -> Element {
|
||||||
|
let signal = create_without_cx();
|
||||||
|
|
||||||
|
render! {
|
||||||
|
"{signal}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = dom.rebuild().santize();
|
||||||
|
dom.mark_dirty(ScopeId(0));
|
||||||
|
dom.render_immediate();
|
||||||
|
|
||||||
|
fn create_without_cx() -> Signal<String> {
|
||||||
|
Signal::new("hello world".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_core::ElementId;
|
||||||
|
use dioxus_signals::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn effects_rerun() {
|
||||||
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct RunCounter {
|
||||||
|
component: usize,
|
||||||
|
effect: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
let counter = Rc::new(RefCell::new(RunCounter::default()));
|
||||||
|
let mut dom = VirtualDom::new_with_props(
|
||||||
|
|cx| {
|
||||||
|
let counter = cx.props;
|
||||||
|
counter.borrow_mut().component += 1;
|
||||||
|
|
||||||
|
let mut signal = use_signal(cx, || 0);
|
||||||
|
cx.use_hook(move || {
|
||||||
|
to_owned![counter];
|
||||||
|
Effect::new(move || {
|
||||||
|
counter.borrow_mut().effect += 1;
|
||||||
|
println!("Signal: {:?}", signal);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
signal += 1;
|
||||||
|
|
||||||
|
render! {
|
||||||
|
div {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
counter.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = dom.rebuild().santize();
|
||||||
|
|
||||||
|
let current_counter = counter.borrow();
|
||||||
|
assert_eq!(current_counter.component, 1);
|
||||||
|
assert_eq!(current_counter.effect, 2);
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_core::ElementId;
|
||||||
|
use dioxus_signals::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reading_subscribes() {
|
||||||
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct RunCounter {
|
||||||
|
parent: usize,
|
||||||
|
children: HashMap<ScopeId, usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let counter = Rc::new(RefCell::new(RunCounter::default()));
|
||||||
|
let mut dom = VirtualDom::new_with_props(
|
||||||
|
|cx| {
|
||||||
|
let mut signal = use_signal(cx, || 0);
|
||||||
|
|
||||||
|
println!("Parent: {:?}", cx.scope_id());
|
||||||
|
if cx.generation() == 1 {
|
||||||
|
signal += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cx.props.borrow_mut().parent += 1;
|
||||||
|
|
||||||
|
render! {
|
||||||
|
for id in 0..10 {
|
||||||
|
Child {
|
||||||
|
signal: signal,
|
||||||
|
counter: cx.props.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
counter.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
|
#[derive(Props, Clone)]
|
||||||
|
struct ChildProps {
|
||||||
|
signal: Signal<usize>,
|
||||||
|
counter: Rc<RefCell<RunCounter>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for ChildProps {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.signal == other.signal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Child(cx: Scope<ChildProps>) -> Element {
|
||||||
|
println!("Child: {:?}", cx.scope_id());
|
||||||
|
*cx.props
|
||||||
|
.counter
|
||||||
|
.borrow_mut()
|
||||||
|
.children
|
||||||
|
.entry(cx.scope_id())
|
||||||
|
.or_default() += 1;
|
||||||
|
|
||||||
|
render! {
|
||||||
|
"{cx.props.signal}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = dom.rebuild().santize();
|
||||||
|
|
||||||
|
{
|
||||||
|
let current_counter = counter.borrow();
|
||||||
|
assert_eq!(current_counter.parent, 1);
|
||||||
|
|
||||||
|
for (scope_id, rerun_count) in current_counter.children.iter() {
|
||||||
|
assert_eq!(rerun_count, &1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dom.mark_dirty(ScopeId(0));
|
||||||
|
dom.render_immediate();
|
||||||
|
dom.render_immediate();
|
||||||
|
|
||||||
|
{
|
||||||
|
let current_counter = counter.borrow();
|
||||||
|
assert_eq!(current_counter.parent, 2);
|
||||||
|
|
||||||
|
for (scope_id, rerun_count) in current_counter.children.iter() {
|
||||||
|
assert_eq!(rerun_count, &2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user