Struct Signal

Source
pub struct Signal<T: SignalType, RT: Runtime> { /* private fields */ }
Expand description

A Signal is a reactive value or a function that produces a value, with subscribers that are automatically notified when the value changes.

When it is a function, the function automatically subscribes to all the other signals it is using and automatically re-runs when any of those signals change.

If the value implements PartialEq then the subscribers are notified only if the value changed.

A Signal is created in a reactive Scope using the signal! macro. It can only be deleted by discarding that Scope.

§Accessors

Only data signals can be manually changed. Func signals that only runs on server or client always return optional values which are some only when runninig on their side.

Value implementsData signalFunc signalFunc signal with
server or client
-.set, .update, .with.with.opt_with
Clone.cloned.cloned.opt_cloned
Copy.get.get.opt_get

§Example

use reactive_signals::{runtimes::ClientRuntime, signal};

// signals are created in scopes
let sx = ClientRuntime::new_root_scope();

// a simple data value
let count = signal!(sx, 5);

// a simple string value
let name = signal!(sx, "kiwi");

// is_plural will update when count changes
let is_plural = signal!(sx, move || count.get() != 1);

// we'll keep a history of all changes
let history = signal!(sx, Vec::<String>::new());

let text = signal!(sx, move || {
    let ending = if is_plural.get() { "s" } else { "" };
    let txt = format!("{} {}{ending}", count.get(), name.get());
    // using .update we can add the text to the vec without cloning the vec
    history.update(|hist| hist.push(txt.clone()));
    txt
});

assert_eq!(text.cloned(), "5 kiwis");

// when setting to same value the subscribers are not notified.
name.set("kiwi");
assert_eq!(history.with(|h| h.join(", ")), "5 kiwis");

// when changing the count the name and is_plural are updated automatically.
count.set(1);
assert_eq!(text.cloned(), "1 kiwi");

// you can update the name
name.update(|t| *t = "fig");
assert_eq!(text.cloned(), "1 fig");

// 1 kiwi is repated because when changing count, is_plural changes as well
// triggering a second update of the text. This will be detected in
// future versions and only notified once.
assert_eq!(
    history.with(|h| h.join(", ")),
    "5 kiwis, 1 kiwi, 1 kiwi, 1 fig"
);

with_signal_arg(count);

// when declaring functions some additional imports are necessary
use reactive_signals::{runtimes::Runtime, Signal, types::*};

fn with_signal_arg<RT: Runtime>(count: Signal<EqData<i32>, RT>) {
}

Implementations§

Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + Modifiable, RT: Runtime,

Source

pub fn set(&self, val: T::Inner)

Set the signal’s value and notifies subscribers if the value changed when it implements PartialEq otherwise it always notifies.

Source

pub fn update<R: 'static>(&self, f: impl Fn(&mut T::Inner) -> R) -> R

Applies a function to the current value to mutate it in place and returns whatever that function returns.

Subscribers are notified if the value changed when it implements PartialEq otherwise it always notifies.

Example of using the return value

let count = signal!(sc, 2);
let is_even = count.update(|val| {
    *val += 1;
    *val % 2 == 0
});
Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + Readable, T::Inner: Copy, RT: Runtime,

Source

pub fn get(&self) -> T::Inner

Get a copy of the signal value (if the value implements Copy)

Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + Readable, T::Inner: Clone, RT: Runtime,

Source

pub fn cloned(&self) -> T::Inner

Get a clone of the signal value (if the value implements Clone)

Use the .with() function if you can in order to avoid the clone.

Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + Readable, RT: Runtime,

Source

pub fn with<R: 'static>(&self, f: impl Fn(&T::Inner) -> R) -> R

Applies a function to the current value to mutate it in place and returns whatever that function returns.

Subscribers are notified if the value changed when it implements PartialEq otherwise it always notifies.

Example of using the return value

let count = signal!(sc, 2);
let is_even = count.with(|val| *val % 2 == 0);
Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + OptReadable, T::Inner: Copy + Default, RT: Runtime,

Source

pub fn opt_get(&self) -> Option<T::Inner>

Get a copy of the signal value (if the value implements Copy)

Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + OptReadable, T::Inner: Clone, RT: Runtime,

Source

pub fn opt_cloned(&self) -> Option<T::Inner>

Get a clone of the signal value (if the value implements Clone)

Use the .with() function if you can in order to avoid the clone.

Source§

impl<T, RT> Signal<T, RT>
where T: SignalType + OptReadable, RT: Runtime,

Source

pub fn opt_with<R: 'static>(&self, f: impl Fn(&T::Inner) -> R) -> Option<R>

Applies a function to the current value to mutate it in place and returns whatever that function returns.

Subscribers are notified if the value changed when it implements PartialEq otherwise it always notifies.

Example of using the return value

let count = signal!(sc, 2);
let is_even = count.with(|val| *val % 2 == 0);

Trait Implementations§

Source§

impl<T: SignalType, RT: Runtime> Clone for Signal<T, RT>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: SignalType, RT: Runtime> Copy for Signal<T, RT>

Auto Trait Implementations§

§

impl<T, RT> Freeze for Signal<T, RT>
where RT: Freeze,

§

impl<T, RT> RefUnwindSafe for Signal<T, RT>

§

impl<T, RT> Send for Signal<T, RT>
where RT: Send, T: Send,

§

impl<T, RT> Sync for Signal<T, RT>
where RT: Sync, T: Sync,

§

impl<T, RT> Unpin for Signal<T, RT>
where RT: Unpin, T: Unpin,

§

impl<T, RT> UnwindSafe for Signal<T, RT>
where RT: UnwindSafe, T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.