IncrState

Struct IncrState 

Source
pub struct IncrState { /* private fields */ }

Implementations§

Source§

impl IncrState

Source

pub fn new() -> Self

Source

pub fn new_with_height(max_height: usize) -> Self

Source

pub fn ptr_eq(&self, other: &Self) -> bool

Source

pub fn weak(&self) -> WeakState

Source

pub fn add_weak_map<S: WeakMap + 'static>(&self, weak_map: Rc<RefCell<S>>)

Source

pub fn weak_memoize_fn<I: Hash + Eq + Clone + 'static + NotObserver, T: Value>( &self, f: impl FnMut(I) -> Incr<T> + Clone, ) -> impl FnMut(I) -> Incr<T> + Clone

Source

pub fn is_stable(&self) -> bool

Returns true if there is nothing to do. In particular, this allows you to find a fixed point in a computation that sets variables during stabilisation.

Source

pub fn stabilise(&self)

Propagates changes in the graph.

let state = incremental::IncrState::new();
let var = state.var(5);
let mapped = var.map(|&x| x + 10);
let obs = mapped.observe();

// Observers can't be used until after they are held through a stabilise
assert!(obs.try_get_value().is_err());
state.stabilise();
assert_eq!(obs.value(), 15);

var.set(10);
assert_eq!(obs.value(), 15, "No change until stabilise");

state.stabilise();
assert_eq!(obs.value(), 20, "Now it changes");

This only affects nodes that are transitively being observed by an Observer. If you create some incremental nodes, changes are not propagated you call .observe() on one of them and keep that observer (i.e. don’t drop it) over a stabilise() call.

§Panics: recursive stabilisation

If you are currently in the middle of stabilising, you cannot stabilise again, and such an attempt will panic.

Examples of code that is executed during stabilisation, in which you must not call stabilise again:

  • A mapping function, e.g. incr.map(|_| { /* in here */ })
  • A subscription, i.e. observer.subscribe(|_| { /* in here */ })
  • Any call stack deep inside one of those two.
§Working with other event loops and schedulers

If you are using Incremental as state management for a React-style web app, for example yew, then consider the following pattern:

  • Event handlers, like onclick, can call stabilise.
  • Incremental subscriptions dirty components using whatever API you’re provided.
  • The scheduler, e.g. yew’s scheduler, runs actual component updates on the “next tick” of the main JavaScript VM event loop. They all do this.

The overall effect is that any further stabilises (e.g. create an observer and stabilise it when instantiating a yew component) are postponed until the next tick. The next tick is its own fresh call stack. So the previous stabilise is allowed to completely finish first.

Source

pub fn is_stabilising(&self) -> bool

Returns true if the current thread of execution is inside a stabilise call. Useful because IncrState::stabilise panics if you call stabilise recursively, so this helps avoid doing so.

Source

pub fn stabilise_debug(&self, dot_file_prefix: &str)

Stabilise, and also write out each step of the stabilisation as a GraphViz dot file.

Source

pub fn constant<T: Value>(&self, value: T) -> Incr<T>

An incremental that never changes. This is more efficient than just using a Var and not mutating it, and also clarifies intent. Typically you will use this to “lift” T into Incr<T> when you are calling code that takes an Incr<T>.

Source

pub fn fold<F, T: Value, R: Value>( &self, vec: Vec<Incr<T>>, init: R, f: F, ) -> Incr<R>
where F: FnMut(R, &T) -> R + 'static + NotObserver,

Source

pub fn var<T: Value>(&self, value: T) -> Var<T>

Source

pub fn var_current_scope<T: Value>(&self, value: T) -> Var<T>

Source

pub fn unsubscribe(&self, token: SubscriptionToken)

Source

pub fn set_max_height_allowed(&self, new_max_height: usize)

Source

pub fn within_scope<R>(&self, scope: Scope, f: impl FnOnce() -> R) -> R

Source

pub fn current_scope(&self) -> Scope

Source

pub fn save_dot_to_file(&self, named: &str)

Source

pub fn stats(&self) -> Stats

Trait Implementations§

Source§

impl Clone for IncrState

Source§

fn clone(&self) -> IncrState

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 Debug for IncrState

Source§

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

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

impl Default for IncrState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for IncrState

Implemented as pointer equality, like Rc::ptr_eq.

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.

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> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> Value for T
where T: Debug + Clone + PartialEq + NotObserver + 'static + Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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