Computed

Struct Computed 

Source
pub struct Computed<T: Clone> { /* private fields */ }
Expand description

A reactive value that is read-only and computed by dependency graph.

§Computed directly from Value

use vertigo::{Value, transaction};

let value = Value::new(5);

let comp = value.to_computed();

transaction(|context| {
    assert_eq!(comp.get(context), 5);
});

// Can't do that
// comp.set(10);

§Computed from Value by provided function

use vertigo::{Computed, Value, transaction};

let value = Value::new(2);

let comp_2 = {
    let v = value.clone();
    Computed::from(move |context| v.get(context) * 2)
};

transaction(|context| {
    assert_eq!(comp_2.get(context), 4);
});

value.set(6);

transaction(|context| {
    assert_eq!(comp_2.get(context), 12);
});

Implementations§

Source§

impl<T: Clone + 'static> Computed<T>

Source

pub fn from<F: Fn(&Context) -> T + 'static>(get_value: F) -> Computed<T>

Creates new Computed<T> which state is determined by provided generator function.

Source

pub fn get(&self, context: &Context) -> T

Get current value, it will be computed on-the-fly if the previous one ran out of date.

Source

pub fn map<K: Clone + 'static, F: 'static + Fn(T) -> K>( &self, fun: F, ) -> Computed<K>

Reactively convert Computed<T> to Computed<K> with provided transformation function applied.

Source

pub fn id(&self) -> GraphId

Source

pub fn subscribe_all<R: 'static, F: Fn(T) -> R + 'static>( self, callback: F, ) -> DropResource

Do something every time the value inside Computed is triggered.

Note that the callback is fired every time the value in Computed is computed, even if the outcome value is not changed.

Source§

impl<T: Clone + PartialEq + 'static> Computed<T>

Source

pub fn subscribe<R: 'static, F: Fn(T) -> R + 'static>( self, callback: F, ) -> DropResource

Do something every time the value inside Computed is changed.

Note that the callback is fired only if the value really changes. This means that even if computation takes place with different source value, but the resulting value is the same as old one, the callback is not fired.

Source

pub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode

Render value inside this Computed. See Value::render_value() for examples.

Source

pub fn render_value_option( &self, render: impl Fn(T) -> Option<DomNode> + 'static, ) -> DomNode

Render optional value inside this Computed. See Value::render_value_option() for examples.

Source§

impl<T: Clone + PartialEq + 'static, L: IntoIterator<Item = T> + Clone + 'static> Computed<L>

Source

pub fn render_list<K: Eq + Hash>( &self, get_key: impl Fn(&T) -> K + 'static, render: impl Fn(&T) -> DomNode + 'static, ) -> DomNode

Render iterable value inside this Computed. See Value::render_list() for examples.

Trait Implementations§

Source§

impl<T: Clone + 'static> Clone for Computed<T>

Source§

fn clone(&self) -> Self

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

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

Performs copy-assignment from source. Read more
Source§

impl<T: ToString + Clone + PartialEq + 'static> EmbedDom for &Computed<T>

Source§

impl<T: ToString + Clone + PartialEq + 'static> EmbedDom for Computed<T>

Source§

impl From<&Computed<Option<String>>> for AttrValue

Source§

fn from(v: &Computed<Option<String>>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<String>> for AttrValue

Source§

fn from(v: &Computed<String>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<bool>> for AttrValue

Source§

fn from(v: &Computed<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<char>> for AttrValue

Source§

fn from(v: &Computed<char>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<f32>> for AttrValue

Source§

fn from(v: &Computed<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<f64>> for AttrValue

Source§

fn from(v: &Computed<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<i16>> for AttrValue

Source§

fn from(v: &Computed<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<i32>> for AttrValue

Source§

fn from(v: &Computed<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<i64>> for AttrValue

Source§

fn from(v: &Computed<i64>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<i8>> for AttrValue

Source§

fn from(v: &Computed<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<isize>> for AttrValue

Source§

fn from(v: &Computed<isize>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<u16>> for AttrValue

Source§

fn from(v: &Computed<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<u32>> for AttrValue

Source§

fn from(v: &Computed<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<u64>> for AttrValue

Source§

fn from(v: &Computed<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<u8>> for AttrValue

Source§

fn from(v: &Computed<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<&Computed<usize>> for AttrValue

Source§

fn from(v: &Computed<usize>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone + 'static> From<&T> for Computed<T>

Source§

fn from(value: &T) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Computed<String>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<Css>> for CssAttrValue

Source§

fn from(value: Computed<Css>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<Option<String>>> for AttrValue

Source§

fn from(v: Computed<Option<String>>) -> Self

Converts to this type from the input type.
Source§

impl<R> From<Computed<Rc<dyn Fn() -> R>>> for Callback<R>

Source§

fn from(value: Computed<Rc<dyn Fn() -> R + 'static>>) -> Self

Converts to this type from the input type.
Source§

impl<T, R> From<Computed<Rc<dyn Fn(T) -> R>>> for Callback1<T, R>

Source§

fn from(value: Computed<Rc<dyn Fn(T) -> R + 'static>>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<String>> for AttrValue

Source§

fn from(v: Computed<String>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<bool>> for AttrValue

Source§

fn from(v: Computed<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<char>> for AttrValue

Source§

fn from(v: Computed<char>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<f32>> for AttrValue

Source§

fn from(v: Computed<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<f64>> for AttrValue

Source§

fn from(v: Computed<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<i16>> for AttrValue

Source§

fn from(v: Computed<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<i32>> for AttrValue

Source§

fn from(v: Computed<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<i64>> for AttrValue

Source§

fn from(v: Computed<i64>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<i8>> for AttrValue

Source§

fn from(v: Computed<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<isize>> for AttrValue

Source§

fn from(v: Computed<isize>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<u16>> for AttrValue

Source§

fn from(v: Computed<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<u32>> for AttrValue

Source§

fn from(v: Computed<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<u64>> for AttrValue

Source§

fn from(v: Computed<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<u8>> for AttrValue

Source§

fn from(v: Computed<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<Computed<usize>> for AttrValue

Source§

fn from(v: Computed<usize>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone + 'static> From<T> for Computed<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone + 'static> From<Value<T>> for Computed<T>

Source§

fn from(val: Value<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone + 'static> PartialEq for Computed<T>

Source§

fn eq(&self, other: &Self) -> 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: Clone + 'static> ToComputed<Resource<Rc<T>>> for Computed<Resource<T>>

Source§

impl<T: Clone + 'static> ToComputed<T> for &Computed<T>

Source§

impl<T: Clone + 'static> ToComputed<T> for Computed<T>

Auto Trait Implementations§

§

impl<T> Freeze for Computed<T>

§

impl<T> !RefUnwindSafe for Computed<T>

§

impl<T> !Send for Computed<T>

§

impl<T> !Sync for Computed<T>

§

impl<T> Unpin for Computed<T>

§

impl<T> !UnwindSafe for Computed<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> 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.