commonMain.tech.skot.model.SKDistantDataWithCacheAndLiveKey.kt Maven / Gradle / Ivy
package tech.skot.model
import kotlinx.serialization.KSerializer
abstract class SKDistantDataWithCacheAndLiveKey(
name: String,
serializer: KSerializer,
private val cache: SKPersistor = globalPersistor,
validity: Long? = null,
private val fixKey: String?,
private val liveKey: () -> String,
fetchData: suspend () -> D
) : SKDistantDataWithCache(
name = name,
serializer = serializer,
key = "${fixKey}_${liveKey()}",
cache = cache,
validity = validity,
fetchData = fetchData
) {
override suspend fun get(validity: Long?): D {
if (_current != null && liveKeyOfCurrentValue != liveKey()) {
update()
}
return super.get(validity)
}
override val completeKey: String?
get() = "${fixKey}_${liveKey()}"
private var liveKeyOfCurrentValue: String? = null
override fun setDatedData(newDatedData: DatedData) {
liveKeyOfCurrentValue = liveKey()
super.setDatedData(newDatedData)
}
override suspend fun newDatedData(): DatedData {
return super.newDatedData().also { liveKeyOfCurrentValue = liveKey() }
}
override suspend fun fallBackValue(): D? {
if (liveKeyOfCurrentValue == liveKey()) {
return super.fallBackValue()
} else {
return null
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy