Boxed and mapped signals preparing for stores (#4413)
This PR contains just the breaking changes required for the upcoming store implementation. It adds two new primitives: Mutable mapped signals (without a different subscriber list). This lets you scope a writable value and pass it to a child component. It requires two closures to map the immutable and mutable references to the inner signal. With stores, these closures will be derived Boxed readable and writable values (ReadSignal, and WriteSignal). Any readable or writable types can be converted to a read signal or write signal with the boxed method or built in types are automatically converted into the boxed variants when passing into props * map signals mutably * boxed signals * remove mutable borrow type from boxed writable * improve map mut example * restore boxed_mut helper * make boxed readable/writable types copy * Move away from the ReadOnlySignal alias in examples * automatically convert Write * Fix some tests * ReadSignal and WriteSignal * move read only signal alias * make maps copy * store prototype * implement derive macro * Handle generics in the derive macro * Make Selector boxable * derive boxed conversion * Get rid of store type * Remove hardcoded type from derive macro * selector -> store * Refactor store implementation * more vec methods * todo mvc store example * Implement Storable for HashMap * fix write * restore sorting logic in todomvc * derive readable and writable * improve todomvc store example * Result store * Option store * collect dead subscribers * fix clippy and formatting * recognize some common foreign types automatically * fix foreign stores in props * slice and array selectors * clean up store example * remove stores * writable is child owned * fix typo * fix clippy * fix doc tests
This commit is contained in:
1 parent
6919f72abc
commit
d8d11db403
64 files changed
+1307
-726
No files matched your search
+57
-37
@@ -1,94 +1,114 @@
|
||||
use crate::{read::Readable, read_impls, ReadableRef};
|
||||
use dioxus_core::IntoAttributeValue;
|
||||
use generational_box::{AnyStorage, BorrowResult, UnsyncStorage};
|
||||
use std::{ops::Deref, rc::Rc};
|
||||
use crate::{read::Readable, read_impls, ReadableExt, ReadableRef};
|
||||
use dioxus_core::{IntoAttributeValue, Subscribers};
|
||||
use generational_box::{AnyStorage, BorrowResult};
|
||||
use std::ops::Deref;
|
||||
|
||||
/// A read only signal that has been mapped to a new type.
|
||||
pub struct MappedSignal<O: ?Sized + 'static, S: AnyStorage = UnsyncStorage> {
|
||||
try_read: Rc<dyn Fn() -> Result<S::Ref<'static, O>, generational_box::BorrowError> + 'static>,
|
||||
try_peek: Rc<dyn Fn() -> Result<S::Ref<'static, O>, generational_box::BorrowError> + 'static>,
|
||||
pub struct MappedSignal<O: ?Sized + 'static, V: Readable, F = fn(&<V as Readable>::Target) -> &O> {
|
||||
value: V,
|
||||
map_fn: F,
|
||||
_marker: std::marker::PhantomData<O>,
|
||||
}
|
||||
|
||||
impl<O: ?Sized, S: AnyStorage> Clone for MappedSignal<O, S> {
|
||||
impl<V, O, F> Clone for MappedSignal<O, V, F>
|
||||
where
|
||||
V: Readable + Clone,
|
||||
F: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
MappedSignal {
|
||||
try_read: self.try_read.clone(),
|
||||
try_peek: self.try_peek.clone(),
|
||||
value: self.value.clone(),
|
||||
map_fn: self.map_fn.clone(),
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<O, S> MappedSignal<O, S>
|
||||
impl<V, O, F> Copy for MappedSignal<O, V, F>
|
||||
where
|
||||
V: Readable + Copy,
|
||||
F: Copy,
|
||||
{
|
||||
}
|
||||
|
||||
impl<V, O, F> MappedSignal<O, V, F>
|
||||
where
|
||||
O: ?Sized,
|
||||
S: AnyStorage,
|
||||
V: Readable,
|
||||
F: Fn(&V::Target) -> &O,
|
||||
{
|
||||
/// Create a new mapped signal.
|
||||
pub(crate) fn new(
|
||||
try_read: Rc<
|
||||
dyn Fn() -> Result<S::Ref<'static, O>, generational_box::BorrowError> + 'static,
|
||||
>,
|
||||
try_peek: Rc<
|
||||
dyn Fn() -> Result<S::Ref<'static, O>, generational_box::BorrowError> + 'static,
|
||||
>,
|
||||
) -> Self {
|
||||
MappedSignal { try_read, try_peek }
|
||||
pub(crate) fn new(value: V, map_fn: F) -> Self {
|
||||
MappedSignal {
|
||||
value,
|
||||
map_fn,
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<O, S> Readable for MappedSignal<O, S>
|
||||
impl<V, O, F> Readable for MappedSignal<O, V, F>
|
||||
where
|
||||
O: ?Sized,
|
||||
S: AnyStorage,
|
||||
V: Readable,
|
||||
F: Fn(&V::Target) -> &O,
|
||||
{
|
||||
type Target = O;
|
||||
type Storage = S;
|
||||
type Storage = V::Storage;
|
||||
|
||||
fn try_read_unchecked(
|
||||
&self,
|
||||
) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
|
||||
(self.try_read)()
|
||||
let value = self.value.try_read_unchecked()?;
|
||||
Ok(V::Storage::map(value, |v| (self.map_fn)(v)))
|
||||
}
|
||||
|
||||
fn try_peek_unchecked(&self) -> BorrowResult<ReadableRef<'static, Self>> {
|
||||
(self.try_peek)()
|
||||
let value = self.value.try_peek_unchecked()?;
|
||||
Ok(V::Storage::map(value, |v| (self.map_fn)(v)))
|
||||
}
|
||||
|
||||
fn subscribers(&self) -> Option<Subscribers> {
|
||||
self.value.subscribers()
|
||||
}
|
||||
}
|
||||
|
||||
impl<O, S> IntoAttributeValue for MappedSignal<O, S>
|
||||
impl<V, O, F> IntoAttributeValue for MappedSignal<O, V, F>
|
||||
where
|
||||
O: Clone + IntoAttributeValue,
|
||||
S: AnyStorage,
|
||||
V: Readable,
|
||||
F: Fn(&V::Target) -> &O,
|
||||
{
|
||||
fn into_value(self) -> dioxus_core::AttributeValue {
|
||||
self.with(|f| f.clone().into_value())
|
||||
}
|
||||
}
|
||||
|
||||
impl<O, S> PartialEq for MappedSignal<O, S>
|
||||
impl<V, O, F> PartialEq for MappedSignal<O, V, F>
|
||||
where
|
||||
O: ?Sized,
|
||||
S: AnyStorage,
|
||||
V: Readable + PartialEq,
|
||||
F: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
std::ptr::eq(&self.try_peek, &other.try_peek)
|
||||
&& std::ptr::eq(&self.try_read, &other.try_read)
|
||||
self.value == other.value && self.map_fn == other.map_fn
|
||||
}
|
||||
}
|
||||
|
||||
/// Allow calling a signal with signal() syntax
|
||||
///
|
||||
/// Currently only limited to copy types, though could probably specialize for string/arc/rc
|
||||
impl<O, S> Deref for MappedSignal<O, S>
|
||||
/// Currently only limited to clone types, though could probably specialize for string/arc/rc
|
||||
impl<V, O, F> Deref for MappedSignal<O, V, F>
|
||||
where
|
||||
O: Clone,
|
||||
S: AnyStorage + 'static,
|
||||
V: Readable + 'static,
|
||||
F: Fn(&V::Target) -> &O + 'static,
|
||||
{
|
||||
type Target = dyn Fn() -> O;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { Readable::deref_impl(self) }
|
||||
unsafe { ReadableExt::deref_impl(self) }
|
||||
}
|
||||
}
|
||||
|
||||
read_impls!(MappedSignal<T, S: AnyStorage>);
|
||||
read_impls!(MappedSignal<T, V, F> where V: Readable, F: Fn(&V::Target) -> &T);
|
||||
Reference in new issue
Block a user