diff --git a/kstore/src/commonMain/kotlin/io/github/xxfast/kstore/KStore.kt b/kstore/src/commonMain/kotlin/io/github/xxfast/kstore/KStore.kt index 660bf87..c9b5c62 100644 --- a/kstore/src/commonMain/kotlin/io/github/xxfast/kstore/KStore.kt +++ b/kstore/src/commonMain/kotlin/io/github/xxfast/kstore/KStore.kt @@ -47,7 +47,7 @@ class KStore( } private suspend fun read(fromCache: Boolean): T? { - if (fromCache && stateFlow.value != null) return stateFlow.value + if (fromCache && stateFlow.value != default) return stateFlow.value val decoded: T? = try { decoder.invoke() } catch (e: Exception) { null } val emitted: T? = decoded ?: default stateFlow.emit(emitted) diff --git a/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KListStoreTests.kt b/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KListStoreTests.kt index b50aa6b..c8bbe9a 100644 --- a/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KListStoreTests.kt +++ b/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KListStoreTests.kt @@ -1,4 +1,4 @@ -@file:OptIn(ExperimentalCoroutinesApi::class) +@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalSerializationApi::class) package io.github.xxfast.kstore @@ -6,7 +6,12 @@ import app.cash.turbine.test import io.github.xxfast.kstore.utils.FILE_SYSTEM import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.okio.encodeToBufferedSink import okio.Path.Companion.toPath +import okio.buffer +import okio.use import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals @@ -43,6 +48,16 @@ class KListStoreTests { assertEquals(expect, actual) } + @Test + fun testReadPreviouslyStoredList() = runTest { + FILE_SYSTEM.sink(filePath.toPath()).buffer().use { Json.encodeToBufferedSink(listOf(OREO) , it) } + // Mylo will never be sent 😿 because there is already a stored value + val newStore: KStore> = listStoreOf(filePath = filePath, default = listOf(MYLO)) + val expect: List = listOf(OREO) + val actual: List = newStore.getOrEmpty() + assertEquals(expect, actual) + } + @Test fun testPlus() = runTest { store.plus(OREO) diff --git a/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KStoreTests.kt b/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KStoreTests.kt index 7d2dcc1..8301a65 100644 --- a/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KStoreTests.kt +++ b/kstore/src/commonTest/kotlin/io/github/xxfast/kstore/KStoreTests.kt @@ -79,6 +79,16 @@ class KStoreTests { assertEquals(expect, actual) } + @Test + fun testReadPreviouslyStoredWithDefault() = runTest { + FILE_SYSTEM.sink(filePath.toPath()).buffer().use { Json.encodeToBufferedSink(OREO, it) } + // Mylo will never be sent 😿 because there is already a stored value + val defaultStore: KStore = storeOf(filePath = filePath, default = MYLO) + val expect: Pet = OREO + val actual: Pet? = defaultStore.get() + assertEquals(expect, actual) + } + @Test fun testWrite() = runTest { store.set(MYLO)