Skip to main content

SnapshotStateObserver

Struct SnapshotStateObserver 

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

Observer that records state object reads performed inside a given scope and notifies the caller when any of the observed objects change.

This is a pragmatic Rust translation of Jetpack Compose’s SnapshotStateObserver. The implementation focuses on the core behaviour needed by the Cranpose runtime:

  • Tracking state object reads per logical scope.
  • Reacting to snapshot apply notifications.
  • Scheduling invalidation callbacks via the supplied executor.

Advanced features from the Kotlin version (derived state tracking, change coalescing, queue minimisation) are deferred

Implementations§

Source§

impl SnapshotStateObserver

Source

pub fn new( on_changed_executor: impl Fn(Box<dyn FnOnce() + 'static>) + 'static, ) -> Self

Create a new observer that schedules callbacks using on_changed_executor.

Source

pub fn observe_reads<T, R>( &self, scope: T, on_value_changed_for_scope: impl Fn(&T) + 'static, block: impl FnOnce() -> R, ) -> R
where T: Any + Clone + PartialEq + 'static,

Observe state object reads performed while executing block.

Subsequent calls to observe_reads replace any previously recorded observations for the provided scope. When one of the observed objects mutates, on_value_changed_for_scope will be invoked on the executor.

Source

pub fn begin_frame(&self)

Notify the observer that a new composition frame is starting.

Source

pub fn with_no_observations<R>(&self, block: impl FnOnce() -> R) -> R

Temporarily pause read observation while executing block.

Source

pub fn clear<T>(&self, scope: &T)
where T: Any + PartialEq + 'static,

Remove any recorded reads for scope.

Source

pub fn clear_if(&self, predicate: impl Fn(&dyn Any) -> bool)

Remove recorded reads for scopes that satisfy predicate.

Source

pub fn clear_all(&self)

Remove all recorded observations.

Source

pub fn start(&self)

Begin listening for snapshot apply notifications.

Source

pub fn stop(&self)

Stop listening for snapshot apply notifications.

Trait Implementations§

Source§

impl Clone for SnapshotStateObserver

Source§

fn clone(&self) -> SnapshotStateObserver

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

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