Composition is not required to use the snapshot system and snapshots are independent of composition. Composition knows about snapshots but not the other way around.
If you run into a crash using
mutableStateOf()
outside of composition please submit an example in a bug report.
As for an example of observation you can either use
SnapshotStateObserver
or
snapshotFlow()
or use
Snapshot.registerApplyObserver()
directly which is what these use internally.
In order to see changes made in the global snapshot you need something like
GlobalSnapshotManager
which is internal to the
UI
library but it uses public API, specifically
Snapshot.sendApplyNotifications()
and
Snapshot.registerGlobalWriteObserver()
. The
GlobalSnapshotManager
schedules a call to
Snapshot.sendApplyNotifications()
whenever a write is performed in the global snapshot to ensure that the change is eventually received by the snapshot apply observer. Using snapshots and observation outside of compose requires something similar as the global snapshot needs to be "applied" periodically to be seen by an apply observer.
As Zach points out, the snapshot system does not rely on the compose compiler plugin and can be used without it.