natario1
02/08/2024, 2:54 PMeygraber
02/08/2024, 3:16 PMGarret Yoder
02/08/2024, 3:54 PMGarret Yoder
02/08/2024, 3:54 PMnatario1
02/08/2024, 4:26 PMsnapshotFlow { }
on them, do something when they change.
If I remember correctly, when you try to create state outside of composition you get an exception. I think compose is doing some setup work on the Snapshot
class. I have to check Molecule but since it also uses compose, it might be leveraging that instead of dealing with Snapshot
directlyZach Klippenstein (he/him) [MOD]
02/08/2024, 4:26 PMZach Klippenstein (he/him) [MOD]
02/08/2024, 4:27 PMnatario1
02/08/2024, 4:28 PMmutableStateOf()
when called outside of compositionChuck Jazdzewski [G]
02/08/2024, 4:43 PMmutableStateOf()
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.Garret Yoder
02/08/2024, 4:59 PMnatario1
02/08/2024, 5:20 PMChuck Jazdzewski [G]
02/08/2024, 6:07 PMmutableSnapshotOf()
outside the mutable snapshot that was created by composition then you would get an exception that you were reading or writing to a snapshot object outside a snapshot. The global snapshot made using state objects outside an explicit snapshot a valid thing to do.Zach Klippenstein (he/him) [MOD]
02/08/2024, 6:15 PMZach Klippenstein (he/him) [MOD]
02/08/2024, 6:15 PM