androidx.compose.runtime.snapshots

Interfaces

ObserverHandle

The type returned by observer registration methods that unregisters the observer when it is disposed.

Cmn
SnapshotContextElement

A CoroutineContext element that enters an associated snapshot whenever a coroutine associated with this context is resumed.

Cmn
SnapshotMutableState

A mutable value holder where reads to the value property during the execution of a Composable function, the current RecomposeScope will be subscribed to changes of that value.

Cmn
StateObject

Interface implemented by all snapshot aware state objects.

Cmn

Classes

MutableSnapshot

A snapshot of the values return by mutable states and other state objects.

Cmn
Snapshot

A snapshot of the values return by mutable states and other state objects.

Cmn
SnapshotApplyResult

The result of a applying a mutable snapshot.

Cmn
SnapshotApplyResult.Failure
Cmn
SnapshotStateList

An implementation of MutableList that can be observed and snapshot.

Cmn
SnapshotStateMap

An implementation of MutableMap that can be observed and snapshot.

Cmn
SnapshotStateObserver

Helper class to efficiently observe snapshot state reads.

Cmn
StateRecord

Snapshot local value of a state object.

Cmn

Exceptions

SnapshotApplyConflictException

An exception that is thrown when SnapshotApplyResult.check is called on a result of a MutableSnapshot.apply that fails to apply.

Cmn

Objects

Annotations

AutoboxingStateValueProperty

This annotation designates that a property on a State class will autobox when it is read from or assigned to.

Cmn
StateFactoryMarker

Designates a function as a factory function that produces a State object.

Cmn

Extension functions summary

SnapshotContextElement

Return a SnapshotContextElement that will enter this Snapshot whenever the associated coroutine is resumed and leave this snapshot when it suspends.

Cmn
T
<T : StateRecord> T.readable(state: StateObject)

Return the current readable state record for the current snapshot.

Cmn
T
<T : StateRecord> T.readable(state: StateObject, snapshot: Snapshot)

Return the current readable state record for the snapshot.

Cmn
inline R
<T : StateRecord, R : Any?> T.withCurrent(block: (r) -> R)

Provides a block with the current record, without notifying any read observers.

Cmn
inline R
<T : StateRecord, R : Any?> T.writable(state: StateObject, block: T.() -> R)

Call block with a writable state record for the given record.

Cmn
inline R
<T : StateRecord, R : Any?> T.writable(
    state: StateObject,
    snapshot: Snapshot,
    block: T.() -> R
)

Call block with a writable state record for snapshot of the given record.

Cmn

Extension functions

asContextElement

@ExperimentalComposeApi
fun Snapshot.asContextElement(): SnapshotContextElement

Return a SnapshotContextElement that will enter this Snapshot whenever the associated coroutine is resumed and leave this snapshot when it suspends. The snapshot still must be disposed separately when it will no longer be used.

import androidx.compose.runtime.snapshots.asContextElement

runBlocking {
    val snapshot = Snapshot.takeSnapshot()
    try {
        withContext(snapshot.asContextElement()) {
            // Data observed by separately reading stateA and stateB are consistent with
            // the snapshot context element across suspensions
            doSomethingSuspending(someObject.stateA)
            doSomethingSuspending(someObject.stateB)
        }
    } finally {
        // Snapshot must be disposed after it will not be used again
        snapshot.dispose()
    }
}

readable

fun <T : StateRecord> T.readable(state: StateObject): T

Return the current readable state record for the current snapshot. It is assumed that this is the first record of state

readable

fun <T : StateRecord> T.readable(state: StateObject, snapshot: Snapshot): T

Return the current readable state record for the snapshot. It is assumed that this is the first record of state

withCurrent

inline fun <T : StateRecord, R : Any?> T.withCurrent(block: (r) -> R): R

Provides a block with the current record, without notifying any read observers.

See also
readable

writable

inline fun <T : StateRecord, R : Any?> T.writable(state: StateObject, block: T.() -> R): R

Call block with a writable state record for the given record. It is assumed that this is called for the first state record in a state object. A record is writable if it was created in the current mutable snapshot.

writable

inline fun <T : StateRecord, R : Any?> T.writable(
    state: StateObject,
    snapshot: Snapshot,
    block: T.() -> R
): R

Call block with a writable state record for snapshot of the given record. It is assumed that this is called for the first state record in a state object. If the snapshot is read-only calling this will throw.