commonMain.ru.alexey.event.threads.StateSaver.kt Maven / Gradle / Ivy
package ru.alexey.event.threads
import androidx.compose.runtime.compositionLocalOf
import kotlin.reflect.KClass
interface StateSaver {
val savedParams: Map, () -> Any>
fun save(clazz: KClass, instance: T)
fun load(clazz: KClass): T?
fun remove(clazz: KClass)
}
internal class DefaultStateSaver : StateSaver {
private val map = mutableMapOf, Any>()
override val savedParams: Map, () -> Any>
get() = map.mapValues {
{ it.value }
}
override fun save(clazz: KClass, instance: T) {
map[clazz] = instance
}
override fun load(clazz: KClass): T? {
return map[clazz] as? T
}
override fun remove(clazz: KClass) {
map.remove(clazz)
}
}
val LocalStateSaver = compositionLocalOf {
error("Provide State Saver")
}