Filip Wiesner
01/26/2022, 7:50 PMFilip Wiesner
01/26/2022, 7:50 PMval writeObserver: (Any) -> Unit = { writtenState ->
val s = writtenState as MutableState<String>
println("State was changed to ${s.value}")
}
var state by mutableStateOf("")
val snap = Snapshot.takeMutableSnapshot(writeObserver = writeObserver)
snap.enter {
state = "A"
Snapshot.withMutableSnapshot {
state = "B"
state = "C"
}
}
snap.apply()
snap.dispose()
//The Result is:
// State was changed to A
// State was changed to B
// State was changed to C
Filip Wiesner
01/26/2022, 7:54 PMtakeNestedMutableSnapshot
which is called by takeMutableSnapshot
) clearly states that this shouldn't be the case:
Take a mutable snapshot of the state values in this snapshot. Entering this snapshot by calling enter allows state objects to be modified that are not visible to the this, the parent snapshot, until the apply is called.What am I missing?
Filip Wiesner
01/27/2022, 8:03 AMval snap = Snapshot.takeMutableSnapshot()
snap.enter {
Snapshot.observe(writeObserver = writeObserver) {
state = "A"
val nestedSnap = snap.takeNestedMutableSnapshot()
nestedSnap.enter {
state = "B"
state = "C"
}
nestedSnap.apply()
nestedSnap.dispose()
}
}
snap.apply()
snap.dispose()
//The Result is:
// State was changed to A
But frankly I have no idea why. And it also doesn't work like I thought it will. It observes only the "A" change but not the "C" change which kinda makes sense.Filip Wiesner
01/27/2022, 8:08 AMcurrentSnapshot()
function returns the global snapshot and not the snap
like I thought it will. So when I create snapshot inside another, they are both in GlobalSnapshot. But even then it doesn't make sense to me 😞Filip Wiesner
02/02/2022, 2:59 PMAdam Powell
02/02/2022, 4:49 PMChuck Jazdzewski [G]
02/02/2022, 5:11 PMGlobalSnapshotManager
does, so it can schedule some action. It is not intended to be used as apply observer which will only send a notification at most once for each change.Chuck Jazdzewski [G]
02/02/2022, 5:16 PMobjects to be modified that are not visible to the this, the parent snapshot, until the apply is called.This is referring to the values of the objects not the notification sent to the read and write observers. Changing the value of
state
in nestedSnap
is not visible in snap
until nestedSnap
is applied.