Skip to content

Instantly share code, notes, and snippets.

@takahirom
Created October 17, 2021 08:05
Show Gist options
  • Save takahirom/c6d62073aba52f819cd44a14fe95cc56 to your computer and use it in GitHub Desktop.
Save takahirom/c6d62073aba52f819cd44a14fe95cc56 to your computer and use it in GitHub Desktop.
class ViewModel {
val state = mutableStateOf("init")
}
fun main() {
val viewModel = ViewModel()
lateinit var currentScope: String
val observations = mutableMapOf<Any, String>()
val snapshot = Snapshot.takeMutableSnapshot(readObserver = {
observations[it] = currentScope
})
snapshot.enter {
currentScope = "Root()"
currentScope = "Content()"
// read
viewModel.state.value
currentScope = "Root()"
}
snapshot.apply()
observations.forEach { mutableState, scope ->
println("`$mutableState` is observed by `$scope`")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment