Signal

Struct Signal 

Source
pub struct Signal<T>(/* private fields */);
Expand description

A container of reactive state that can be updated and subscribed to.

§Example

Creating a Signal requires a reactive Scope. Generally, you can use the cx parameter obtained from your component or from inside a create_effect_scoped.

let signal = create_signal(cx, 123);    // A signal of type `i32`.
let signal = create_signal(cx, true);   // A signal of type `bool`.
let signal = create_signal(cx, "abc");  // A signal of type `&str`.

Implementations§

Source§

impl<T> Signal<T>

Source

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);
Source

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);
Source

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.

Source

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.

Source

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);
Source

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.

Source§

impl<T> Signal<T>
where T: Clone,

Source

pub fn modify(&self) -> Modify<'_, T>

Return a mutable handle to make it easier to mutate the inner value. This requires the inner type to implement Clone.

§Example
let state = create_signal(cx, "Hello ".to_string());
state.modify().push_str("World!");
assert_eq!(*state.get(), "Hello World!");
Source§

impl<T> Signal<T>
where T: Default,

Source

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.

Source

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>>§

Source

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);
Source

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);
Source

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);
Source

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§

Source§

impl<T> AddAssign<T> for &Signal<T>
where T: AddAssign + Copy,

Source§

fn add_assign(&mut self, other: T)

Performs the += operation. Read more
Source§

impl<'a, T> AnyReadSignal<'a> for Signal<T>

Source§

fn track(&self)

Call the ReadSignal::track method.
Source§

impl<T> Debug for Signal<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Signal<T>

Source§

type Target = ReadSignal<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Signal<T> as Deref>::Target

Dereferences the value.
Source§

impl<T> Display for Signal<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> DivAssign<T> for &Signal<T>
where T: DivAssign + Copy,

Source§

fn div_assign(&mut self, other: T)

Performs the /= operation. Read more
Source§

impl<T> Hash for Signal<T>
where T: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> MulAssign<T> for &Signal<T>
where T: MulAssign + Copy,

Source§

fn mul_assign(&mut self, other: T)

Performs the *= operation. Read more
Source§

impl<T> PartialEq for Signal<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Signal<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> SubAssign<T> for &Signal<T>
where T: SubAssign + Copy,

Source§

fn sub_assign(&mut self, other: T)

Performs the -= operation. Read more
Source§

impl<T> Eq for Signal<T>
where T: Eq,

Auto Trait Implementations§

§

impl<T> !Freeze for Signal<T>

§

impl<T> !RefUnwindSafe for Signal<T>

§

impl<T> !Send for Signal<T>

§

impl<T> !Sync for Signal<T>

§

impl<T> Unpin for Signal<T>

§

impl<T> !UnwindSafe for Signal<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, G> IntoView<G> for T
where T: Display + 'static, G: GenericNode,

Source§

fn create(&self) -> View<G>

Called during the initial render when creating the DOM nodes. Should return a View.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.