mirror of
https://github.com/leptos-rs/leptos.git
synced 2025-12-28 23:02:37 -05:00
Compare commits
5 Commits
cleanup-te
...
once-resou
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca903c1053 | ||
|
|
eef88ac180 | ||
|
|
c594c0e523 | ||
|
|
b8f553c3a1 | ||
|
|
55873e9a10 |
@@ -41,10 +41,7 @@
|
||||
//!
|
||||
//! Use `SyncCallback` if the function is not `Sync` and `Send`.
|
||||
|
||||
use reactive_graph::{
|
||||
owner::{LocalStorage, StoredValue},
|
||||
traits::WithValue,
|
||||
};
|
||||
use reactive_graph::owner::{LocalStorage, StoredValue};
|
||||
use std::{fmt, rc::Rc, sync::Arc};
|
||||
|
||||
/// A wrapper trait for calling callbacks.
|
||||
|
||||
@@ -548,9 +548,7 @@ fn node_to_tokens(
|
||||
view_marker,
|
||||
disable_inert_html,
|
||||
),
|
||||
Node::Block(block) => {
|
||||
Some(quote! { ::leptos::prelude::IntoRender::into_render(#block) })
|
||||
}
|
||||
Node::Block(block) => Some(quote! { #block }),
|
||||
Node::Text(text) => Some(text_to_tokens(&text.value)),
|
||||
Node::RawText(raw) => {
|
||||
let text = raw.to_string_best();
|
||||
@@ -727,11 +725,6 @@ pub(crate) fn element_to_tokens(
|
||||
quote! { ::leptos::tachys::html::element::#custom(#name) }
|
||||
} else if is_svg_element(&tag) {
|
||||
parent_type = TagType::Svg;
|
||||
let name = if tag == "use" || tag == "use_" {
|
||||
Ident::new_raw("use", name.span()).to_token_stream()
|
||||
} else {
|
||||
name.to_token_stream()
|
||||
};
|
||||
quote! { ::leptos::tachys::svg::#name() }
|
||||
} else if is_math_ml_element(&tag) {
|
||||
parent_type = TagType::Math;
|
||||
@@ -885,7 +878,7 @@ fn attribute_to_tokens(
|
||||
NodeName::Path(path) => path.path.get_ident(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let value = attribute_value(node, false);
|
||||
let value = attribute_value(node);
|
||||
quote! {
|
||||
.#node_ref(#value)
|
||||
}
|
||||
@@ -935,13 +928,13 @@ fn attribute_to_tokens(
|
||||
// we don't provide statically-checked methods for SVG attributes
|
||||
|| (tag_type == TagType::Svg && name != "inner_html")
|
||||
{
|
||||
let value = attribute_value(node, true);
|
||||
let value = attribute_value(node);
|
||||
quote! {
|
||||
.attr(#name, #value)
|
||||
}
|
||||
} else {
|
||||
let key = attribute_name(&node.key);
|
||||
let value = attribute_value(node, true);
|
||||
let value = attribute_value(node);
|
||||
|
||||
// special case of global_class and class attribute
|
||||
if &node.key.to_string() == "class"
|
||||
@@ -978,11 +971,11 @@ pub(crate) fn attribute_absolute(
|
||||
let id = &parts[0];
|
||||
match id {
|
||||
NodeNameFragment::Ident(id) => {
|
||||
let value = attribute_value(node);
|
||||
// ignore `let:` and `clone:`
|
||||
if id == "let" || id == "clone" {
|
||||
None
|
||||
} else if id == "attr" {
|
||||
let value = attribute_value(node, true);
|
||||
let key = &parts[1];
|
||||
let key_name = key.to_string();
|
||||
if key_name == "class" || key_name == "style" {
|
||||
@@ -990,7 +983,6 @@ pub(crate) fn attribute_absolute(
|
||||
quote! { ::leptos::tachys::html::#key::#key(#value) },
|
||||
)
|
||||
} else if key_name == "aria" {
|
||||
let value = attribute_value(node, true);
|
||||
let mut parts_iter = parts.iter();
|
||||
parts_iter.next();
|
||||
let fn_name = parts_iter.map(|p| p.to_string()).collect::<Vec<String>>().join("_");
|
||||
@@ -1019,7 +1011,6 @@ pub(crate) fn attribute_absolute(
|
||||
},
|
||||
)
|
||||
} else if id == "style" || id == "class" {
|
||||
let value = attribute_value(node, false);
|
||||
let key = &node.key.to_string();
|
||||
let key = key
|
||||
.replacen("style:", "", 1)
|
||||
@@ -1028,7 +1019,6 @@ pub(crate) fn attribute_absolute(
|
||||
quote! { ::leptos::tachys::html::#id::#id((#key, #value)) },
|
||||
)
|
||||
} else if id == "prop" {
|
||||
let value = attribute_value(node, false);
|
||||
let key = &node.key.to_string();
|
||||
let key = key.replacen("prop:", "", 1);
|
||||
Some(
|
||||
@@ -1085,7 +1075,7 @@ pub(crate) fn two_way_binding_to_tokens(
|
||||
name: &str,
|
||||
node: &KeyedAttribute,
|
||||
) -> TokenStream {
|
||||
let value = attribute_value(node, false);
|
||||
let value = attribute_value(node);
|
||||
|
||||
let ident =
|
||||
format_ident!("{}", name.to_case(UpperCamel), span = node.key.span());
|
||||
@@ -1110,7 +1100,7 @@ pub(crate) fn event_type_and_handler(
|
||||
name: &str,
|
||||
node: &KeyedAttribute,
|
||||
) -> (TokenStream, TokenStream, TokenStream) {
|
||||
let handler = attribute_value(node, false);
|
||||
let handler = attribute_value(node);
|
||||
|
||||
let (event_type, is_custom, is_force_undelegated, is_targeted) =
|
||||
parse_event_name(name);
|
||||
@@ -1167,7 +1157,7 @@ fn class_to_tokens(
|
||||
class: TokenStream,
|
||||
class_name: Option<&str>,
|
||||
) -> TokenStream {
|
||||
let value = attribute_value(node, false);
|
||||
let value = attribute_value(node);
|
||||
if let Some(class_name) = class_name {
|
||||
quote! {
|
||||
.#class((#class_name, #value))
|
||||
@@ -1184,7 +1174,7 @@ fn style_to_tokens(
|
||||
style: TokenStream,
|
||||
style_name: Option<&str>,
|
||||
) -> TokenStream {
|
||||
let value = attribute_value(node, false);
|
||||
let value = attribute_value(node);
|
||||
if let Some(style_name) = style_name {
|
||||
quote! {
|
||||
.#style((#style_name, #value))
|
||||
@@ -1201,7 +1191,7 @@ fn prop_to_tokens(
|
||||
prop: TokenStream,
|
||||
key: &str,
|
||||
) -> TokenStream {
|
||||
let value = attribute_value(node, false);
|
||||
let value = attribute_value(node);
|
||||
quote! {
|
||||
.#prop(#key, #value)
|
||||
}
|
||||
@@ -1358,10 +1348,7 @@ fn attribute_name(name: &NodeName) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
fn attribute_value(
|
||||
attr: &KeyedAttribute,
|
||||
is_attribute_proper: bool,
|
||||
) -> TokenStream {
|
||||
fn attribute_value(attr: &KeyedAttribute) -> TokenStream {
|
||||
match attr.possible_value.to_value() {
|
||||
None => quote! { true },
|
||||
Some(value) => match &value.value {
|
||||
@@ -1376,26 +1363,14 @@ fn attribute_value(
|
||||
}
|
||||
}
|
||||
|
||||
if matches!(expr, Expr::Lit(_)) || !is_attribute_proper {
|
||||
quote! {
|
||||
#expr
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
::leptos::prelude::IntoAttributeValue::into_attribute_value(#expr)
|
||||
}
|
||||
quote! {
|
||||
{#expr}
|
||||
}
|
||||
}
|
||||
// any value in braces: expand as-is to give proper r-a support
|
||||
KVAttributeValue::InvalidBraced(block) => {
|
||||
if is_attribute_proper {
|
||||
quote! {
|
||||
::leptos::prelude::IntoAttributeValue::into_attribute_value(#block)
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#block
|
||||
}
|
||||
quote! {
|
||||
#block
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -35,10 +35,10 @@ impl<T> Clone for ArcLocalResource<T> {
|
||||
|
||||
impl<T> ArcLocalResource<T> {
|
||||
#[track_caller]
|
||||
pub fn new<Fut>(fetcher: impl Fn() -> Fut + 'static) -> Self
|
||||
pub fn new<Fut>(fetcher: impl Fn() -> Fut + Send + Sync + 'static) -> Self
|
||||
where
|
||||
T: 'static,
|
||||
Fut: Future<Output = T> + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
Fut: Future<Output = T> + Send + 'static,
|
||||
{
|
||||
let fetcher = move || {
|
||||
let fut = fetcher();
|
||||
@@ -60,7 +60,7 @@ impl<T> ArcLocalResource<T> {
|
||||
}
|
||||
};
|
||||
Self {
|
||||
data: ArcAsyncDerived::new_unsync(fetcher),
|
||||
data: ArcAsyncDerived::new(fetcher),
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: Location::caller(),
|
||||
}
|
||||
@@ -103,7 +103,7 @@ impl<T> DefinedAt for ArcLocalResource<T> {
|
||||
|
||||
impl<T> ReadUntracked for ArcLocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
type Value = ReadGuard<Option<T>, AsyncPlain<Option<T>>>;
|
||||
|
||||
@@ -201,7 +201,7 @@ impl<T> LocalResource<T> {
|
||||
pub fn new<Fut>(fetcher: impl Fn() -> Fut + 'static) -> Self
|
||||
where
|
||||
T: 'static,
|
||||
Fut: Future<Output = T> + 'static,
|
||||
Fut: Future<Output = T> + Send + 'static,
|
||||
{
|
||||
let fetcher = move || {
|
||||
let fut = fetcher();
|
||||
@@ -230,7 +230,7 @@ impl<T> LocalResource<T> {
|
||||
let fetcher = SendWrapper::new(fetcher);
|
||||
AsyncDerived::new(move || {
|
||||
let fut = fetcher();
|
||||
SendWrapper::new(async move { SendWrapper::new(fut.await) })
|
||||
async move { SendWrapper::new(fut.await) }
|
||||
})
|
||||
},
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -280,7 +280,7 @@ impl<T> DefinedAt for LocalResource<T> {
|
||||
|
||||
impl<T> ReadUntracked for LocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
type Value =
|
||||
ReadGuard<Option<SendWrapper<T>>, AsyncPlain<Option<SendWrapper<T>>>>;
|
||||
@@ -307,7 +307,7 @@ impl<T: 'static> IsDisposed for LocalResource<T> {
|
||||
|
||||
impl<T: 'static> ToAnySource for LocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
fn to_any_source(&self) -> AnySource {
|
||||
self.data.to_any_source()
|
||||
@@ -316,7 +316,7 @@ where
|
||||
|
||||
impl<T: 'static> ToAnySubscriber for LocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
fn to_any_subscriber(&self) -> AnySubscriber {
|
||||
self.data.to_any_subscriber()
|
||||
@@ -325,7 +325,7 @@ where
|
||||
|
||||
impl<T> Source for LocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
fn add_subscriber(&self, subscriber: AnySubscriber) {
|
||||
self.data.add_subscriber(subscriber)
|
||||
@@ -342,7 +342,7 @@ where
|
||||
|
||||
impl<T> ReactiveNode for LocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
fn mark_dirty(&self) {
|
||||
self.data.mark_dirty();
|
||||
@@ -363,7 +363,7 @@ where
|
||||
|
||||
impl<T> Subscriber for LocalResource<T>
|
||||
where
|
||||
T: 'static,
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
fn add_source(&self, source: AnySource) {
|
||||
self.data.add_source(source);
|
||||
|
||||
@@ -434,6 +434,14 @@ pub struct OnceResource<T, Ser = JsonSerdeCodec> {
|
||||
defined_at: &'static Location<'static>,
|
||||
}
|
||||
|
||||
impl<T, Ser> Clone for OnceResource<T, Ser> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> Copy for OnceResource<T, Ser> {}
|
||||
|
||||
impl<T, Ser> OnceResource<T, Ser>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
|
||||
@@ -15,7 +15,6 @@ use crate::{
|
||||
};
|
||||
pub use arc_memo::*;
|
||||
pub use async_derived::*;
|
||||
pub(crate) use inner::MemoInner;
|
||||
pub use memo::*;
|
||||
pub use selector::*;
|
||||
|
||||
|
||||
@@ -12,14 +12,12 @@ use std::{
|
||||
sync::{Arc, RwLock, Weak},
|
||||
};
|
||||
|
||||
mod arc_stored_value;
|
||||
mod arena;
|
||||
mod arena_item;
|
||||
mod context;
|
||||
mod storage;
|
||||
mod stored_value;
|
||||
use self::arena::Arena;
|
||||
pub use arc_stored_value::ArcStoredValue;
|
||||
#[cfg(feature = "sandboxed-arenas")]
|
||||
pub use arena::sandboxed::Sandboxed;
|
||||
#[cfg(feature = "sandboxed-arenas")]
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
use crate::{
|
||||
signal::guards::{Plain, ReadGuard, UntrackedWriteGuard},
|
||||
traits::{DefinedAt, IsDisposed, ReadValue, WriteValue},
|
||||
};
|
||||
use std::{
|
||||
fmt::{Debug, Formatter},
|
||||
hash::Hash,
|
||||
panic::Location,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
/// A reference-counted getter for any value non-reactively.
|
||||
///
|
||||
/// This is a reference-counted value, which is `Clone` but not `Copy`.
|
||||
/// For arena-allocated `Copy` values, use [`StoredValue`](super::StoredValue).
|
||||
///
|
||||
/// This allows you to create a stable reference for any value by storing it within
|
||||
/// the reactive system. Unlike e.g. [`ArcRwSignal`](crate::signal::ArcRwSignal), it is not reactive;
|
||||
/// accessing it does not cause effects to subscribe, and
|
||||
/// updating it does not notify anything else.
|
||||
pub struct ArcStoredValue<T> {
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: &'static Location<'static>,
|
||||
value: Arc<RwLock<T>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for ArcStoredValue<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: self.defined_at,
|
||||
value: Arc::clone(&self.value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Debug for ArcStoredValue<T> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("ArcStoredValue")
|
||||
.field("type", &std::any::type_name::<T>())
|
||||
.field("value", &Arc::as_ptr(&self.value))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default> Default for ArcStoredValue<T> {
|
||||
#[track_caller]
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: Location::caller(),
|
||||
value: Arc::new(RwLock::new(T::default())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PartialEq for ArcStoredValue<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Arc::ptr_eq(&self.value, &other.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Eq for ArcStoredValue<T> {}
|
||||
|
||||
impl<T> Hash for ArcStoredValue<T> {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
std::ptr::hash(&Arc::as_ptr(&self.value), state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DefinedAt for ArcStoredValue<T> {
|
||||
fn defined_at(&self) -> Option<&'static Location<'static>> {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
Some(self.defined_at)
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ArcStoredValue<T> {
|
||||
/// Creates a new stored value, taking the initial value as its argument.
|
||||
#[cfg_attr(
|
||||
feature = "tracing",
|
||||
tracing::instrument(level = "trace", skip_all)
|
||||
)]
|
||||
#[track_caller]
|
||||
pub fn new(value: T) -> Self {
|
||||
Self {
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: Location::caller(),
|
||||
value: Arc::new(RwLock::new(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ReadValue for ArcStoredValue<T>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
type Value = ReadGuard<T, Plain<T>>;
|
||||
|
||||
fn try_read_value(&self) -> Option<ReadGuard<T, Plain<T>>> {
|
||||
Plain::try_new(Arc::clone(&self.value)).map(ReadGuard::new)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> WriteValue for ArcStoredValue<T>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
fn try_write_value(&self) -> Option<UntrackedWriteGuard<T>> {
|
||||
UntrackedWriteGuard::try_new(self.value.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IsDisposed for ArcStoredValue<T> {
|
||||
fn is_disposed(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
use super::{
|
||||
arc_stored_value::ArcStoredValue, ArenaItem, LocalStorage, Storage,
|
||||
SyncStorage,
|
||||
};
|
||||
use super::{ArenaItem, LocalStorage, Storage, SyncStorage};
|
||||
use crate::{
|
||||
signal::guards::{Plain, ReadGuard, UntrackedWriteGuard},
|
||||
traits::{DefinedAt, Dispose, IsDisposed, ReadValue, WriteValue},
|
||||
traits::{DefinedAt, Dispose, IsDisposed},
|
||||
unwrap_signal,
|
||||
};
|
||||
use or_poisoned::OrPoisoned;
|
||||
use std::{
|
||||
fmt::{Debug, Formatter},
|
||||
hash::Hash,
|
||||
panic::Location,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
/// A **non-reactive**, `Copy` handle for any value.
|
||||
@@ -20,8 +18,9 @@ use std::{
|
||||
/// and [`RwSignal`](crate::signal::RwSignal)), it is `Copy` and `'static`. Unlike the signal
|
||||
/// types, it is not reactive; accessing it does not cause effects to subscribe, and
|
||||
/// updating it does not notify anything else.
|
||||
#[derive(Debug)]
|
||||
pub struct StoredValue<T, S = SyncStorage> {
|
||||
value: ArenaItem<ArcStoredValue<T>, S>,
|
||||
value: ArenaItem<Arc<RwLock<T>>, S>,
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: &'static Location<'static>,
|
||||
}
|
||||
@@ -34,18 +33,6 @@ impl<T, S> Clone for StoredValue<T, S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Debug for StoredValue<T, S>
|
||||
where
|
||||
S: Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("StoredValue")
|
||||
.field("type", &std::any::type_name::<T>())
|
||||
.field("value", &self.value)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> PartialEq for StoredValue<T, S> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.value == other.value
|
||||
@@ -76,13 +63,13 @@ impl<T, S> DefinedAt for StoredValue<T, S> {
|
||||
impl<T, S> StoredValue<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<ArcStoredValue<T>>,
|
||||
S: Storage<Arc<RwLock<T>>>,
|
||||
{
|
||||
/// Stores the given value in the arena allocator.
|
||||
#[track_caller]
|
||||
pub fn new_with_storage(value: T) -> Self {
|
||||
Self {
|
||||
value: ArenaItem::new_with_storage(ArcStoredValue::new(value)),
|
||||
value: ArenaItem::new_with_storage(Arc::new(RwLock::new(value))),
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: Location::caller(),
|
||||
}
|
||||
@@ -92,7 +79,7 @@ where
|
||||
impl<T, S> Default for StoredValue<T, S>
|
||||
where
|
||||
T: Default + 'static,
|
||||
S: Storage<ArcStoredValue<T>>,
|
||||
S: Storage<Arc<RwLock<T>>>,
|
||||
{
|
||||
#[track_caller] // Default trait is not annotated with #[track_caller]
|
||||
fn default() -> Self {
|
||||
@@ -122,31 +109,268 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> ReadValue for StoredValue<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<ArcStoredValue<T>>,
|
||||
{
|
||||
type Value = ReadGuard<T, Plain<T>>;
|
||||
|
||||
fn try_read_value(&self) -> Option<ReadGuard<T, Plain<T>>> {
|
||||
impl<T, S: Storage<Arc<RwLock<T>>>> StoredValue<T, S> {
|
||||
/// Returns an [`Option`] of applying a function to the value within the [`StoredValue`].
|
||||
///
|
||||
/// If the owner of the reactive node has not been disposed [`Some`] is returned. Calling this
|
||||
/// function after the owner has been disposed will always return [`None`].
|
||||
///
|
||||
/// See [`StoredValue::with_value`] for a version that panics in the case of the owner being
|
||||
/// disposed.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
/// # use reactive_graph::traits::Dispose;
|
||||
///
|
||||
/// // Does not implement Clone
|
||||
/// struct Data {
|
||||
/// rows: Vec<u8>,
|
||||
/// }
|
||||
///
|
||||
/// let data = StoredValue::new(Data {
|
||||
/// rows: vec![0, 1, 2, 3, 4],
|
||||
/// });
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static.
|
||||
/// // *NOTE* this is not the same thing as a derived signal!
|
||||
/// // *NOTE* this will not be automatically rerun as StoredValue is NOT reactive!
|
||||
/// let length_fn = move || data.try_with_value(|inner| inner.rows.len());
|
||||
///
|
||||
/// let sum = data.try_with_value(|inner| inner.rows.iter().sum::<u8>());
|
||||
///
|
||||
/// assert_eq!(sum, Some(10));
|
||||
/// assert_eq!(length_fn(), Some(5));
|
||||
///
|
||||
/// // You should not call dispose yourself in normal user code.
|
||||
/// // This is shown here for the sake of the example.
|
||||
/// data.dispose();
|
||||
///
|
||||
/// let last = data.try_with_value(|inner| inner.rows.last().cloned());
|
||||
///
|
||||
/// assert_eq!(last, None);
|
||||
/// assert_eq!(length_fn(), None);
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn try_with_value<U>(&self, fun: impl FnOnce(&T) -> U) -> Option<U> {
|
||||
self.value
|
||||
.try_get_value()
|
||||
.and_then(|inner| inner.try_read_value())
|
||||
.map(|inner| fun(&*inner.read().or_poisoned()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WriteValue for StoredValue<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<ArcStoredValue<T>>,
|
||||
{
|
||||
type Value = T;
|
||||
/// Returns the output of applying a function to the value within the [`StoredValue`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics when called after the owner of the reactive node has been disposed.
|
||||
/// See [`StoredValue::try_with_value`] for a version without panic.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
///
|
||||
/// // Does not implement Clone
|
||||
/// struct Data {
|
||||
/// rows: Vec<u8>,
|
||||
/// }
|
||||
///
|
||||
/// let data = StoredValue::new(Data {
|
||||
/// rows: vec![1, 2, 3],
|
||||
/// });
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static.
|
||||
/// // *NOTE* this is not the same thing as a derived signal!
|
||||
/// // *NOTE* this will not be automatically rerun as StoredValue is NOT reactive!
|
||||
/// let length_fn = move || data.with_value(|inner| inner.rows.len());
|
||||
///
|
||||
/// let sum = data.with_value(|inner| inner.rows.iter().sum::<u8>());
|
||||
///
|
||||
/// assert_eq!(sum, 6);
|
||||
/// assert_eq!(length_fn(), 3);
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn with_value<U>(&self, fun: impl FnOnce(&T) -> U) -> U {
|
||||
self.try_with_value(fun)
|
||||
.unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
|
||||
fn try_write_value(&self) -> Option<UntrackedWriteGuard<T>> {
|
||||
/// Returns a read guard to the stored data, or `None` if the owner of the reactive node has been disposed.
|
||||
#[track_caller]
|
||||
pub fn try_read_value(&self) -> Option<ReadGuard<T, Plain<T>>> {
|
||||
self.value
|
||||
.try_get_value()
|
||||
.and_then(|inner| inner.try_write_value())
|
||||
.and_then(|inner| Plain::try_new(inner).map(ReadGuard::new))
|
||||
}
|
||||
|
||||
/// Returns a read guard to the stored data.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics when called after the owner of the reactive node has been disposed.
|
||||
/// See [`StoredValue::try_read_value`] for a version without panic.
|
||||
#[track_caller]
|
||||
pub fn read_value(&self) -> ReadGuard<T, Plain<T>> {
|
||||
self.try_read_value().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
|
||||
/// Returns a write guard to the stored data, or `None` if the owner of the reactive node has been disposed.
|
||||
#[track_caller]
|
||||
pub fn try_write_value(&self) -> Option<UntrackedWriteGuard<T>> {
|
||||
self.value
|
||||
.try_get_value()
|
||||
.and_then(|inner| UntrackedWriteGuard::try_new(inner))
|
||||
}
|
||||
|
||||
/// Returns a write guard to the stored data.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics when called after the owner of the reactive node has been disposed.
|
||||
/// See [`StoredValue::try_write_value`] for a version without panic.
|
||||
#[track_caller]
|
||||
pub fn write_value(&self) -> UntrackedWriteGuard<T> {
|
||||
self.try_write_value().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
|
||||
/// Updates the current value by applying the given closure, returning the return value of the
|
||||
/// closure, or `None` if the value has already been disposed.
|
||||
pub fn try_update_value<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&mut T) -> U,
|
||||
) -> Option<U> {
|
||||
self.value
|
||||
.try_get_value()
|
||||
.map(|inner| fun(&mut *inner.write().or_poisoned()))
|
||||
}
|
||||
|
||||
/// Updates the value within [`StoredValue`] by applying a function to it.
|
||||
///
|
||||
/// # Panics
|
||||
/// This function panics when called after the owner of the reactive node has been disposed.
|
||||
/// See [`StoredValue::try_update_value`] for a version without panic.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
///
|
||||
/// #[derive(Default)] // Does not implement Clone
|
||||
/// struct Data {
|
||||
/// rows: Vec<u8>,
|
||||
/// }
|
||||
///
|
||||
/// let data = StoredValue::new(Data::default());
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static.
|
||||
/// // *NOTE* this is not the same thing as a derived signal!
|
||||
/// // *NOTE* this will not be automatically rerun as StoredValue is NOT reactive!
|
||||
/// let push_next = move || {
|
||||
/// data.update_value(|inner| match inner.rows.last().as_deref() {
|
||||
/// Some(n) => inner.rows.push(n + 1),
|
||||
/// None => inner.rows.push(0),
|
||||
/// })
|
||||
/// };
|
||||
///
|
||||
/// data.update_value(|inner| inner.rows = vec![5, 6, 7]);
|
||||
/// data.with_value(|inner| assert_eq!(inner.rows.last(), Some(&7)));
|
||||
///
|
||||
/// push_next();
|
||||
/// data.with_value(|inner| assert_eq!(inner.rows.last(), Some(&8)));
|
||||
///
|
||||
/// data.update_value(|inner| {
|
||||
/// std::mem::take(inner) // sets Data back to default
|
||||
/// });
|
||||
/// data.with_value(|inner| assert!(inner.rows.is_empty()));
|
||||
/// ```
|
||||
pub fn update_value<U>(&self, fun: impl FnOnce(&mut T) -> U) {
|
||||
self.try_update_value(fun);
|
||||
}
|
||||
|
||||
/// Sets the value within [`StoredValue`].
|
||||
///
|
||||
/// Returns [`Some`] containing the passed value if the owner of the reactive node has been
|
||||
/// disposed.
|
||||
///
|
||||
/// For types that do not implement [`Clone`], or in cases where allocating the entire object
|
||||
/// would be too expensive, prefer [`StoredValue::try_update_value`].
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
/// # use reactive_graph::traits::Dispose;
|
||||
///
|
||||
/// let data = StoredValue::new(String::default());
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static.
|
||||
/// // *NOTE* this is not the same thing as a derived signal!
|
||||
/// // *NOTE* this will not be automatically rerun as StoredValue is NOT reactive!
|
||||
/// let say_hello = move || {
|
||||
/// // Note that using the `update` methods would be more efficient here.
|
||||
/// data.try_set_value("Hello, World!".into())
|
||||
/// };
|
||||
/// // *NOTE* this is not the same thing as a derived signal!
|
||||
/// // *NOTE* this will not be automatically rerun as StoredValue is NOT reactive!
|
||||
/// let reset = move || {
|
||||
/// // Note that using the `update` methods would be more efficient here.
|
||||
/// data.try_set_value(Default::default())
|
||||
/// };
|
||||
/// assert_eq!(data.get_value(), "");
|
||||
///
|
||||
/// // None is returned because the value was able to be updated
|
||||
/// assert_eq!(say_hello(), None);
|
||||
///
|
||||
/// assert_eq!(data.get_value(), "Hello, World!");
|
||||
///
|
||||
/// reset();
|
||||
/// assert_eq!(data.get_value(), "");
|
||||
///
|
||||
/// // You should not call dispose yourself in normal user code.
|
||||
/// // This is shown here for the sake of the example.
|
||||
/// data.dispose();
|
||||
///
|
||||
/// // The reactive owner is disposed, so the value we intended to set is now
|
||||
/// // returned as some.
|
||||
/// assert_eq!(say_hello().as_deref(), Some("Hello, World!"));
|
||||
/// assert_eq!(reset().as_deref(), Some(""));
|
||||
/// ```
|
||||
pub fn try_set_value(&self, value: T) -> Option<T> {
|
||||
match self.value.try_get_value() {
|
||||
Some(inner) => {
|
||||
*inner.write().or_poisoned() = value;
|
||||
None
|
||||
}
|
||||
None => Some(value),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the value within [`StoredValue`].
|
||||
///
|
||||
/// For types that do not implement [`Clone`], or in cases where allocating the entire object
|
||||
/// would be too expensive, prefer [`StoredValue::update_value`].
|
||||
///
|
||||
/// # Panics
|
||||
/// This function panics when called after the owner of the reactive node has been disposed.
|
||||
/// See [`StoredValue::try_set_value`] for a version without panic.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
///
|
||||
/// let data = StoredValue::new(10);
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static.
|
||||
/// // *NOTE* this is not the same thing as a derived signal!
|
||||
/// // *NOTE* this will not be automatically rerun as StoredValue is NOT reactive!
|
||||
/// let maxout = move || data.set_value(u8::MAX);
|
||||
/// let zero = move || data.set_value(u8::MIN);
|
||||
///
|
||||
/// maxout();
|
||||
/// assert_eq!(data.get_value(), u8::MAX);
|
||||
///
|
||||
/// zero();
|
||||
/// assert_eq!(data.get_value(), u8::MIN);
|
||||
/// ```
|
||||
pub fn set_value(&self, value: T) {
|
||||
self.update_value(|n| *n = value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,39 +380,90 @@ impl<T, S> IsDisposed for StoredValue<T, S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: Storage<Arc<RwLock<T>>>> StoredValue<T, S>
|
||||
where
|
||||
T: Clone + 'static,
|
||||
{
|
||||
/// Returns the value within [`StoredValue`] by cloning.
|
||||
///
|
||||
/// Returns [`Some`] containing the value if the owner of the reactive node has not been
|
||||
/// disposed. When disposed, returns [`None`].
|
||||
///
|
||||
/// See [`StoredValue::try_with_value`] for a version that avoids cloning. See
|
||||
/// [`StoredValue::get_value`] for a version that clones, but panics if the node is disposed.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
/// # use reactive_graph::traits::Dispose;
|
||||
///
|
||||
/// // u8 is practically free to clone.
|
||||
/// let data: StoredValue<u8> = StoredValue::new(10);
|
||||
///
|
||||
/// // Larger data structures can become very expensive to clone.
|
||||
/// // You may prefer to use StoredValue::try_with_value.
|
||||
/// let _expensive: StoredValue<Vec<String>> = StoredValue::new(vec![]);
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static
|
||||
/// let maxout = move || data.set_value(u8::MAX);
|
||||
/// let zero = move || data.set_value(u8::MIN);
|
||||
///
|
||||
/// maxout();
|
||||
/// assert_eq!(data.try_get_value(), Some(u8::MAX));
|
||||
///
|
||||
/// zero();
|
||||
/// assert_eq!(data.try_get_value(), Some(u8::MIN));
|
||||
///
|
||||
/// // You should not call dispose yourself in normal user code.
|
||||
/// // This is shown here for the sake of the example.
|
||||
/// data.dispose();
|
||||
///
|
||||
/// assert_eq!(data.try_get_value(), None);
|
||||
/// ```
|
||||
pub fn try_get_value(&self) -> Option<T> {
|
||||
self.try_with_value(T::clone)
|
||||
}
|
||||
|
||||
/// Returns the value within [`StoredValue`] by cloning.
|
||||
///
|
||||
/// See [`StoredValue::with_value`] for a version that avoids cloning.
|
||||
///
|
||||
/// # Panics
|
||||
/// This function panics when called after the owner of the reactive node has been disposed.
|
||||
/// See [`StoredValue::try_get_value`] for a version without panic.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use reactive_graph::owner::StoredValue; let owner = reactive_graph::owner::Owner::new(); owner.set();
|
||||
///
|
||||
/// // u8 is practically free to clone.
|
||||
/// let data: StoredValue<u8> = StoredValue::new(10);
|
||||
///
|
||||
/// // Larger data structures can become very expensive to clone.
|
||||
/// // You may prefer to use StoredValue::try_with_value.
|
||||
/// let _expensive: StoredValue<Vec<String>> = StoredValue::new(vec![]);
|
||||
///
|
||||
/// // Easy to move into closures because StoredValue is Copy + 'static
|
||||
/// let maxout = move || data.set_value(u8::MAX);
|
||||
/// let zero = move || data.set_value(u8::MIN);
|
||||
///
|
||||
/// maxout();
|
||||
/// assert_eq!(data.get_value(), u8::MAX);
|
||||
///
|
||||
/// zero();
|
||||
/// assert_eq!(data.get_value(), u8::MIN);
|
||||
/// ```
|
||||
pub fn get_value(&self) -> T {
|
||||
self.with_value(T::clone)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Dispose for StoredValue<T, S> {
|
||||
fn dispose(self) {
|
||||
self.value.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<ArcStoredValue<T>> for StoredValue<T>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
#[track_caller]
|
||||
fn from(value: ArcStoredValue<T>) -> Self {
|
||||
StoredValue {
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: Location::caller(),
|
||||
value: ArenaItem::new(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> From<StoredValue<T, S>> for ArcStoredValue<T>
|
||||
where
|
||||
S: Storage<ArcStoredValue<T>>,
|
||||
{
|
||||
#[track_caller]
|
||||
fn from(value: StoredValue<T, S>) -> Self {
|
||||
value
|
||||
.value
|
||||
.try_get_value()
|
||||
.unwrap_or_else(unwrap_signal!(value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new [`StoredValue`].
|
||||
#[inline(always)]
|
||||
#[track_caller]
|
||||
|
||||
@@ -52,7 +52,7 @@ use crate::{
|
||||
effect::Effect,
|
||||
graph::{Observer, Source, Subscriber, ToAnySource},
|
||||
owner::Owner,
|
||||
signal::{arc_signal, guards::UntrackedWriteGuard, ArcReadSignal},
|
||||
signal::{arc_signal, ArcReadSignal},
|
||||
};
|
||||
use any_spawner::Executor;
|
||||
use futures::{Stream, StreamExt};
|
||||
@@ -639,189 +639,3 @@ pub fn panic_getting_disposed_signal(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A variation of the [`Read`] trait that provides a signposted "always-non-reactive" API.
|
||||
/// E.g. for [`StoredValue`](`crate::owner::StoredValue`).
|
||||
pub trait ReadValue: Sized + DefinedAt {
|
||||
/// The guard type that will be returned, which can be dereferenced to the value.
|
||||
type Value: Deref;
|
||||
|
||||
/// Returns the non-reactive guard, or `None` if the value has already been disposed.
|
||||
#[track_caller]
|
||||
fn try_read_value(&self) -> Option<Self::Value>;
|
||||
|
||||
/// Returns the non-reactive guard.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if you try to access a value that has been disposed.
|
||||
#[track_caller]
|
||||
fn read_value(&self) -> Self::Value {
|
||||
self.try_read_value().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
/// A variation of the [`With`] trait that provides a signposted "always-non-reactive" API.
|
||||
/// E.g. for [`StoredValue`](`crate::owner::StoredValue`).
|
||||
pub trait WithValue: DefinedAt {
|
||||
/// The type of the value contained in the value.
|
||||
type Value: ?Sized;
|
||||
|
||||
/// Applies the closure to the value, non-reactively, and returns the result,
|
||||
/// or `None` if the value has already been disposed.
|
||||
#[track_caller]
|
||||
fn try_with_value<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&Self::Value) -> U,
|
||||
) -> Option<U>;
|
||||
|
||||
/// Applies the closure to the value, non-reactively, and returns the result.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if you try to access a value that has been disposed.
|
||||
#[track_caller]
|
||||
fn with_value<U>(&self, fun: impl FnOnce(&Self::Value) -> U) -> U {
|
||||
self.try_with_value(fun)
|
||||
.unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> WithValue for T
|
||||
where
|
||||
T: DefinedAt + ReadValue,
|
||||
{
|
||||
type Value = <<Self as ReadValue>::Value as Deref>::Target;
|
||||
|
||||
fn try_with_value<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&Self::Value) -> U,
|
||||
) -> Option<U> {
|
||||
self.try_read_value().map(|value| fun(&value))
|
||||
}
|
||||
}
|
||||
|
||||
/// A variation of the [`Get`] trait that provides a signposted "always-non-reactive" API.
|
||||
/// E.g. for [`StoredValue`](`crate::owner::StoredValue`).
|
||||
pub trait GetValue: DefinedAt {
|
||||
/// The type of the value contained in the value.
|
||||
type Value: Clone;
|
||||
|
||||
/// Clones and returns the value of the value, non-reactively,
|
||||
/// or `None` if the value has already been disposed.
|
||||
#[track_caller]
|
||||
fn try_get_value(&self) -> Option<Self::Value>;
|
||||
|
||||
/// Clones and returns the value of the value, non-reactively.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if you try to access a value that has been disposed.
|
||||
#[track_caller]
|
||||
fn get_value(&self) -> Self::Value {
|
||||
self.try_get_value().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GetValue for T
|
||||
where
|
||||
T: WithValue,
|
||||
T::Value: Clone,
|
||||
{
|
||||
type Value = <Self as WithValue>::Value;
|
||||
|
||||
fn try_get_value(&self) -> Option<Self::Value> {
|
||||
self.try_with_value(Self::Value::clone)
|
||||
}
|
||||
}
|
||||
|
||||
/// A variation of the [`Write`] trait that provides a signposted "always-non-reactive" API.
|
||||
/// E.g. for [`StoredValue`](`crate::owner::StoredValue`).
|
||||
pub trait WriteValue: Sized + DefinedAt {
|
||||
/// The type of the value's value.
|
||||
type Value: Sized + 'static;
|
||||
|
||||
/// Returns a non-reactive write guard, or `None` if the value has already been disposed.
|
||||
#[track_caller]
|
||||
fn try_write_value(&self) -> Option<UntrackedWriteGuard<Self::Value>>;
|
||||
|
||||
/// Returns a non-reactive write guard.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if you try to access a value that has been disposed.
|
||||
#[track_caller]
|
||||
fn write_value(&self) -> UntrackedWriteGuard<Self::Value> {
|
||||
self.try_write_value().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
/// A variation of the [`Update`] trait that provides a signposted "always-non-reactive" API.
|
||||
/// E.g. for [`StoredValue`](`crate::owner::StoredValue`).
|
||||
pub trait UpdateValue: DefinedAt {
|
||||
/// The type of the value contained in the value.
|
||||
type Value;
|
||||
|
||||
/// Updates the value, returning the value that is
|
||||
/// returned by the update function, or `None` if the value has already been disposed.
|
||||
#[track_caller]
|
||||
fn try_update_value<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&mut Self::Value) -> U,
|
||||
) -> Option<U>;
|
||||
|
||||
/// Updates the value.
|
||||
#[track_caller]
|
||||
fn update_value(&self, fun: impl FnOnce(&mut Self::Value)) {
|
||||
self.try_update_value(fun);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> UpdateValue for T
|
||||
where
|
||||
T: WriteValue,
|
||||
{
|
||||
type Value = <Self as WriteValue>::Value;
|
||||
|
||||
#[track_caller]
|
||||
fn try_update_value<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&mut Self::Value) -> U,
|
||||
) -> Option<U> {
|
||||
let mut guard = self.try_write_value()?;
|
||||
Some(fun(&mut *guard))
|
||||
}
|
||||
}
|
||||
|
||||
/// A variation of the [`Set`] trait that provides a signposted "always-non-reactive" API.
|
||||
/// E.g. for [`StoredValue`](`crate::owner::StoredValue`).
|
||||
pub trait SetValue: DefinedAt {
|
||||
/// The type of the value contained in the value.
|
||||
type Value;
|
||||
|
||||
/// Updates the value by replacing it, non-reactively.
|
||||
///
|
||||
/// If the value has already been disposed, returns `Some(value)` with the value that was
|
||||
/// passed in. Otherwise, returns `None`.
|
||||
#[track_caller]
|
||||
fn try_set_value(&self, value: Self::Value) -> Option<Self::Value>;
|
||||
|
||||
/// Updates the value by replacing it, non-reactively.
|
||||
#[track_caller]
|
||||
fn set_value(&self, value: Self::Value) {
|
||||
self.try_set_value(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> SetValue for T
|
||||
where
|
||||
T: WriteValue,
|
||||
{
|
||||
type Value = <Self as WriteValue>::Value;
|
||||
|
||||
fn try_set_value(&self, value: Self::Value) -> Option<Self::Value> {
|
||||
// Unlike most other traits, for these None actually means success:
|
||||
if let Some(mut guard) = self.try_write_value() {
|
||||
*guard = value;
|
||||
None
|
||||
} else {
|
||||
Some(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,15 @@
|
||||
/// Types that abstract over signals with values that can be read.
|
||||
pub mod read {
|
||||
use crate::{
|
||||
computed::{ArcMemo, Memo, MemoInner},
|
||||
computed::{ArcMemo, Memo},
|
||||
graph::untrack,
|
||||
owner::{
|
||||
ArcStoredValue, ArenaItem, FromLocal, LocalStorage, Storage,
|
||||
SyncStorage,
|
||||
},
|
||||
signal::{
|
||||
guards::{Mapped, Plain, ReadGuard},
|
||||
ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal,
|
||||
},
|
||||
traits::{
|
||||
DefinedAt, Dispose, Get, Read, ReadUntracked, ReadValue, Track,
|
||||
With, WithValue,
|
||||
},
|
||||
owner::{ArenaItem, FromLocal, LocalStorage, Storage, SyncStorage},
|
||||
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
|
||||
traits::{DefinedAt, Dispose, Get, With, WithUntracked},
|
||||
unwrap_signal,
|
||||
};
|
||||
use send_wrapper::SendWrapper;
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
fmt::Display,
|
||||
ops::Deref,
|
||||
panic::Location,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
use std::{panic::Location, sync::Arc};
|
||||
|
||||
/// Possibilities for the inner type of a [`Signal`].
|
||||
pub enum SignalTypes<T, S = SyncStorage>
|
||||
@@ -39,8 +24,6 @@ pub mod read {
|
||||
Memo(ArcMemo<T, S>),
|
||||
/// A derived signal.
|
||||
DerivedSignal(Arc<dyn Fn() -> T + Send + Sync>),
|
||||
/// A static, stored value.
|
||||
Stored(ArcStoredValue<T>),
|
||||
}
|
||||
|
||||
impl<T, S> Clone for SignalTypes<T, S>
|
||||
@@ -52,7 +35,6 @@ pub mod read {
|
||||
Self::ReadSignal(arg0) => Self::ReadSignal(arg0.clone()),
|
||||
Self::Memo(arg0) => Self::Memo(arg0.clone()),
|
||||
Self::DerivedSignal(arg0) => Self::DerivedSignal(arg0.clone()),
|
||||
Self::Stored(arg0) => Self::Stored(arg0.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,9 +52,6 @@ pub mod read {
|
||||
Self::DerivedSignal(_) => {
|
||||
f.debug_tuple("DerivedSignal").finish()
|
||||
}
|
||||
Self::Stored(arg0) => {
|
||||
f.debug_tuple("Static").field(arg0).finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -189,39 +168,6 @@ pub mod read {
|
||||
defined_at: std::panic::Location::caller(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves a static, nonreactive value into a signal, backed by [`ArcStoredValue`].
|
||||
#[track_caller]
|
||||
pub fn stored(value: T) -> Self {
|
||||
Self {
|
||||
inner: SignalTypes::Stored(ArcStoredValue::new(value)),
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: std::panic::Location::caller(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> ArcSignal<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
/// Subscribes to this signal in the current reactive scope without doing anything with its value.
|
||||
#[track_caller]
|
||||
pub fn track(&self) {
|
||||
match &self.inner {
|
||||
SignalTypes::ReadSignal(i) => {
|
||||
i.track();
|
||||
}
|
||||
SignalTypes::Memo(i) => {
|
||||
i.track();
|
||||
}
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
i();
|
||||
}
|
||||
// Doesn't change.
|
||||
SignalTypes::Stored(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for ArcSignal<T, SyncStorage>
|
||||
@@ -229,7 +175,7 @@ pub mod read {
|
||||
T: Default + Send + Sync + 'static,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::stored(Default::default())
|
||||
Self::derive(|| Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,6 +231,24 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WithUntracked for ArcSignal<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
fn try_with_untracked<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&Self::Value) -> U,
|
||||
) -> Option<U> {
|
||||
match &self.inner {
|
||||
SignalTypes::ReadSignal(i) => i.try_with_untracked(fun),
|
||||
SignalTypes::Memo(i) => i.try_with_untracked(fun),
|
||||
SignalTypes::DerivedSignal(i) => Some(untrack(|| fun(&i()))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> With for ArcSignal<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
@@ -300,63 +264,10 @@ pub mod read {
|
||||
SignalTypes::ReadSignal(i) => i.try_with(fun),
|
||||
SignalTypes::Memo(i) => i.try_with(fun),
|
||||
SignalTypes::DerivedSignal(i) => Some(fun(&i())),
|
||||
SignalTypes::Stored(i) => i.try_with_value(fun),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> ReadUntracked for ArcSignal<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
type Value = ReadGuard<T, SignalReadGuard<T, S>>;
|
||||
|
||||
fn try_read_untracked(&self) -> Option<Self::Value> {
|
||||
match &self.inner {
|
||||
SignalTypes::ReadSignal(i) => {
|
||||
i.try_read_untracked().map(SignalReadGuard::Read)
|
||||
}
|
||||
SignalTypes::Memo(i) => {
|
||||
i.try_read_untracked().map(SignalReadGuard::Memo)
|
||||
}
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
Some(SignalReadGuard::Owned(untrack(|| i())))
|
||||
}
|
||||
SignalTypes::Stored(i) => {
|
||||
i.try_read_value().map(SignalReadGuard::Read)
|
||||
}
|
||||
}
|
||||
.map(ReadGuard::new)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Read for ArcSignal<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
type Value = ReadGuard<T, SignalReadGuard<T, S>>;
|
||||
|
||||
fn try_read(&self) -> Option<Self::Value> {
|
||||
match &self.inner {
|
||||
SignalTypes::ReadSignal(i) => {
|
||||
i.try_read().map(SignalReadGuard::Read)
|
||||
}
|
||||
SignalTypes::Memo(i) => i.try_read().map(SignalReadGuard::Memo),
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
Some(SignalReadGuard::Owned(i()))
|
||||
}
|
||||
SignalTypes::Stored(i) => {
|
||||
i.try_read_value().map(SignalReadGuard::Read)
|
||||
}
|
||||
}
|
||||
.map(ReadGuard::new)
|
||||
}
|
||||
|
||||
fn read(&self) -> Self::Value {
|
||||
self.try_read().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper for any kind of arena-allocated reactive signal:
|
||||
/// an [`ReadSignal`], [`Memo`], [`RwSignal`], or derived signal closure.
|
||||
///
|
||||
@@ -432,6 +343,31 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WithUntracked for Signal<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
fn try_with_untracked<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&Self::Value) -> U,
|
||||
) -> Option<U> {
|
||||
self.inner
|
||||
// clone the inner Arc type and release the lock
|
||||
// prevents deadlocking if the derived value includes taking a lock on the arena
|
||||
.try_with_value(Clone::clone)
|
||||
.and_then(|inner| match &inner {
|
||||
SignalTypes::ReadSignal(i) => i.try_with_untracked(fun),
|
||||
SignalTypes::Memo(i) => i.try_with_untracked(fun),
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
Some(untrack(|| fun(&i())))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> With for Signal<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
@@ -451,79 +387,10 @@ pub mod read {
|
||||
SignalTypes::ReadSignal(i) => i.try_with(fun),
|
||||
SignalTypes::Memo(i) => i.try_with(fun),
|
||||
SignalTypes::DerivedSignal(i) => Some(fun(&i())),
|
||||
SignalTypes::Stored(i) => i.try_with_value(fun),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> ReadUntracked for Signal<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
type Value = ReadGuard<T, SignalReadGuard<T, S>>;
|
||||
|
||||
fn try_read_untracked(&self) -> Option<Self::Value> {
|
||||
self.inner
|
||||
// clone the inner Arc type and release the lock
|
||||
// prevents deadlocking if the derived value includes taking a lock on the arena
|
||||
.try_with_value(Clone::clone)
|
||||
.and_then(|inner| {
|
||||
match &inner {
|
||||
SignalTypes::ReadSignal(i) => {
|
||||
i.try_read_untracked().map(SignalReadGuard::Read)
|
||||
}
|
||||
SignalTypes::Memo(i) => {
|
||||
i.try_read_untracked().map(SignalReadGuard::Memo)
|
||||
}
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
Some(SignalReadGuard::Owned(untrack(|| i())))
|
||||
}
|
||||
SignalTypes::Stored(i) => {
|
||||
i.try_read_value().map(SignalReadGuard::Read)
|
||||
}
|
||||
}
|
||||
.map(ReadGuard::new)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Read for Signal<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
type Value = ReadGuard<T, SignalReadGuard<T, S>>;
|
||||
|
||||
fn try_read(&self) -> Option<Self::Value> {
|
||||
self.inner
|
||||
// clone the inner Arc type and release the lock
|
||||
// prevents deadlocking if the derived value includes taking a lock on the arena
|
||||
.try_with_value(Clone::clone)
|
||||
.and_then(|inner| {
|
||||
match &inner {
|
||||
SignalTypes::ReadSignal(i) => {
|
||||
i.try_read().map(SignalReadGuard::Read)
|
||||
}
|
||||
SignalTypes::Memo(i) => {
|
||||
i.try_read().map(SignalReadGuard::Memo)
|
||||
}
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
Some(SignalReadGuard::Owned(i()))
|
||||
}
|
||||
SignalTypes::Stored(i) => {
|
||||
i.try_read_value().map(SignalReadGuard::Read)
|
||||
}
|
||||
}
|
||||
.map(ReadGuard::new)
|
||||
})
|
||||
}
|
||||
|
||||
fn read(&self) -> Self::Value {
|
||||
self.try_read().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Signal<T>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
@@ -566,18 +433,6 @@ pub mod read {
|
||||
defined_at: std::panic::Location::caller(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves a static, nonreactive value into a signal, backed by [`ArcStoredValue`].
|
||||
#[track_caller]
|
||||
pub fn stored(value: T) -> Self {
|
||||
Self {
|
||||
inner: ArenaItem::new_with_storage(SignalTypes::Stored(
|
||||
ArcStoredValue::new(value),
|
||||
)),
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: std::panic::Location::caller(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Signal<T, LocalStorage>
|
||||
@@ -605,49 +460,6 @@ pub mod read {
|
||||
defined_at: std::panic::Location::caller(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves a static, nonreactive value into a signal, backed by [`ArcStoredValue`].
|
||||
/// Works like [`Signal::stored`] but uses [`LocalStorage`].
|
||||
#[track_caller]
|
||||
pub fn stored_local(value: T) -> Self {
|
||||
Self {
|
||||
inner: ArenaItem::new_local(SignalTypes::Stored(
|
||||
ArcStoredValue::new(value),
|
||||
)),
|
||||
#[cfg(debug_assertions)]
|
||||
defined_at: std::panic::Location::caller(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Signal<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
/// Subscribes to this signal in the current reactive scope without doing anything with its value.
|
||||
#[track_caller]
|
||||
pub fn track(&self) {
|
||||
let inner = self
|
||||
.inner
|
||||
// clone the inner Arc type and release the lock
|
||||
// prevents deadlocking if the derived value includes taking a lock on the arena
|
||||
.try_with_value(Clone::clone)
|
||||
.unwrap_or_else(unwrap_signal!(self));
|
||||
match inner {
|
||||
SignalTypes::ReadSignal(i) => {
|
||||
i.track();
|
||||
}
|
||||
SignalTypes::Memo(i) => {
|
||||
i.track();
|
||||
}
|
||||
SignalTypes::DerivedSignal(i) => {
|
||||
i();
|
||||
}
|
||||
// Doesn't change.
|
||||
SignalTypes::Stored(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Signal<T>
|
||||
@@ -655,7 +467,7 @@ pub mod read {
|
||||
T: Send + Sync + Default + 'static,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::stored(Default::default())
|
||||
Self::derive(|| Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -664,34 +476,34 @@ pub mod read {
|
||||
T: Default + 'static,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::stored_local(Default::default())
|
||||
Self::derive_local(|| Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Send + Sync + 'static> From<T> for ArcSignal<T, SyncStorage> {
|
||||
impl<T: Clone + Send + Sync + 'static> From<T> for ArcSignal<T, SyncStorage> {
|
||||
#[track_caller]
|
||||
fn from(value: T) -> Self {
|
||||
ArcSignal::stored(value)
|
||||
Self::derive(move || value.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Signal<T>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
T: Clone + Send + Sync + 'static,
|
||||
{
|
||||
#[track_caller]
|
||||
fn from(value: T) -> Self {
|
||||
Self::stored(value)
|
||||
Self::derive(move || value.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Signal<T, LocalStorage>
|
||||
where
|
||||
T: 'static,
|
||||
T: Clone + 'static,
|
||||
{
|
||||
#[track_caller]
|
||||
fn from(value: T) -> Self {
|
||||
Self::stored_local(value)
|
||||
Self::derive_local(move || value.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -903,6 +715,23 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WithUntracked for MaybeSignal<T, S>
|
||||
where
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
fn try_with_untracked<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&Self::Value) -> U,
|
||||
) -> Option<U> {
|
||||
match self {
|
||||
Self::Static(t) => Some(fun(t)),
|
||||
Self::Dynamic(s) => s.try_with_untracked(fun),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> With for MaybeSignal<T, S>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
@@ -921,44 +750,6 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> ReadUntracked for MaybeSignal<T, S>
|
||||
where
|
||||
T: Clone,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
type Value = ReadGuard<T, SignalReadGuard<T, S>>;
|
||||
|
||||
fn try_read_untracked(&self) -> Option<Self::Value> {
|
||||
match self {
|
||||
Self::Static(t) => {
|
||||
Some(ReadGuard::new(SignalReadGuard::Owned(t.clone())))
|
||||
}
|
||||
Self::Dynamic(s) => s.try_read_untracked(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Read for MaybeSignal<T, S>
|
||||
where
|
||||
T: Clone,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
type Value = ReadGuard<T, SignalReadGuard<T, S>>;
|
||||
|
||||
fn try_read(&self) -> Option<Self::Value> {
|
||||
match self {
|
||||
Self::Static(t) => {
|
||||
Some(ReadGuard::new(SignalReadGuard::Owned(t.clone())))
|
||||
}
|
||||
Self::Dynamic(s) => s.try_read(),
|
||||
}
|
||||
}
|
||||
|
||||
fn read(&self) -> Self::Value {
|
||||
self.try_read().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MaybeSignal<T>
|
||||
where
|
||||
T: Send + Sync,
|
||||
@@ -980,21 +771,6 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> MaybeSignal<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<SignalTypes<T, S>> + Storage<T>,
|
||||
{
|
||||
/// Subscribes to this signal in the current reactive scope without doing anything with its value.
|
||||
#[track_caller]
|
||||
pub fn track(&self) {
|
||||
match self {
|
||||
Self::Static(_) => {}
|
||||
Self::Dynamic(signal) => signal.track(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for MaybeSignal<T, SyncStorage>
|
||||
where
|
||||
SyncStorage: Storage<T>,
|
||||
@@ -1117,7 +893,7 @@ pub mod read {
|
||||
|
||||
impl<S> From<&str> for MaybeSignal<String, S>
|
||||
where
|
||||
S: Storage<String> + Storage<Arc<RwLock<String>>>,
|
||||
S: Storage<String>,
|
||||
{
|
||||
fn from(value: &str) -> Self {
|
||||
Self::Static(value.to_string())
|
||||
@@ -1192,6 +968,23 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WithUntracked for MaybeProp<T, S>
|
||||
where
|
||||
S: Storage<SignalTypes<Option<T>, S>> + Storage<Option<T>>,
|
||||
{
|
||||
type Value = Option<T>;
|
||||
|
||||
fn try_with_untracked<U>(
|
||||
&self,
|
||||
fun: impl FnOnce(&Self::Value) -> U,
|
||||
) -> Option<U> {
|
||||
match &self.0 {
|
||||
None => Some(fun(&None)),
|
||||
Some(inner) => inner.try_with_untracked(fun),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> With for MaybeProp<T, S>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
@@ -1210,40 +1003,6 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> ReadUntracked for MaybeProp<T, S>
|
||||
where
|
||||
T: Clone,
|
||||
S: Storage<SignalTypes<Option<T>, S>> + Storage<Option<T>>,
|
||||
{
|
||||
type Value = ReadGuard<Option<T>, SignalReadGuard<Option<T>, S>>;
|
||||
|
||||
fn try_read_untracked(&self) -> Option<Self::Value> {
|
||||
match &self.0 {
|
||||
None => Some(ReadGuard::new(SignalReadGuard::Owned(None))),
|
||||
Some(inner) => inner.try_read_untracked(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Read for MaybeProp<T, S>
|
||||
where
|
||||
T: Clone,
|
||||
S: Storage<SignalTypes<Option<T>, S>> + Storage<Option<T>>,
|
||||
{
|
||||
type Value = ReadGuard<Option<T>, SignalReadGuard<Option<T>, S>>;
|
||||
|
||||
fn try_read(&self) -> Option<Self::Value> {
|
||||
match &self.0 {
|
||||
None => Some(ReadGuard::new(SignalReadGuard::Owned(None))),
|
||||
Some(inner) => inner.try_read(),
|
||||
}
|
||||
}
|
||||
|
||||
fn read(&self) -> Self::Value {
|
||||
self.try_read().unwrap_or_else(unwrap_signal!(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MaybeProp<T>
|
||||
where
|
||||
T: Send + Sync,
|
||||
@@ -1257,21 +1016,6 @@ pub mod read {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> MaybeProp<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: Storage<SignalTypes<Option<T>, S>> + Storage<Option<T>>,
|
||||
{
|
||||
/// Subscribes to this signal in the current reactive scope without doing anything with its value.
|
||||
#[track_caller]
|
||||
pub fn track(&self) {
|
||||
match &self.0 {
|
||||
None => {}
|
||||
Some(signal) => signal.track(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for MaybeProp<T>
|
||||
where
|
||||
SyncStorage: Storage<Option<T>>,
|
||||
@@ -1498,76 +1242,6 @@ pub mod read {
|
||||
Self(Some(MaybeSignal::from_local(Some(value.to_string()))))
|
||||
}
|
||||
}
|
||||
|
||||
/// The content of a [`Signal`] wrapper read guard, variable depending on the signal type.
|
||||
#[derive(Debug)]
|
||||
pub enum SignalReadGuard<T: 'static, S: Storage<T>> {
|
||||
/// A read signal guard.
|
||||
Read(ReadGuard<T, Plain<T>>),
|
||||
/// A memo guard.
|
||||
Memo(ReadGuard<T, Mapped<Plain<MemoInner<T, S>>, T>>),
|
||||
/// A fake guard for derived signals, the content had to actually be cloned, so it's not a guard but we pretend it is.
|
||||
Owned(T),
|
||||
}
|
||||
|
||||
impl<T, S> Clone for SignalReadGuard<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
T: Clone,
|
||||
Plain<T>: Clone,
|
||||
Mapped<Plain<MemoInner<T, S>>, T>: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
SignalReadGuard::Read(i) => SignalReadGuard::Read(i.clone()),
|
||||
SignalReadGuard::Memo(i) => SignalReadGuard::Memo(i.clone()),
|
||||
SignalReadGuard::Owned(i) => SignalReadGuard::Owned(i.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Deref for SignalReadGuard<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
type Target = T;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match self {
|
||||
SignalReadGuard::Read(i) => i,
|
||||
SignalReadGuard::Memo(i) => i,
|
||||
SignalReadGuard::Owned(i) => i,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Borrow<T> for SignalReadGuard<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
{
|
||||
fn borrow(&self) -> &T {
|
||||
self.deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> PartialEq<T> for SignalReadGuard<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
T: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &T) -> bool {
|
||||
self.deref() == other
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Display for SignalReadGuard<T, S>
|
||||
where
|
||||
S: Storage<T>,
|
||||
T: Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
Display::fmt(&**self, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Types that abstract over the ability to update a signal.
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
use reactive_graph::{
|
||||
computed::Memo,
|
||||
owner::{on_cleanup, Owner},
|
||||
signal::{RwSignal, Trigger},
|
||||
traits::{Dispose, GetUntracked, Track},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn cleanup_on_dispose() {
|
||||
let owner = Owner::new();
|
||||
owner.set();
|
||||
|
||||
struct ExecuteOnDrop(Option<Box<dyn FnOnce() + Send + Sync>>);
|
||||
|
||||
impl ExecuteOnDrop {
|
||||
fn new(f: impl FnOnce() + Send + Sync + 'static) -> Self {
|
||||
Self(Some(Box::new(f)))
|
||||
}
|
||||
}
|
||||
impl Drop for ExecuteOnDrop {
|
||||
fn drop(&mut self) {
|
||||
self.0.take().unwrap()();
|
||||
}
|
||||
}
|
||||
|
||||
let trigger = Trigger::new();
|
||||
|
||||
println!("STARTING");
|
||||
|
||||
let memo = Memo::new(move |_| {
|
||||
trigger.track();
|
||||
|
||||
// An example of why you might want to do this is that
|
||||
// when something goes out of reactive scope you want it to be cleaned up.
|
||||
// The cleaning up might have side effects, and those side effects might cause
|
||||
// re-renders where new `on_cleanup` are registered.
|
||||
let on_drop = ExecuteOnDrop::new(|| {
|
||||
on_cleanup(|| println!("Nested cleanup in progress."))
|
||||
});
|
||||
|
||||
on_cleanup(move || {
|
||||
println!("Cleanup in progress.");
|
||||
drop(on_drop)
|
||||
});
|
||||
});
|
||||
println!("Memo 1: {:?}", memo);
|
||||
memo.get_untracked(); // First cleanup registered.
|
||||
|
||||
memo.dispose(); // Cleanup not run here.
|
||||
|
||||
println!("Cleanup should have been executed.");
|
||||
|
||||
let memo = Memo::new(move |_| {
|
||||
// New cleanup registered. It'll panic here.
|
||||
on_cleanup(move || println!("Test passed."));
|
||||
});
|
||||
println!("Memo 2: {:?}", memo);
|
||||
println!("^ Note how the memos have the same key (different versions).");
|
||||
memo.get_untracked(); // First cleanup registered.
|
||||
|
||||
println!("Test passed.");
|
||||
|
||||
memo.dispose();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leak_on_dispose() {
|
||||
let owner = Owner::new();
|
||||
owner.set();
|
||||
|
||||
let trigger = Trigger::new();
|
||||
|
||||
let value = Arc::new(());
|
||||
let weak = Arc::downgrade(&value);
|
||||
|
||||
let memo = Memo::new(move |_| {
|
||||
trigger.track();
|
||||
|
||||
RwSignal::new(value.clone());
|
||||
});
|
||||
|
||||
memo.get_untracked();
|
||||
|
||||
memo.dispose();
|
||||
|
||||
assert!(weak.upgrade().is_none()); // Should have been dropped.
|
||||
}
|
||||
@@ -11,26 +11,6 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
/// Declares that this type can be converted into some other type, which is a valid attribute value.
|
||||
pub trait IntoAttributeValue {
|
||||
/// The attribute value into which this type can be converted.
|
||||
type Output;
|
||||
|
||||
/// Consumes this value, transforming it into an attribute value.
|
||||
fn into_attribute_value(self) -> Self::Output;
|
||||
}
|
||||
|
||||
impl<T> IntoAttributeValue for T
|
||||
where
|
||||
T: AttributeValue,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn into_attribute_value(self) -> Self::Output {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A possible value for an HTML attribute.
|
||||
pub trait AttributeValue: Send {
|
||||
/// The state that should be retained between building and rebuilding.
|
||||
|
||||
@@ -4,8 +4,8 @@ use crate::{
|
||||
renderer::{CastFrom, Rndr},
|
||||
ssr::StreamBuilder,
|
||||
view::{
|
||||
add_attr::AddAnyAttr, IntoRender, Mountable, Position, PositionState,
|
||||
Render, RenderHtml, ToTemplate,
|
||||
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
|
||||
RenderHtml, ToTemplate,
|
||||
},
|
||||
};
|
||||
use const_str_slice_concat::{
|
||||
@@ -65,13 +65,11 @@ impl<E, At, Ch, NewChild> ElementChild<NewChild> for HtmlElement<E, At, Ch>
|
||||
where
|
||||
E: ElementWithChildren,
|
||||
Ch: Render + NextTuple,
|
||||
<Ch as NextTuple>::Output<NewChild::Output>: Render,
|
||||
<Ch as NextTuple>::Output<NewChild>: Render,
|
||||
|
||||
NewChild: IntoRender,
|
||||
NewChild::Output: Render,
|
||||
NewChild: Render,
|
||||
{
|
||||
type Output =
|
||||
HtmlElement<E, At, <Ch as NextTuple>::Output<NewChild::Output>>;
|
||||
type Output = HtmlElement<E, At, <Ch as NextTuple>::Output<NewChild>>;
|
||||
|
||||
fn child(self, child: NewChild) -> Self::Output {
|
||||
let HtmlElement {
|
||||
@@ -84,7 +82,7 @@ where
|
||||
tag,
|
||||
|
||||
attributes,
|
||||
children: children.next_tuple(child.into_render()),
|
||||
children: children.next_tuple(child),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +116,7 @@ where
|
||||
/// Adds a child to the element.
|
||||
pub trait ElementChild<NewChild>
|
||||
where
|
||||
NewChild: IntoRender,
|
||||
NewChild: Render,
|
||||
{
|
||||
/// The type of the element, with the child added.
|
||||
type Output;
|
||||
|
||||
@@ -19,7 +19,6 @@ pub mod prelude {
|
||||
OnAttribute, OnTargetAttribute, PropAttribute,
|
||||
StyleAttribute,
|
||||
},
|
||||
IntoAttributeValue,
|
||||
},
|
||||
directive::DirectiveAttribute,
|
||||
element::{ElementChild, ElementExt, InnerHtmlAttribute},
|
||||
@@ -27,8 +26,8 @@ pub mod prelude {
|
||||
},
|
||||
renderer::{dom::Dom, Renderer},
|
||||
view::{
|
||||
add_attr::AddAnyAttr, any_view::IntoAny, IntoRender, Mountable,
|
||||
Render, RenderHtml,
|
||||
add_attr::AddAnyAttr, any_view::IntoAny, Mountable, Render,
|
||||
RenderHtml,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ macro_rules! svg_elements {
|
||||
($($tag:ident [$($attr:ty),*]),* $(,)?) => {
|
||||
paste::paste! {
|
||||
$(
|
||||
/// An SVG element.
|
||||
/// An SVG attribute.
|
||||
// `tag()` function
|
||||
#[allow(non_snake_case)]
|
||||
pub fn $tag() -> HtmlElement<[<$tag:camel>], (), ()>
|
||||
@@ -151,33 +151,4 @@ svg_elements![
|
||||
view [],
|
||||
];
|
||||
|
||||
/// An SVG element.
|
||||
#[allow(non_snake_case)]
|
||||
pub fn r#use() -> HtmlElement<Use, (), ()>
|
||||
where {
|
||||
HtmlElement {
|
||||
tag: Use,
|
||||
attributes: (),
|
||||
children: (),
|
||||
}
|
||||
}
|
||||
|
||||
/// An SVG element.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Use;
|
||||
|
||||
impl ElementType for Use {
|
||||
type Output = web_sys::SvgElement;
|
||||
|
||||
const TAG: &'static str = "use";
|
||||
const SELF_CLOSING: bool = false;
|
||||
const ESCAPE_CHILDREN: bool = true;
|
||||
const NAMESPACE: Option<&'static str> = Some("http://www.w3.org/2000/svg");
|
||||
|
||||
#[inline(always)]
|
||||
fn tag(&self) -> &str {
|
||||
Self::TAG
|
||||
}
|
||||
}
|
||||
|
||||
impl ElementWithChildren for Use {}
|
||||
// TODO <use>
|
||||
|
||||
@@ -432,23 +432,3 @@ pub enum Position {
|
||||
/// This is the last child of its parent.
|
||||
LastChild,
|
||||
}
|
||||
|
||||
/// Declares that this type can be converted into some other type, which can be renderered.
|
||||
pub trait IntoRender {
|
||||
/// The renderable type into which this type can be converted.
|
||||
type Output;
|
||||
|
||||
/// Consumes this value, transforming it into the renderable type.
|
||||
fn into_render(self) -> Self::Output;
|
||||
}
|
||||
|
||||
impl<T> IntoRender for T
|
||||
where
|
||||
T: Render,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn into_render(self) -> Self::Output {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user