Struct sycamore_reactive::RcSignal
source · [−]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 Scope::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, Scope::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(|ctx| {
// 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 = ctx.create_memo(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 ctx.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 = ctx.create_signal(0);
assert_eq!(*state.get(), 0);
state.set(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 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 = ctx.create_signal(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 = ctx.create_signal(1);
let double = ctx.create_memo(|| *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,
ctx: ScopeRef<'a>,
f: impl FnMut(&T) -> U + 'a
) -> &'a ReadSignal<U>
pub fn map<'a, U>(
&'a self,
ctx: ScopeRef<'a>,
f: impl FnMut(&T) -> U + 'a
) -> &'a ReadSignal<U>
Creates a mapped ReadSignal
. This is equivalent to using
create_memo
.
Example
let state = ctx.create_signal(1);
let double = state.map(&ctx, |&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)
Call the ReadSignal::track
method.
impl<T: Eq> Eq for RcSignal<T>
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 T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcepub fn equivalent(&self, key: &K) -> bool
pub fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more