All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.tech.skot.model.SKDistantDataWithCacheAndLiveKey.kt Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
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