Skip to main content

Observer

Struct Observer 

Source
pub struct Observer { /* private fields */ }
Expand description

Runs a closure while recording which reactive values it reads, and calls on_invalidate (once per run) when any of them changes.

Implementations§

Source§

impl Observer

Source

pub fn new(on_invalidate: impl Fn() + 'static) -> Self

Creates a dirty observer that calls on_invalidate when a tracked dependency changes.

use std::cell::Cell;
use std::rc::Rc;
use ravenrs_reactive::{Observer, State};

let name = State::new("Ada".to_string());
let hits = Rc::new(Cell::new(0));
let h = hits.clone();
let observer = Observer::new(move || h.set(h.get() + 1));
observer.track(|| name.get());
name.set("Grace".to_string());
assert_eq!(hits.get(), 1);
assert!(observer.is_dirty());
Source

pub fn track<R>(&self, f: impl FnOnce() -> R) -> R

Runs f, replacing the previous dependency set with what f reads.

Source

pub fn untracked<R>(f: impl FnOnce() -> R) -> R

Runs f without recording dependencies for any observer.

Source

pub fn is_dirty(&self) -> bool

True until the first track and after any dependency changed.

Source

pub fn invalidate(&self)

Marks the observer dirty and fires its callback, as if a dependency changed.

Trait Implementations§

Source§

impl Debug for Observer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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, 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.