Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get previously stored when a default is set #7

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class KStore<T : @Serializable Any>(
}

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@file:OptIn(ExperimentalCoroutinesApi::class)
@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalSerializationApi::class)

package io.github.xxfast.kstore

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
Expand Down Expand Up @@ -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<List<Cat>> = listStoreOf(filePath = filePath, default = listOf(MYLO))
val expect: List<Cat> = listOf(OREO)
val actual: List<Cat> = newStore.getOrEmpty()
assertEquals(expect, actual)
}

@Test
fun testPlus() = runTest {
store.plus(OREO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cat> = storeOf(filePath = filePath, default = MYLO)
val expect: Pet = OREO
val actual: Pet? = defaultStore.get()
assertEquals(expect, actual)
}

@Test
fun testWrite() = runTest {
store.set(MYLO)
Expand Down