Macro avalanche::tracked[][src]

macro_rules! tracked {
    ($e : expr) => { ... };
}
Expand description

Unwraps and propogates a Tracked value.

By default, tracked takes ownership of its input. Passing a reference instead will return a reference to the input’s inner value.

Within a #[component] body, wraps the expression containing it in a Tracked instance maintaining whether any of the tracked!() values were updated. Closure values are wrapped in Tracked if they contain tracked or updated calls with external identifiers as inputs.

Outside of #[component], provides access to the tracked value without rewrapping the containing expression.

Example

#[component]
fn ComponentBody() -> View {
    let a = Tracked::new(8, true);
    let b: Tracked<u32> = tracked!(a);
 
    // c has type Tracked<u32>
    let c = tracked!(a) + tracked!(b);
    assert_eq!(tracked!(c), 16);
    assert!(updated!(c));
 
    // Tracked closure behavior
    let d = Tracked::new(2, false);
    let closure = || {
        let a = Tracked::new(2, true);
        tracked!(a) + tracked!(d)
    };
    // closure depends on external identifier d,
    // so it is wrapped in Tracked, but since d is not updated, 
    // neither is closure.
    assert!(!updated!(closure));
 
    // .. 
}