Google is committed to advancing racial equity for Black communities. See how.

SnapshotStateObserver

class SnapshotStateObserver
kotlin.Any
   ↳ androidx.compose.runtime.snapshots.SnapshotStateObserver

Summary

Public constructors
<init>(onChangedExecutor: (callback: () -> Unit) -> Unit)

Public methods
Unit
clear(target: Any)

Clears all model read observations for a given target.

Unit

Remove all observations.

Unit

Remove all hooks used to track changes.

Unit

Starts or stops watching for model commits based on enabled.

Unit
notifyChanges(changes: Set<Any>, snapshot: Snapshot)

This method is only used for testing.

Unit
observeReads(target: T, onChanged: (T) -> Unit, block: () -> Unit)

Executes block, observing state object reads during its execution.

Unit
pauseObservingReads(block: () -> Unit)

Stops observing state object reads while executing block.

Unit
removeObservationsFor(predicate: (scope: Any) -> Boolean)

Remove observations using predicate to identify target scopes to be removed.

Public constructors

<init>

SnapshotStateObserver(onChangedExecutor: (callback: () -> Unit) -> Unit)

Public methods

clear

fun clear(target: Any): Unit

Clears all model read observations for a given target. This clears values for all onCommit methods passed in observeReads.

clear

fun clear(): Unit

Remove all observations.

dispose

fun dispose(): Unit

Remove all hooks used to track changes.

enableStateUpdatesObserving

fun enableStateUpdatesObserving(enabled: Boolean): Unit

Starts or stops watching for model commits based on enabled.

notifyChanges

@TestOnly fun notifyChanges(
    changes: Set<Any>,
    snapshot: Snapshot
): Unit

This method is only used for testing. It notifies that changes have been made on snapshot.

observeReads

fun <T : Any> observeReads(
    target: T,
    onChanged: (T) -> Unit,
    block: () -> Unit
): Unit

Executes block, observing state object reads during its execution.

The target and onChanged are associated with any values that are read so that when those values change, onChanged can be called with the target parameter.

Observation for target will be paused when a new observeReads call is made or when pauseObservingReads is called.

Any previous observation with the given target and onChanged will be cleared when the onChanged is called for target. The onChanged should trigger a new observeReads call to resubscribe to changes. They may also be cleared using removeObservationsFor or clear.

pauseObservingReads

fun pauseObservingReads(block: () -> Unit): Unit

Stops observing state object reads while executing block. State object reads may be restarted by calling observeReads inside block.

removeObservationsFor

fun removeObservationsFor(predicate: (scope: Any) -> Boolean): Unit

Remove observations using predicate to identify target scopes to be removed. This is used when a scope is no longer in the hierarchy and should not receive any callbacks.