SnapshotObserver

Interface

Common
@ExperimentalComposeRuntimeApi
public interface SnapshotObserver

An observer for the snapshot system that notifies an observer when a snapshot is created, applied, and/or disposed.

All methods are called in the thread of the snapshot so all observers must be thread safe as they may be called from any thread.

Calling any of the Snapshot API (including, reading or writing mutable state objects) is not supported and may produce inconsistent result or throw an exception.

Functions

public fun onPreCreate(parent: Snapshot?, readonly: Boolean): SnapshotInstanceObservers?

Called before a snapshot is created allowing reads and writes to the snapshot to be observed.

This method is called in the same thread that creates the snapshot.

Parameters

parentthe parent snapshot for the new snapshot if it is a nested snapshot or null otherwise.
readonlywhether the snapshot being created will be read-only.

Returns

optional read and write observers that will be added to the snapshot created.
public fun onCreated(
        snapshot: Snapshot,
        parent: Snapshot?,
        observers: SnapshotInstanceObservers?,
    )

Called after snapshot is created.

This is called prior to the instance being returned by Snapshot.takeSnapshot or Snapshot.takeMutableSnapshot.

This method is called in the same thread that creates the snapshot.

Parameters

snapshotthe snapshot that was created.
parentthe parent snapshot for the new snapshot if it is a nested snapshot or null if it is a root snapshot.
observersthe read and write observers that were installed by the value returned by onCreated. This allows correlating which snapshot observers returned by onPreCreate to the snapshot that was created.
public fun onPreDispose(snapshot: Snapshot)

Called while a snapshot is being disposed.

This method is called in the same thread that disposes the snapshot.

Parameters

snapshotinformation about the snapshot that was created.
public fun onApplied(snapshot: Snapshot, changed: Set<Any>)

Called after a snapshot is applied.

For nested snapshots, the changes will only be visible to the parent snapshot, not globally. Snapshots do not have a parent will have changes that are visible globally and such notification are equivalent the notification sent to Snapshot.registerApplyObserver and will include all objects modified by any nested snapshots that have been applied to the parent snapshot.

This method is called in the same thread that applies the snapshot.

Parameters

snapshotthe snapshot that was applied.
changedthe set of objects that were modified during the snapshot.