pub struct RcSignal<T>(_);
Expand description
A signal that is not bound to a Scope
.
Sometimes, it is useful to have a signal that can escape the enclosing reactive scope.
However, this cannot be achieved simply with create_signal
because the resulting
Signal
is tied to the Scope
by it’s lifetime. The Signal
can only live as long as
the Scope
.
With RcSignal
on the other hand, the lifetime is not tied to a Scope
. Memory is managed
using a reference-counted smart pointer (Rc
). What this means is that RcSignal
cannot
implement the Copy
trait and therefore needs to be manually cloned into all closures where
it is used.
In general, create_signal
should be preferred, both for performance and ergonomics.
Usage
To create a RcSignal
, use the create_rc_signal
function.
Example
let mut outer = None;
create_scope_immediate(|cx| {
// Even though the RcSignal is created inside a reactive scope, it can escape out of it.
let rc_state = create_rc_signal(0);
let rc_state_cloned = rc_state.clone();
let double = create_memo(cx, move || *rc_state_cloned.get() * 2);
assert_eq!(*double.get(), 0);
rc_state.set(1);
assert_eq!(*double.get(), 2);
// This isn't possible with simply create_signal(_)
outer = Some(rc_state);
});
Methods from Deref<Target = Signal<T>>
sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Set the current value of the state.
This will notify and update any effects and memos that depend on this value.
Example
let state = create_signal(cx, 0);
assert_eq!(*state.get(), 0);
state.set(1);
assert_eq!(*state.get(), 1);
sourcepub fn set_rc(&self, value: Rc<T>)
pub fn set_rc(&self, value: Rc<T>)
Set the current value of the state wrapped in a Rc
. Unlike Signal::set()
, this
method accepts the value wrapped in a Rc
because the underlying storage is already using
Rc
, thus preventing an unnecessary clone.
This will notify and update any effects and memos that depend on this value.
Example
let state = create_signal(cx, 0);
assert_eq!(*state.get(), 0);
state.set_rc(Rc::new(1));
assert_eq!(*state.get(), 1);
sourcepub fn set_silent(&self, value: T)
pub fn set_silent(&self, value: T)
Set the current value of the state without triggering subscribers.
Make sure you know what you are doing because this can make state inconsistent.
sourcepub fn set_rc_silent(&self, value: Rc<T>)
pub fn set_rc_silent(&self, value: Rc<T>)
Set the current value of the state wrapped in a Rc
without triggering subscribers.
See the documentation for Signal::set_rc()
for more information.
Make sure you know what you are doing because this can make state inconsistent.
sourcepub fn split(&self) -> (impl Fn() + Copy, impl Fn(T) + Copy)
pub fn split(&self) -> (impl Fn() + Copy, impl Fn(T) + Copy)
Split a signal into getter and setter handles.
Example
let (state, set_state) = create_signal(cx, 0).split();
assert_eq!(*state(), 0);
set_state(1);
assert_eq!(*state(), 1);
sourcepub fn trigger_subscribers(&self)
pub fn trigger_subscribers(&self)
Calls all the subscribers without modifying the state.
This can be useful when using patterns such as inner mutability where the state updated will
not be automatically triggered. In the general case, however, it is preferable to use
Signal::set()
instead.
This will also re-compute all the subscribers of this signal by calling all the dependency callbacks.
sourcepub fn take(&self) -> Rc<T>
pub fn take(&self) -> Rc<T>
Take the current value out and replace it with the default value.
This will notify and update any effects and memos that depend on this value.
sourcepub fn take_silent(&self) -> Rc<T>
pub fn take_silent(&self) -> Rc<T>
Take the current value out and replace it with the default value without triggering subscribers.
Make sure you know what you are doing because this can make state inconsistent.
Methods from Deref<Target = ReadSignal<T>>
sourcepub fn get(&self) -> Rc<T>
pub fn get(&self) -> Rc<T>
Get the current value of the state. When called inside a reactive scope, calling this will add itself to the scope’s dependencies.
Example
let state = create_signal(cx, 0);
assert_eq!(*state.get(), 0);
state.set(1);
assert_eq!(*state.get(), 1);
sourcepub fn get_untracked(&self) -> Rc<T>
pub fn get_untracked(&self) -> Rc<T>
Get the current value of the state, without tracking this as a dependency if inside a reactive context.
Example
let state = create_signal(cx, 1);
let double = create_memo(cx, || *state.get_untracked() * 2);
assert_eq!(*double.get(), 2);
state.set(2);
// double value should still be old value because state was untracked
assert_eq!(*double.get(), 2);
sourcepub fn map<'a, U>(
&'a self,
cx: BoundedScope<'a, 'a>,
f: impl FnMut(&T) -> U + 'a
) -> &'a ReadSignal<U>
pub fn map<'a, U>(
&'a self,
cx: BoundedScope<'a, 'a>,
f: impl FnMut(&T) -> U + 'a
) -> &'a ReadSignal<U>
Creates a mapped ReadSignal
. This is equivalent to using
create_memo
.
Example
let state = create_signal(cx, 1);
let double = state.map(cx, |&x| x * 2);
assert_eq!(*double.get(), 2);
state.set(2);
assert_eq!(*double.get(), 4);
sourcepub fn track(&self)
pub fn track(&self)
When called inside a reactive scope, calling this will add itself to the scope’s dependencies.
To both track and get the value of the signal, use ReadSignal::get
instead.
Trait Implementations
sourceimpl<'a, T> AnyReadSignal<'a> for RcSignal<T>
impl<'a, T> AnyReadSignal<'a> for RcSignal<T>
sourcefn track(&self)
fn track(&self)
ReadSignal::track
method.sourceimpl<'de, T> Deserialize<'de> for RcSignal<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for RcSignal<T>where
T: Deserialize<'de>,
sourcefn deserialize<D>(
deserializer: D
) -> Result<RcSignal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<RcSignal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceimpl<T> PartialEq<RcSignal<T>> for RcSignal<T>where
T: PartialEq<T>,
impl<T> PartialEq<RcSignal<T>> for RcSignal<T>where
T: PartialEq<T>,
sourceimpl<T> Serialize for RcSignal<T>where
T: Serialize,
impl<T> Serialize for RcSignal<T>where
T: Serialize,
sourcefn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Eq for RcSignal<T>where
T: Eq,
Auto Trait Implementations
impl<T> !RefUnwindSafe for RcSignal<T>
impl<T> !Send for RcSignal<T>
impl<T> !Sync for RcSignal<T>
impl<T> Unpin for RcSignal<T>
impl<T> !UnwindSafe for RcSignal<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> CallHasher for Twhere
T: Hash + ?Sized,
impl<T> CallHasher for Twhere
T: Hash + ?Sized,
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.