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

commonMain.net.folivo.trixnity.client.store.AccountStore.kt Maven / Gradle / Ivy

There is a newer version: 4.7.1
Show newest version
package net.folivo.trixnity.client.store

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
import net.folivo.trixnity.client.store.cache.MinimalRepositoryObservableCache
import net.folivo.trixnity.client.store.repository.AccountRepository
import net.folivo.trixnity.client.store.repository.RepositoryTransactionManager
import kotlin.time.Duration

class AccountStore(
    repository: AccountRepository,
    tm: RepositoryTransactionManager,
    storeScope: CoroutineScope
) : Store {
    private val accountCache = MinimalRepositoryObservableCache(repository, tm, storeScope, Duration.INFINITE)

    suspend fun getAccount() = accountCache.read(1).first()
    fun getAccountAsFlow() = accountCache.read(1)
    suspend fun updateAccount(updater: suspend (Account) -> Account) = accountCache.write(1) { account ->
        updater(account ?: Account(null, null, null, null, null, null, null, null, null, null))
    }

    override suspend fun clearCache() {}

    override suspend fun deleteAll() {
        accountCache.deleteAll()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy