fix signals crate
This commit is contained in:
1 parent
9e388c3c51
commit
6b17d3db1e
11 files changed
+277
-129
No files matched your search
@@ -170,7 +170,7 @@ impl Effect {
|
||||
/// Run the effect callback immediately. Returns `true` if the effect was run. Returns `false` is the effect is dead.
|
||||
pub fn try_run(&self) {
|
||||
tracing::trace!("Running effect: {:?}", self);
|
||||
if let Some(mut inner) = self.inner.try_write() {
|
||||
if let Ok(mut inner) = self.inner.try_write() {
|
||||
{
|
||||
EFFECT_STACK.with(|stack| {
|
||||
stack.effects.write().push(*self);
|
||||
|
||||
@@ -12,32 +12,37 @@ use std::{
|
||||
macro_rules! read_impls {
|
||||
($ty:ident, $bound:path) => {
|
||||
impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn default() -> Self {
|
||||
Self::new_maybe_sync(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: $bound> std::clone::Clone for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: $bound + Copy> Copy for $ty<T, S> {}
|
||||
impl<T, S: $bound> Copy for $ty<T, S> {}
|
||||
|
||||
impl<T: Display + 'static, S: $bound> Display for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.with(|v| Display::fmt(v, f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug + 'static, S: $bound> Debug for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.with(|v| Debug::fmt(v, f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq + 'static, S: $bound> PartialEq<T> for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn eq(&self, other: &T) -> bool {
|
||||
self.with(|v| *v == *other)
|
||||
}
|
||||
@@ -50,18 +55,21 @@ macro_rules! write_impls {
|
||||
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
|
||||
type Output = T;
|
||||
|
||||
#[track_caller]
|
||||
fn add(self, rhs: T) -> Self::Output {
|
||||
self.with(|v| *v + rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn add_assign(&mut self, rhs: T) {
|
||||
self.with_mut(|v| *v = *v + rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn sub_assign(&mut self, rhs: T) {
|
||||
self.with_mut(|v| *v = *v - rhs)
|
||||
}
|
||||
@@ -70,12 +78,14 @@ macro_rules! write_impls {
|
||||
impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
|
||||
type Output = T;
|
||||
|
||||
#[track_caller]
|
||||
fn sub(self, rhs: T) -> Self::Output {
|
||||
self.with(|v| *v - rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn mul_assign(&mut self, rhs: T) {
|
||||
self.with_mut(|v| *v = *v * rhs)
|
||||
}
|
||||
@@ -84,12 +94,14 @@ macro_rules! write_impls {
|
||||
impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
|
||||
type Output = T;
|
||||
|
||||
#[track_caller]
|
||||
fn mul(self, rhs: T) -> Self::Output {
|
||||
self.with(|v| *v * rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
|
||||
#[track_caller]
|
||||
fn div_assign(&mut self, rhs: T) {
|
||||
self.with_mut(|v| *v = *v / rhs)
|
||||
}
|
||||
@@ -98,6 +110,7 @@ macro_rules! write_impls {
|
||||
impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
|
||||
type Output = T;
|
||||
|
||||
#[track_caller]
|
||||
fn div(self, rhs: T) -> Self::Output {
|
||||
self.with(|v| *v / rhs)
|
||||
}
|
||||
@@ -105,51 +118,61 @@ macro_rules! write_impls {
|
||||
|
||||
impl<T: 'static, S: $vec_bound> $ty<Vec<T>, S> {
|
||||
/// Pushes a new value to the end of the vector.
|
||||
#[track_caller]
|
||||
pub fn push(&self, value: T) {
|
||||
self.with_mut(|v| v.push(value))
|
||||
}
|
||||
|
||||
/// Pops the last value from the vector.
|
||||
#[track_caller]
|
||||
pub fn pop(&self) -> Option<T> {
|
||||
self.with_mut(|v| v.pop())
|
||||
}
|
||||
|
||||
/// Inserts a new value at the given index.
|
||||
#[track_caller]
|
||||
pub fn insert(&self, index: usize, value: T) {
|
||||
self.with_mut(|v| v.insert(index, value))
|
||||
}
|
||||
|
||||
/// Removes the value at the given index.
|
||||
#[track_caller]
|
||||
pub fn remove(&self, index: usize) -> T {
|
||||
self.with_mut(|v| v.remove(index))
|
||||
}
|
||||
|
||||
/// Clears the vector, removing all values.
|
||||
#[track_caller]
|
||||
pub fn clear(&self) {
|
||||
self.with_mut(|v| v.clear())
|
||||
}
|
||||
|
||||
/// Extends the vector with the given iterator.
|
||||
#[track_caller]
|
||||
pub fn extend(&self, iter: impl IntoIterator<Item = T>) {
|
||||
self.with_mut(|v| v.extend(iter))
|
||||
}
|
||||
|
||||
/// Truncates the vector to the given length.
|
||||
#[track_caller]
|
||||
pub fn truncate(&self, len: usize) {
|
||||
self.with_mut(|v| v.truncate(len))
|
||||
}
|
||||
|
||||
/// Swaps two values in the vector.
|
||||
#[track_caller]
|
||||
pub fn swap_remove(&self, index: usize) -> T {
|
||||
self.with_mut(|v| v.swap_remove(index))
|
||||
}
|
||||
|
||||
/// Retains only the values that match the given predicate.
|
||||
#[track_caller]
|
||||
pub fn retain(&self, f: impl FnMut(&T) -> bool) {
|
||||
self.with_mut(|v| v.retain(f))
|
||||
}
|
||||
|
||||
/// Splits the vector into two at the given index.
|
||||
#[track_caller]
|
||||
pub fn split_off(&self, at: usize) -> Vec<T> {
|
||||
self.with_mut(|v| v.split_off(at))
|
||||
}
|
||||
@@ -161,6 +184,7 @@ read_impls!(CopyValue, Storage<T>);
|
||||
|
||||
impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
||||
/// Read a value from the inner vector.
|
||||
#[track_caller]
|
||||
pub fn get(&self, index: usize) -> Option<<S::Ref as Mappable<Vec<T>>>::Mapped<T>> {
|
||||
S::Ref::try_map(self.read(), move |v| v.get(index))
|
||||
}
|
||||
@@ -168,6 +192,7 @@ impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
||||
|
||||
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
||||
/// Unwraps the inner value and clones it.
|
||||
#[track_caller]
|
||||
pub fn unwrap(&self) -> T
|
||||
where
|
||||
T: Clone,
|
||||
@@ -176,6 +201,7 @@ impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
||||
}
|
||||
|
||||
/// Attempts to read the inner value of the Option.
|
||||
#[track_caller]
|
||||
pub fn as_ref(&self) -> Option<<S::Ref as Mappable<Option<T>>>::Mapped<T>> {
|
||||
S::Ref::try_map(self.read(), |v| v.as_ref())
|
||||
}
|
||||
@@ -185,21 +211,25 @@ write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
|
||||
|
||||
impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
||||
/// Takes the value out of the Option.
|
||||
#[track_caller]
|
||||
pub fn take(&self) -> Option<T> {
|
||||
self.with_mut(|v| v.take())
|
||||
}
|
||||
|
||||
/// Replace the value in the Option.
|
||||
#[track_caller]
|
||||
pub fn replace(&self, value: T) -> Option<T> {
|
||||
self.with_mut(|v| v.replace(value))
|
||||
}
|
||||
|
||||
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
||||
#[track_caller]
|
||||
pub fn get_or_insert(&self, default: T) -> <S::Ref as Mappable<Option<T>>>::Mapped<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]
|
||||
pub fn get_or_insert_with(
|
||||
&self,
|
||||
default: impl FnOnce() -> T,
|
||||
|
||||
@@ -7,10 +7,7 @@ use std::rc::Rc;
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
use generational_box::{
|
||||
BorrowError, BorrowMutError, GenerationalBox, GenerationalRef, GenerationalRefMut, Owner,
|
||||
Storage, Store,
|
||||
};
|
||||
use generational_box::{GenerationalBox, Owner, Storage};
|
||||
|
||||
use crate::Effect;
|
||||
|
||||
@@ -82,6 +79,7 @@ impl<T: 'static> CopyValue<T> {
|
||||
}
|
||||
|
||||
/// Create a new CopyValue. The value will be stored in the given scope. When the specified scope is dropped, the value will be dropped.
|
||||
#[track_caller]
|
||||
pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
|
||||
Self::new_maybe_sync_in_scope(value, scope)
|
||||
}
|
||||
@@ -91,6 +89,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
||||
/// Create a new CopyValue. The value will be stored in the current component.
|
||||
///
|
||||
/// Once the component this value is created in is dropped, the value will be dropped.
|
||||
#[track_caller]
|
||||
pub fn new_maybe_sync(value: T) -> Self {
|
||||
let owner = current_owner();
|
||||
|
||||
@@ -117,6 +116,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
||||
}
|
||||
|
||||
/// Create a new CopyValue. The value will be stored in the given scope. When the specified scope is dropped, the value will be dropped.
|
||||
#[track_caller]
|
||||
pub fn new_maybe_sync_in_scope(value: T, scope: ScopeId) -> Self {
|
||||
let owner = owner_in_scope(scope);
|
||||
|
||||
@@ -143,7 +143,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
||||
/// Try to read the value. If the value has been dropped, this will return None.
|
||||
#[track_caller]
|
||||
|
||||
pub fn try_read(&self) -> Option<S::Ref> {
|
||||
pub fn try_read(&self) -> Result<S::Ref, generational_box::BorrowError> {
|
||||
self.value.try_read()
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
||||
|
||||
/// Try to write the value. If the value has been dropped, this will return None.
|
||||
#[track_caller]
|
||||
pub fn try_write(&self) -> Option<S::Mut> {
|
||||
pub fn try_write(&self) -> Result<S::Mut, generational_box::BorrowMutError> {
|
||||
self.value.try_write()
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ use crate::{use_signal, EffectInner, EFFECT_STACK};
|
||||
/// render! { "{double}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||
pub fn use_selector<R: PartialEq>(
|
||||
cx: &ScopeState,
|
||||
@@ -47,6 +48,7 @@ pub fn use_selector<R: PartialEq>(
|
||||
/// render! { "{double}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||
pub fn use_maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
cx: &ScopeState,
|
||||
@@ -71,6 +73,7 @@ pub fn use_maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
/// render! { "{double}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||
pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency>(
|
||||
cx: &ScopeState,
|
||||
@@ -99,6 +102,7 @@ where
|
||||
/// render! { "{double}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||
pub fn use_maybe_sync_selector_with_dependencies<
|
||||
R: PartialEq,
|
||||
@@ -129,6 +133,7 @@ where
|
||||
/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
|
||||
///
|
||||
/// Selectors can be used to efficiently compute derived data from signals.
|
||||
#[track_caller]
|
||||
pub fn selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
|
||||
maybe_sync_selector(f)
|
||||
}
|
||||
@@ -136,6 +141,7 @@ pub fn selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<
|
||||
/// Creates a new Selector that may be Sync + Send. The selector will be run immediately and whenever any signal it reads changes.
|
||||
///
|
||||
/// Selectors can be used to efficiently compute derived data from signals.
|
||||
#[track_caller]
|
||||
pub fn maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
mut f: impl FnMut() -> R + 'static,
|
||||
) -> ReadOnlySignal<R, S> {
|
||||
|
||||
@@ -99,11 +99,20 @@ pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<
|
||||
/// }
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[track_caller]
|
||||
pub fn use_signal_sync<T: Send + Sync + 'static>(
|
||||
cx: &ScopeState,
|
||||
f: impl FnOnce() -> T,
|
||||
) -> Signal<T, SyncStorage> {
|
||||
*cx.use_hook(|| Signal::new_maybe_sync(f()))
|
||||
#[cfg(debug_assertions)]
|
||||
let caller = std::panic::Location::caller();
|
||||
*cx.use_hook(|| {
|
||||
Signal::new_with_caller(
|
||||
f(),
|
||||
#[cfg(debug_assertions)]
|
||||
caller,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -207,6 +216,7 @@ impl<T: 'static> Signal<T> {
|
||||
}
|
||||
|
||||
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
|
||||
#[track_caller]
|
||||
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
|
||||
Self::new_maybe_sync_in_scope(value, owner)
|
||||
}
|
||||
@@ -214,6 +224,7 @@ impl<T: 'static> Signal<T> {
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
|
||||
#[track_caller]
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn new_maybe_sync(value: T) -> Self {
|
||||
Self {
|
||||
@@ -246,6 +257,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
}
|
||||
|
||||
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
|
||||
#[track_caller]
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
|
||||
Self {
|
||||
@@ -513,6 +525,7 @@ pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage>
|
||||
|
||||
impl<T: 'static> ReadOnlySignal<T> {
|
||||
/// Create a new read-only signal.
|
||||
#[track_caller]
|
||||
pub fn new(signal: Signal<T>) -> Self {
|
||||
Self::new_maybe_sync(signal)
|
||||
}
|
||||
@@ -520,6 +533,7 @@ impl<T: 'static> ReadOnlySignal<T> {
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
|
||||
/// Create a new read-only signal that is maybe sync.
|
||||
#[track_caller]
|
||||
pub fn new_maybe_sync(signal: Signal<T, S>) -> Self {
|
||||
Self { inner: signal }
|
||||
}
|
||||
@@ -542,7 +556,9 @@ impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
|
||||
/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
|
||||
///
|
||||
/// If the signal has been dropped, this will panic.
|
||||
pub fn peek(&self) -> GenerationalRef<T> {
|
||||
pub fn peek(
|
||||
&self,
|
||||
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
||||
self.inner.peek()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user