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>
impl<T: Clone + 'static> Computed<T>
Sourcepub fn from<F: Fn(&Context) -> T + 'static>(get_value: F) -> Computed<T>
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.
Sourcepub fn get(&self, context: &Context) -> T
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.
Sourcepub fn map<K: Clone + 'static, F: 'static + Fn(T) -> K>(
&self,
fun: F,
) -> Computed<K>
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.
pub fn id(&self) -> GraphId
Sourcepub fn subscribe_all<R: 'static, F: Fn(T) -> R + 'static>(
self,
callback: F,
) -> DropResource
pub fn subscribe_all<R: 'static, F: Fn(T) -> R + 'static>( self, callback: F, ) -> DropResource
Source§impl<T: Clone + PartialEq + 'static> Computed<T>
impl<T: Clone + PartialEq + 'static> Computed<T>
Sourcepub fn subscribe<R: 'static, F: Fn(T) -> R + 'static>(
self,
callback: F,
) -> DropResource
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.
Sourcepub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode
pub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode
Render value inside this Computed. See Value::render_value() for examples.
Sourcepub fn render_value_option(
&self,
render: impl Fn(T) -> Option<DomNode> + 'static,
) -> DomNode
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.