SnapshotStateObserver


Helper class to efficiently observe snapshot state reads. See observeReads for more details.

NOTE: This class is not thread-safe, so implementations should not reuse observer between different threads to avoid race conditions.

Summary

Public constructors

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

Public functions

Unit

Remove all observations.

Cmn
Unit
clear(scope: Any)

Clears all state read observations for a given scope.

Cmn
Unit
clearIf(predicate: (scope: Any) -> Boolean)

Remove observations using predicate to identify scopes to be removed.

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

This method is only used for testing.

Cmn
Unit
<T : Any> observeReads(
    scope: T,
    onValueChangedForScope: (T) -> Unit,
    block: () -> Unit
)

Executes block, observing state object reads during its execution.

Cmn
Unit

Starts watching for state commits.

Cmn
Unit

Stops watching for state commits.

Cmn
Unit
withNoObservations(block: () -> Unit)

This function is deprecated. Replace with Snapshot.withoutReadObservation()

Cmn

Public constructors

SnapshotStateObserver

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

Public functions

clear

fun clear(): Unit

Remove all observations.

clear

fun clear(scope: Any): Unit

Clears all state read observations for a given scope. This clears values for all onValueChangedForScope callbacks passed in observeReads.

clearIf

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

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

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(
    scope: T,
    onValueChangedForScope: (T) -> Unit,
    block: () -> Unit
): Unit

Executes block, observing state object reads during its execution.

The scope and onValueChangedForScope are associated with any values that are read so that when those values change, onValueChangedForScope will be called with the scope parameter.

Observation can be paused with Snapshot.withoutReadObservation.

Parameters
scope: T

value associated with the observed scope.

onValueChangedForScope: (T) -> Unit

is called with the scope when value read within block has been changed. For repeated observations, it is more performant to pass the same instance of the callback, as observedScopeMaps grows with each new callback instance.

block: () -> Unit

to observe reads within.

start

fun start(): Unit

Starts watching for state commits.

stop

fun stop(): Unit

Stops watching for state commits.

withNoObservations

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

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