pub struct IncrState { /* private fields */ }Implementations§
Source§impl IncrState
impl IncrState
pub fn new() -> Self
pub fn new_with_height(max_height: usize) -> Self
pub fn ptr_eq(&self, other: &Self) -> bool
pub fn weak(&self) -> WeakState
pub fn add_weak_map<S: WeakMap + 'static>(&self, weak_map: Rc<RefCell<S>>)
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
Sourcepub fn is_stable(&self) -> bool
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.
Sourcepub fn stabilise(&self)
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.
Sourcepub fn is_stabilising(&self) -> bool
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.
Sourcepub fn stabilise_debug(&self, dot_file_prefix: &str)
pub fn stabilise_debug(&self, dot_file_prefix: &str)
Stabilise, and also write out each step of the stabilisation as a GraphViz dot file.
Sourcepub fn constant<T: Value>(&self, value: T) -> Incr<T>
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>.
pub fn fold<F, T: Value, R: Value>( &self, vec: Vec<Incr<T>>, init: R, f: F, ) -> Incr<R>
pub fn var<T: Value>(&self, value: T) -> Var<T>
pub fn var_current_scope<T: Value>(&self, value: T) -> Var<T>
pub fn unsubscribe(&self, token: SubscriptionToken)
pub fn set_max_height_allowed(&self, new_max_height: usize)
pub fn within_scope<R>(&self, scope: Scope, f: impl FnOnce() -> R) -> R
pub fn current_scope(&self) -> Scope
pub fn save_dot_to_file(&self, named: &str)
pub fn stats(&self) -> Stats
Trait Implementations§
Source§impl PartialEq for IncrState
Implemented as pointer equality, like Rc::ptr_eq.
impl PartialEq for IncrState
Implemented as pointer equality, like Rc::ptr_eq.