fix signal error message and add tests (#2118)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-03-20 08:45:27 -07:00
1 parent 58f7efafea
commit 9f283f571f
10 files changed
+187 -121

No files matched your search

-75
View File
@@ -1,75 +0,0 @@
use dioxus::prelude::*;
fn main() {
launch(app);
}
#[derive(Clone, Copy)]
enum ErrorComponent {
Read,
ReadMut,
ReadDropped,
}
fn app() -> Element {
let mut error = use_signal(|| None as Option<ErrorComponent>);
rsx! {
match error() {
Some(ErrorComponent::Read) => rsx! { Read {} },
Some(ErrorComponent::ReadMut) => rsx! { ReadMut {} },
Some(ErrorComponent::ReadDropped) => rsx! { ReadDropped {} },
None => rsx! {
button { onclick: move |_| error.set(Some(ErrorComponent::Read)), "Read" }
button { onclick: move |_| error.set(Some(ErrorComponent::ReadMut)), "ReadMut" }
button { onclick: move |_| error.set(Some(ErrorComponent::ReadDropped)), "ReadDropped"}
}
}
}
}
#[component]
fn Read() -> Element {
let signal = use_signal_sync(|| 0);
let _write = signal.write_unchecked();
let _read = signal.read();
unreachable!()
}
#[component]
fn ReadMut() -> Element {
let signal = use_signal_sync(|| 0);
let _read = signal.read();
let _write = signal.write_unchecked();
unreachable!()
}
#[component]
fn ReadDropped() -> Element {
let signal = use_signal_sync(|| None as Option<SyncSignal<i32>>);
if generation() < 4 {
needs_update();
}
rsx! {
if let Some(value) = signal() {
"{value:?}"
} else {
ReadDroppedSignalChild { parent_signal: signal }
}
}
}
#[component]
fn ReadDroppedSignalChild(parent_signal: SyncSignal<Option<SyncSignal<i32>>>) -> Element {
let signal = use_signal_sync(|| 0);
use_hook(move || parent_signal.set(Some(signal)));
rsx! { "{signal}" }
}
+2
View File
@@ -121,12 +121,14 @@ impl<T: 'static, S: Storage<T>> Readable for CopyValue<T, S> {
type Target = T;
type Storage = S;
#[track_caller]
fn try_read_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
self.value.try_read()
}
#[track_caller]
fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
self.value.read()
}
+1
View File
@@ -67,6 +67,7 @@ pub trait Readable {
/// Get a reference to the value without checking the lifetime. This will subscribe the current scope to the signal.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
#[track_caller]
fn read_unchecked(&self) -> ReadableRef<'static, Self> {
self.try_read_unchecked().unwrap()
}
+3
View File
@@ -52,6 +52,7 @@ pub trait Writable: Readable {
/// Get a mutable reference to the value without checking the lifetime. This will update any subscribers.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
#[track_caller]
fn write_unchecked(&self) -> WritableRef<'static, Self> {
self.try_write_unchecked().unwrap()
}
@@ -114,11 +115,13 @@ pub trait Writable: Readable {
/// An extension trait for Writable<Option<T>> that provides some convenience methods.
pub trait WritableOptionExt<T: 'static>: Writable<Target = Option<T>> {
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
#[track_caller]
fn get_or_insert(&mut self, default: T) -> WritableRef<'_, Self, T> {
self.get_or_insert_with(|| default)
}
/// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
#[track_caller]
fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> WritableRef<'_, Self, T> {
let is_none = self.read().is_none();
if is_none {