fix selector and signal unsync default
This commit is contained in:
1 parent
ebbaacf073
commit
6eb4e7358b
5 files changed
+130
-34
No files matched your search
@@ -14,6 +14,7 @@ simple_logger = "4.2.0"
|
|||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
parking_lot = "0.12.1"
|
parking_lot = "0.12.1"
|
||||||
once_cell = "1.18.0"
|
once_cell = "1.18.0"
|
||||||
|
ghost = "0.1.16"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
dioxus = { workspace = true }
|
dioxus = { workspace = true }
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ macro_rules! read_impls {
|
|||||||
($ty:ident, $bound:path) => {
|
($ty:ident, $bound:path) => {
|
||||||
impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
|
impl<T: Default + 'static, S: $bound> Default for $ty<T, S> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(Default::default())
|
Self::new_maybe_sync(Default::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use generational_box::UnsyncStorage;
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@@ -44,7 +45,7 @@ fn owner_in_scope<S: Storage<T>, T>(scope: ScopeId) -> Rc<Owner<S>> {
|
|||||||
/// CopyValue is a wrapper around a value to make the value mutable and Copy.
|
/// CopyValue is a wrapper around a value to make the value mutable and Copy.
|
||||||
///
|
///
|
||||||
/// It is internally backed by [`generational_box::GenerationalBox`].
|
/// It is internally backed by [`generational_box::GenerationalBox`].
|
||||||
pub struct CopyValue<T: 'static, S: Storage<T>> {
|
pub struct CopyValue<T: 'static, S: Storage<T> = UnsyncStorage> {
|
||||||
pub(crate) value: GenerationalBox<T, S>,
|
pub(crate) value: GenerationalBox<T, S>,
|
||||||
origin_scope: ScopeId,
|
origin_scope: ScopeId,
|
||||||
}
|
}
|
||||||
@@ -71,11 +72,25 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, S: Storage<T>> CopyValue<T, S> {
|
impl<T: 'static> CopyValue<T> {
|
||||||
/// Create a new CopyValue. The value will be stored in the current component.
|
/// 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.
|
/// Once the component this value is created in is dropped, the value will be dropped.
|
||||||
pub fn new(value: T) -> Self {
|
pub fn new(value: T) -> Self {
|
||||||
|
Self::new_maybe_sync(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new CopyValue. The value will be stored in the given scope. When the specified scope is dropped, the value will be dropped.
|
||||||
|
pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
|
||||||
|
Self::new_maybe_sync_in_scope(value, scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
pub fn new_maybe_sync(value: T) -> Self {
|
||||||
let owner = current_owner();
|
let owner = current_owner();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
@@ -85,7 +100,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.
|
/// Create a new CopyValue. The value will be stored in the given scope. When the specified scope is dropped, the value will be dropped.
|
||||||
pub fn new_in_scope(value: T, scope: ScopeId) -> Self {
|
pub fn new_maybe_sync_in_scope(value: T, scope: ScopeId) -> Self {
|
||||||
let owner = owner_in_scope(scope);
|
let owner = owner_in_scope(scope);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::dependency::Dependency;
|
|||||||
use crate::use_signal;
|
use crate::use_signal;
|
||||||
use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySignal, Signal};
|
use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySignal, Signal};
|
||||||
|
|
||||||
/// Creates a new Selector. The selector will be run immediately and whenever any signal it reads changes.
|
/// 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.
|
/// Selectors can be used to efficiently compute derived data from signals.
|
||||||
///
|
///
|
||||||
@@ -23,14 +23,41 @@ use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySig
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||||
pub fn use_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
pub fn use_selector<R: PartialEq>(
|
||||||
|
cx: &ScopeState,
|
||||||
|
f: impl FnMut() -> R + 'static,
|
||||||
|
) -> ReadOnlySignal<R> {
|
||||||
|
use_maybe_sync_selector(cx, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Creates a new Selector that may be sync. The selector will be run immediately and whenever any signal it reads changes.
|
||||||
|
///
|
||||||
|
/// Selectors can be used to efficiently compute derived data from signals.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use dioxus::prelude::*;
|
||||||
|
/// use dioxus_signals::*;
|
||||||
|
///
|
||||||
|
/// fn App(cx: Scope) -> Element {
|
||||||
|
/// let mut count = use_signal(cx, || 0);
|
||||||
|
/// let double = use_selector(cx, move || count * 2);
|
||||||
|
/// count += 1;
|
||||||
|
/// assert_eq!(double.value(), count * 2);
|
||||||
|
///
|
||||||
|
/// render! { "{double}" }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[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,
|
cx: &ScopeState,
|
||||||
f: impl FnMut() -> R + 'static,
|
f: impl FnMut() -> R + 'static,
|
||||||
) -> ReadOnlySignal<R, S> {
|
) -> ReadOnlySignal<R, S> {
|
||||||
*cx.use_hook(|| selector(f))
|
*cx.use_hook(|| maybe_sync_selector(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
|
|
||||||
|
/// Creates a new unsync Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
|
||||||
///
|
///
|
||||||
/// Selectors can be used to efficiently compute derived data from signals.
|
/// Selectors can be used to efficiently compute derived data from signals.
|
||||||
///
|
///
|
||||||
@@ -47,7 +74,35 @@ pub fn use_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||||
pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency, S: Storage<SignalData<R>>>(
|
pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency, >(
|
||||||
|
cx: &ScopeState,
|
||||||
|
dependencies: D,
|
||||||
|
mut f: impl FnMut(D::Out) -> R + 'static,
|
||||||
|
) -> ReadOnlySignal<R>
|
||||||
|
where
|
||||||
|
D::Out: 'static,
|
||||||
|
{
|
||||||
|
use_maybe_sync_selector_with_dependencies(cx, dependencies, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new Selector that may be sync with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
|
||||||
|
///
|
||||||
|
/// Selectors can be used to efficiently compute derived data from signals.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use dioxus::prelude::*;
|
||||||
|
/// use dioxus_signals::*;
|
||||||
|
///
|
||||||
|
/// fn App(cx: Scope) -> Element {
|
||||||
|
/// let mut local_state = use_state(cx, || 0);
|
||||||
|
/// let double = use_selector_with_dependencies(cx, (local_state.get(),), move |(local_state,)| local_state * 2);
|
||||||
|
/// local_state.set(1);
|
||||||
|
///
|
||||||
|
/// render! { "{double}" }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||||
|
pub fn use_maybe_sync_selector_with_dependencies<R: PartialEq, D: Dependency, S: Storage<SignalData<R>>>(
|
||||||
cx: &ScopeState,
|
cx: &ScopeState,
|
||||||
dependencies: D,
|
dependencies: D,
|
||||||
mut f: impl FnMut(D::Out) -> R + 'static,
|
mut f: impl FnMut(D::Out) -> R + 'static,
|
||||||
@@ -57,7 +112,7 @@ where
|
|||||||
{
|
{
|
||||||
let dependencies_signal = use_signal(cx, || dependencies.out());
|
let dependencies_signal = use_signal(cx, || dependencies.out());
|
||||||
let selector = *cx.use_hook(|| {
|
let selector = *cx.use_hook(|| {
|
||||||
selector(move || {
|
maybe_sync_selector(move || {
|
||||||
let deref = &*dependencies_signal.read();
|
let deref = &*dependencies_signal.read();
|
||||||
f(deref.clone())
|
f(deref.clone())
|
||||||
})
|
})
|
||||||
@@ -69,10 +124,19 @@ where
|
|||||||
selector
|
selector
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new Selector. The selector will be run immediately and whenever any signal it reads changes.
|
/// 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.
|
/// Selectors can be used to efficiently compute derived data from signals.
|
||||||
pub fn selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
pub fn selector<R: PartialEq>(
|
||||||
|
mut f: impl FnMut() -> R + 'static,
|
||||||
|
) -> ReadOnlySignal<R> {
|
||||||
|
maybe_sync_selector(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
pub fn maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||||
mut f: impl FnMut() -> R + 'static,
|
mut f: impl FnMut() -> R + 'static,
|
||||||
) -> ReadOnlySignal<R, S> {
|
) -> ReadOnlySignal<R, S> {
|
||||||
let state = Signal::<R, S> {
|
let state = Signal::<R, S> {
|
||||||
@@ -108,5 +172,5 @@ pub fn selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
ReadOnlySignal::new(state)
|
ReadOnlySignal::new_maybe_sync(state)
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ pub struct SignalData<T> {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Signal<T: 'static, S: Storage<SignalData<T>>> {
|
pub struct Signal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
|
||||||
pub(crate) inner: CopyValue<SignalData<T>, S>,
|
pub(crate) inner: CopyValue<SignalData<T>, S>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,11 +188,23 @@ impl<'de, T: serde::Deserialize<'de> + 'static> serde::Deserialize<'de> for Sign
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
impl<T: 'static> Signal<T> {
|
||||||
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
|
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
|
||||||
pub fn new(value: T) -> Self {
|
pub fn new(value: T) -> Self {
|
||||||
|
Self::new_maybe_sync(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
|
||||||
|
Self::new_maybe_sync_in_scope(value, owner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
pub fn new_maybe_sync(value: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: CopyValue::new(SignalData {
|
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync(SignalData {
|
||||||
subscribers: Default::default(),
|
subscribers: Default::default(),
|
||||||
update_any: schedule_update_any().expect("in a virtual dom"),
|
update_any: schedule_update_any().expect("in a virtual dom"),
|
||||||
value,
|
value,
|
||||||
@@ -202,9 +214,9 @@ 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.
|
/// 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.
|
||||||
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
|
pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: CopyValue::new_in_scope(
|
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync_in_scope(
|
||||||
SignalData {
|
SignalData {
|
||||||
subscribers: Default::default(),
|
subscribers: Default::default(),
|
||||||
update_any: schedule_update_any().expect("in a virtual dom"),
|
update_any: schedule_update_any().expect("in a virtual dom"),
|
||||||
@@ -228,10 +240,11 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
|||||||
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
) -> <<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T> {
|
||||||
let inner = self.inner.read();
|
let inner = self.inner.read();
|
||||||
if let Some(effect) = inner.effect_stack.current() {
|
if let Some(effect) = inner.effect_stack.current() {
|
||||||
let mut subscribers = inner.subscribers.write();
|
let subscribers = inner.subscribers.read();
|
||||||
let mut effect_subscribers = &mut subscribers.effect_subscribers;
|
if !subscribers.effect_subscribers.contains(&effect) {
|
||||||
if !effect_subscribers.contains(&effect) {
|
drop(subscribers);
|
||||||
effect_subscribers.push(effect);
|
let mut subscribers = inner.subscribers.write();
|
||||||
|
subscribers.effect_subscribers.push(effect);
|
||||||
}
|
}
|
||||||
} else if let Some(current_scope_id) = current_scope_id() {
|
} else if let Some(current_scope_id) = current_scope_id() {
|
||||||
// only subscribe if the vdom is rendering
|
// only subscribe if the vdom is rendering
|
||||||
@@ -241,17 +254,13 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
|||||||
self.inner.value,
|
self.inner.value,
|
||||||
current_scope_id
|
current_scope_id
|
||||||
);
|
);
|
||||||
let mut subscribers = inner.subscribers.write();
|
let subscribers = inner.subscribers.read();
|
||||||
let subscribers = &mut subscribers.subscribers;
|
if !subscribers.subscribers.contains(¤t_scope_id) {
|
||||||
if !subscribers.contains(¤t_scope_id) {
|
|
||||||
subscribers.push(current_scope_id);
|
|
||||||
drop(subscribers);
|
drop(subscribers);
|
||||||
|
let mut subscribers = inner.subscribers.write();
|
||||||
|
subscribers.subscribers.push(current_scope_id);
|
||||||
let unsubscriber = current_unsubscriber();
|
let unsubscriber = current_unsubscriber();
|
||||||
inner
|
subscribers.subscribers.push(unsubscriber.scope);
|
||||||
.subscribers
|
|
||||||
.write()
|
|
||||||
.subscribers
|
|
||||||
.push(unsubscriber.scope);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -441,13 +450,20 @@ impl<T, B: MappableMut<T>, S: Storage<SignalData<I>>, I> DerefMut for Write<T, B
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A signal that can only be read from.
|
/// A signal that can only be read from.
|
||||||
pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>>> {
|
pub struct ReadOnlySignal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
|
||||||
inner: Signal<T, S>,
|
inner: Signal<T, S>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
|
impl<T: 'static> ReadOnlySignal<T> {
|
||||||
/// Create a new read-only signal.
|
/// Create a new read-only signal.
|
||||||
pub fn new(signal: Signal<T, S>) -> Self {
|
pub fn new(signal: Signal<T>) -> Self {
|
||||||
|
Self::new_maybe_sync(signal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static, S: Storage<SignalData<T>>> ReadOnlySignal<T, S> {
|
||||||
|
/// Create a new read-only signal that is maybe sync.
|
||||||
|
pub fn new_maybe_sync(signal: Signal<T, S>) -> Self {
|
||||||
Self { inner: signal }
|
Self { inner: signal }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user