Skip to main content

euv_ui/hook/previous/
fn.rs

1use super::*;
2
3/// Obtains the previous-value tracker registered against the current
4/// hook context slot.
5///
6/// Behaves like `HookContext::use_hook`: the same `Previous` is
7/// returned on every render at the same hook index, so the captured
8/// `previous` signal survives across renders without losing state.
9///
10/// # Returns
11///
12/// - `Previous<T>` - The previous-value tracker handle.
13///   Returns the factory result directly when no hook context is
14///   active (e.g. when called outside a render cycle).
15pub fn use_previous<T>() -> Previous<T>
16where
17    T: Clone + PartialEq + Debug + 'static,
18{
19    HookContext::use_hook(Previous::<T>::new)
20}
21
22/// Records `current` against the supplied tracker and returns the
23/// snapshot of what was previously recorded.
24///
25/// Convenience wrapper used by component-level consumers that want
26/// the "compute previous" + "record new current" steps glued together.
27/// Returns `None` on the first call (no prior value exists yet).
28///
29/// # Arguments
30///
31/// - `Previous<T>` - The tracker obtained from `use_previous()`.
32/// - `T` - The current value to record.
33///
34/// # Returns
35///
36/// - `Option<T>` - The value that was recorded on the previous call,
37///   or `None` if no prior value exists.
38pub fn previous_step<T>(previous: Previous<T>, current: T) -> Option<T>
39where
40    T: Clone + PartialEq + Debug + 'static,
41{
42    let snapshot: Option<T> = previous.get_previous_snapshot();
43    previous.record(current);
44    snapshot
45}