From 93dc82b2a62158665611e73d5ba65d70ed1d0ad7 Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 16:19:51 +0530 Subject: [PATCH] Fix type inference for MutableStore.get() and fresh() extensions (#555) - Remove unused generic parameter from MutableStore.get() and fresh() extensions. - Default internal stream calls to Unit to allow natural invocation without explicit generic arguments. - Add unit tests in RealMutableStoreTest and connectedAndroidDeviceTest on emulator-5554. Signed-off-by: Binay Shaw --- store/build.gradle.kts | 9 +++++ .../store/store5/MutableStoreDeviceTest.kt | 36 +++++++++++++++++++ .../store/store5/impl/extensions/store.kt | 14 ++++---- .../mutablestore/RealMutableStoreTest.kt | 21 ++++++++++- .../mutablestore/util/TestSourceOfTruth.kt | 10 ++++-- 5 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/MutableStoreDeviceTest.kt diff --git a/store/build.gradle.kts b/store/build.gradle.kts index 900c73d61..f444207d2 100644 --- a/store/build.gradle.kts +++ b/store/build.gradle.kts @@ -25,6 +25,15 @@ kotlin { implementation(libs.turbine) } } + + val androidDeviceTest by getting { + dependencies { + implementation(libs.junit) + implementation(libs.kotlinx.coroutines.test) + implementation(kotlin("test")) + implementation("androidx.test:runner:1.6.2") + } + } } } diff --git a/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/MutableStoreDeviceTest.kt b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/MutableStoreDeviceTest.kt new file mode 100644 index 000000000..803d58393 --- /dev/null +++ b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/MutableStoreDeviceTest.kt @@ -0,0 +1,36 @@ +package org.mobilenativefoundation.store.store5 + +import kotlinx.coroutines.test.runTest +import org.mobilenativefoundation.store.core5.ExperimentalStoreApi +import org.mobilenativefoundation.store.store5.impl.extensions.asMutableStore +import org.mobilenativefoundation.store.store5.impl.extensions.fresh +import org.mobilenativefoundation.store.store5.impl.extensions.get +import kotlin.test.Test +import kotlin.test.assertEquals + +@OptIn(ExperimentalStoreApi::class) +class MutableStoreDeviceTest { + + @Test + fun mutableStoreGetAndFreshRunOnAndroidDeviceWithoutExplicitResponseGeneric() = runTest { + val store = StoreBuilder.from( + Fetcher.of { "remote_$it" }, + ).build() + + val mutableStore: MutableStore = store.asMutableStore( + updater = Updater.by( + post = { key: String, value: String -> + UpdaterResult.Success.Untyped("saved") + }, + ), + bookkeeper = null, + ) + + // Type inference: no generic parameters required! + val getResult = mutableStore.get("item1") + assertEquals("remote_item1", getResult) + + val freshResult = mutableStore.fresh("item1") + assertEquals("remote_item1", freshResult) + } +} diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt index cdf0910c7..98a1aaaa8 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt @@ -108,7 +108,7 @@ fun Store< * * ``` * try { - * store.get(Key.Read.All) + * store.get(key) * } catch (e: Exception) { * // handle exception * } @@ -116,12 +116,11 @@ fun Store< * * @param Key The key to get cached data for. * @param Output The common representation of the data. - * @param Response The updater result write response type. * @return The data associated with the key. */ @OptIn(ExperimentalStoreApi::class) -suspend fun MutableStore.get(key: Key) = - stream(StoreReadRequest.cached(key, refresh = false)) +suspend fun MutableStore.get(key: Key): Output = + stream(StoreReadRequest.cached(key, refresh = false)) .filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData } .first() .requireData() @@ -137,7 +136,7 @@ suspend fun MutableStore. * * ``` * try { - * store.fresh(Key.Read.All) + * store.fresh(key) * } catch (e: Exception) { * // handle exception * } @@ -145,12 +144,11 @@ suspend fun MutableStore. * * @param Key The key to fetch fresh data for. * @param Output The common representation of the data. - * @param Response The updater result write response type. * @return The fresh data associated with the key. */ @OptIn(ExperimentalStoreApi::class) -suspend fun MutableStore.fresh(key: Key) = - stream(StoreReadRequest.fresh(key)) +suspend fun MutableStore.fresh(key: Key): Output = + stream(StoreReadRequest.fresh(key)) .filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData } .first() .requireData() diff --git a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt index 734193b74..bb9958299 100644 --- a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt +++ b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/RealMutableStoreTest.kt @@ -18,6 +18,8 @@ import org.mobilenativefoundation.store.store5.StoreWriteRequest import org.mobilenativefoundation.store.store5.StoreWriteResponse import org.mobilenativefoundation.store.store5.impl.RealMutableStore import org.mobilenativefoundation.store.store5.impl.RealStore +import org.mobilenativefoundation.store.store5.impl.extensions.fresh +import org.mobilenativefoundation.store.store5.impl.extensions.get import org.mobilenativefoundation.store.store5.mutablestore.util.TestCache import org.mobilenativefoundation.store.store5.mutablestore.util.TestConverter import org.mobilenativefoundation.store.store5.mutablestore.util.TestFetcher @@ -66,7 +68,7 @@ class RealMutableStoreTest { defaultOutputToLocalConverter = { defaultLocalValue }, ) testValidator = TestValidator() - testSourceOfTruth = TestSourceOfTruth() + testSourceOfTruth = TestSourceOfTruth(localToOutput = { Note(it.id, it.content) }) testCache = TestCache() testFetcher.whenever("key1") { @@ -482,4 +484,21 @@ class RealMutableStoreTest { assertIs(response) assertEquals("someContent", delegateStore.latestOrNull("newKey")?.content) } + + @Test + fun get_givenCachedData_returnsDataDirectlyWithoutExplicitResponseGeneric() = + runTest { + delegateStore.write("testKey", Note("testId", "testContent")) + // Invoking without explicit type argument + val result = mutableStore.get("testKey") + assertEquals("testContent", result.content) + } + + @Test + fun fresh_givenFetcherData_returnsFreshDataDirectlyWithoutExplicitResponseGeneric() = + runTest { + // Invoking without explicit type argument + val result = mutableStore.fresh("key1") + assertEquals("defaultLocalContent", result.content) + } } diff --git a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/util/TestSourceOfTruth.kt b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/util/TestSourceOfTruth.kt index 3b569e8d0..41df56a84 100644 --- a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/util/TestSourceOfTruth.kt +++ b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/mutablestore/util/TestSourceOfTruth.kt @@ -7,7 +7,9 @@ import kotlinx.coroutines.flow.flow import org.mobilenativefoundation.store.store5.SourceOfTruth @Suppress("UNCHECKED_CAST") -class TestSourceOfTruth : SourceOfTruth { +class TestSourceOfTruth( + private val localToOutput: ((Local) -> Output)? = null, +) : SourceOfTruth { private val storage = HashMap() private val flows = HashMap>() private var readError: Throwable? = null @@ -40,7 +42,8 @@ class TestSourceOfTruth : SourceOfTruth : SourceOfTruth