The 'previous' in Snapshot is difficult to understand. Why is 'previous' either 40 or 0?
@Test
fun myTest() {
fun myPolicy(): SnapshotMutationPolicy<Int> = object : SnapshotMutationPolicy<Int> {
override fun equivalent(a: Int, b: Int): Boolean {
println("equivalent a: $a, equivalent b: $b")
return a == b
}
override fun merge(previous: Int, current: Int, applied: Int): Int {
println("previous: $previous, current: $current, applied: $applied")
return applied
}
}
val state = mutableStateOf(0, myPolicy())
val snapshot1 = Snapshot.takeMutableSnapshot()
val snapshot2 = Snapshot.takeMutableSnapshot()
val snapshot3 = Snapshot.takeMutableSnapshot()
val snapshot4 = Snapshot.takeMutableSnapshot()
snapshot1.enter {
state.value = 10
}
snapshot2.enter {
state.value = 20
}
snapshot3.enter {
state.value = 30
}
snapshot4.enter {
state.value = 40
}
snapshot1.apply() // previous: 40, current: 0, applied: 10
snapshot2.apply() // previous: 40, current: 10, applied: 20
snapshot3.apply() // previous: 0, current: 20, applied: 30
snapshot4.apply() // previous: 0, current: 30, applied: 40
}