diff --git a/packages/signals/src/map.rs b/packages/signals/src/map.rs index 9e1ec7ce5..1442309e1 100644 --- a/packages/signals/src/map.rs +++ b/packages/signals/src/map.rs @@ -1,3 +1,4 @@ +use crate::CopyValue; use crate::Signal; use dioxus_core::ScopeId; use std::cell::Ref; @@ -5,26 +6,28 @@ use std::fmt::Debug; use std::fmt::Display; /// A read only signal that has been mapped to a new type. -pub struct SignalMap { - mapping: Box Ref<'static, U>>, +pub struct SignalMap { + origin_scope: ScopeId, + mapping: CopyValue Ref<'static, U>>>, } -impl SignalMap { +impl SignalMap { /// Create a new mapped signal. - pub fn new(signal: Signal, mapping: fn(&T) -> &U) -> Self { + pub fn new(signal: Signal, mapping: fn(&T) -> &U) -> Self { Self { - mapping: Box::new(move || Ref::map(signal.read(), |v| (mapping)(v))), + origin_scope: signal.origin_scope(), + mapping: CopyValue::new(Box::new(move || Ref::map(signal.read(), |v| (mapping)(v)))), } } /// Get the scope that the signal was created in. pub fn origin_scope(&self) -> ScopeId { - self.inner.origin_scope() + self.origin_scope } /// Get the current value of the signal. This will subscribe the current scope to the signal. - pub fn read(&self) -> Ref { - (self.mapping)() + pub fn read(&self) -> Ref<'static, U> { + (self.mapping.read())() } /// Run a closure with a reference to the signal's value. @@ -42,7 +45,7 @@ impl SignalMap { impl PartialEq for SignalMap { fn eq(&self, other: &Self) -> bool { - self.inner == other.inner + self.mapping == other.mapping } } @@ -77,7 +80,7 @@ impl SignalMap> { /// Unwraps the inner value and clones it. pub fn unwrap(&self) -> U where - T: Clone, + U: Clone, { self.with(|v| v.clone()).unwrap() } diff --git a/packages/signals/src/signal.rs b/packages/signals/src/signal.rs index 921aa356e..ad025c839 100644 --- a/packages/signals/src/signal.rs +++ b/packages/signals/src/signal.rs @@ -171,7 +171,7 @@ impl Signal { /// Get the current value of the signal. This will subscribe the current scope to the signal. /// If the signal has been dropped, this will panic. - pub fn read(&self) -> Ref { + pub fn read(&self) -> Ref<'static, T> { let inner = self.inner.read(); if let Some(effect) = Effect::current() { let mut effect_subscribers = inner.effect_subscribers.borrow_mut(); @@ -200,7 +200,7 @@ impl Signal { /// Get a mutable reference to the signal's value. /// If the signal has been dropped, this will panic. - pub fn write(&self) -> Write<'_, T> { + pub fn write(&self) -> Write<'static, T> { let inner = self.inner.write(); let borrow = RefMut::map(inner, |v| &mut v.value); Write { @@ -257,7 +257,7 @@ impl Signal { } /// Map the signal to a new type. - pub fn map(self, f: fn(&T) -> &O) -> SignalMap { + pub fn map(self, f: fn(&T) -> &O) -> SignalMap { SignalMap::new(self, f) } }