From 5e4d0498dee0eb03ab9034242a145fc432da6602 Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 03:32:35 +0530 Subject: [PATCH 1/2] Add safe getOrResult, freshOrResult, getOrNull, freshOrNull extensions (#553) --- .../store/store5/impl/extensions/store.kt | 109 ++++++++++++++++++ .../store/store5/StoreExtensionsTest.kt | 99 ++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreExtensionsTest.kt 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..b372a9411 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 @@ -1,5 +1,6 @@ package org.mobilenativefoundation.store.store5.impl.extensions +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.flow.filterNot import kotlinx.coroutines.flow.first import org.mobilenativefoundation.store.core5.ExperimentalStoreApi @@ -63,6 +64,58 @@ suspend fun Store.fresh(key: Key) = .first() .requireData() +/** + * Returns data for [key] if cached, otherwise fresh data, wrapped in [Result]. + * Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException]. + * + * @param key The key to get cached data for. + * @return [Result] containing the data or the failure. + */ +suspend fun Store.getOrResult(key: Key): Result = + try { + Result.success(get(key)) + } catch (cancellation: CancellationException) { + throw cancellation + } catch (e: Throwable) { + Result.failure(e) + } + +/** + * Returns data for [key] if cached, otherwise fresh data. + * Returns `null` if an error occurs while fetching or reading data. + * + * @param key The key to get cached data for. + * @return The data associated with the key, or null if an error occurred. + */ +suspend fun Store.getOrNull(key: Key): Output? = + getOrResult(key).getOrNull() + +/** + * Returns fresh data for [key] while updating caches, wrapped in [Result]. + * Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException]. + * + * @param key The key to fetch fresh data for. + * @return [Result] containing the fresh data or the failure. + */ +suspend fun Store.freshOrResult(key: Key): Result = + try { + Result.success(fresh(key)) + } catch (cancellation: CancellationException) { + throw cancellation + } catch (e: Throwable) { + Result.failure(e) + } + +/** + * Returns fresh data for [key] while updating caches. + * Returns `null` if an error occurs while fetching data. + * + * @param key The key to fetch fresh data for. + * @return The fresh data associated with the key, or null if an error occurred. + */ +suspend fun Store.freshOrNull(key: Key): Output? = + freshOrResult(key).getOrNull() + /** * Extension function to convert a [Store] into a [MutableStore]. * @@ -154,3 +207,59 @@ suspend fun MutableStore. .filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData } .first() .requireData() + +/** + * Returns data for the given [key] if cached, otherwise fresh data, wrapped in [Result]. + * Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException]. + * + * @param key The key to get cached data for. + * @return [Result] containing the data or the failure. + */ +@OptIn(ExperimentalStoreApi::class) +suspend fun MutableStore.getOrResult(key: Key): Result = + try { + Result.success(get(key)) + } catch (cancellation: CancellationException) { + throw cancellation + } catch (e: Throwable) { + Result.failure(e) + } + +/** + * Returns data for the given [key] if cached, otherwise fresh data. + * Returns `null` if an error occurs while fetching or reading data. + * + * @param key The key to get cached data for. + * @return The data associated with the key, or null if an error occurred. + */ +@OptIn(ExperimentalStoreApi::class) +suspend fun MutableStore.getOrNull(key: Key): Output? = + getOrResult(key).getOrNull() + +/** + * Returns fresh data for the given [key] while updating caches, wrapped in [Result]. + * Catches non-cancellation errors into [Result.failure] while rethrowing [CancellationException]. + * + * @param key The key to fetch fresh data for. + * @return [Result] containing the fresh data or the failure. + */ +@OptIn(ExperimentalStoreApi::class) +suspend fun MutableStore.freshOrResult(key: Key): Result = + try { + Result.success(fresh(key)) + } catch (cancellation: CancellationException) { + throw cancellation + } catch (e: Throwable) { + Result.failure(e) + } + +/** + * Returns fresh data for the given [key] while updating caches. + * Returns `null` if an error occurs while fetching data. + * + * @param key The key to fetch fresh data for. + * @return The fresh data associated with the key, or null if an error occurred. + */ +@OptIn(ExperimentalStoreApi::class) +suspend fun MutableStore.freshOrNull(key: Key): Output? = + freshOrResult(key).getOrNull() diff --git a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreExtensionsTest.kt b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreExtensionsTest.kt new file mode 100644 index 000000000..af8eb2cb7 --- /dev/null +++ b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreExtensionsTest.kt @@ -0,0 +1,99 @@ +package org.mobilenativefoundation.store.store5 + +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.mobilenativefoundation.store.store5.impl.extensions.freshOrNull +import org.mobilenativefoundation.store.store5.impl.extensions.freshOrResult +import org.mobilenativefoundation.store.store5.impl.extensions.getOrNull +import org.mobilenativefoundation.store.store5.impl.extensions.getOrResult +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class StoreExtensionsTest { + + @Test + fun getOrResult_returnsSuccessWhenDataAvailable() = runTest { + val store = StoreBuilder.from( + Fetcher.of { "fetched_$it" }, + ).build() + + val result = store.getOrResult("key1") + assertTrue(result.isSuccess) + assertEquals("fetched_key1", result.getOrNull()) + } + + @Test + fun getOrResult_returnsFailureWhenFetcherThrows() = runTest { + val store = StoreBuilder.from( + Fetcher.of { throw IllegalStateException("Network error") }, + ).build() + + val result = store.getOrResult("key1") + assertTrue(result.isFailure) + assertTrue(result.exceptionOrNull() is IllegalStateException) + assertEquals("Network error", result.exceptionOrNull()?.message) + } + + @Test + fun getOrNull_returnsNullWhenFetcherThrows() = runTest { + val store = StoreBuilder.from( + Fetcher.of { throw IllegalStateException("Network error") }, + ).build() + + val value = store.getOrNull("key1") + assertNull(value) + } + + @Test + fun freshOrResult_returnsSuccessWhenDataAvailable() = runTest { + val store = StoreBuilder.from( + Fetcher.of { "fresh_$it" }, + ).build() + + val result = store.freshOrResult("key1") + assertTrue(result.isSuccess) + assertEquals("fresh_key1", result.getOrNull()) + } + + @Test + fun freshOrResult_returnsFailureWhenFetcherThrows() = runTest { + val store = StoreBuilder.from( + Fetcher.of { throw RuntimeException("Failed to fetch fresh data") }, + ).build() + + val result = store.freshOrResult("key1") + assertTrue(result.isFailure) + assertTrue(result.exceptionOrNull() is RuntimeException) + } + + @Test + fun freshOrNull_returnsNullWhenFetcherThrows() = runTest { + val store = StoreBuilder.from( + Fetcher.of { throw RuntimeException("Failed to fetch fresh data") }, + ).build() + + val value = store.freshOrNull("key1") + assertNull(value) + } + + @Test + fun getOrResult_rethrowsCancellationException() = runTest { + val store = StoreBuilder.from( + Fetcher.ofFlow { key -> + flow { + throw CancellationException("Cancelled") + } + }, + ).build() + + assertFailsWith { + store.getOrResult("key1") + } + } +} From 46860ab2680954242a5cf80fd11db4f304257af4 Mon Sep 17 00:00:00 2001 From: Binay Shaw Date: Sat, 21 Nov 2026 04:17:05 +0530 Subject: [PATCH 2/2] Add connected Android device test for safe get/fresh extensions (#553) --- store/build.gradle.kts | 9 +++++ .../store/store5/SafeExtensionsDeviceTest.kt | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/SafeExtensionsDeviceTest.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/SafeExtensionsDeviceTest.kt b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/SafeExtensionsDeviceTest.kt new file mode 100644 index 000000000..f7edf7428 --- /dev/null +++ b/store/src/androidDeviceTest/kotlin/org/mobilenativefoundation/store/store5/SafeExtensionsDeviceTest.kt @@ -0,0 +1,37 @@ +package org.mobilenativefoundation.store.store5 + +import kotlinx.coroutines.test.runTest +import org.mobilenativefoundation.store.store5.impl.extensions.freshOrNull +import org.mobilenativefoundation.store.store5.impl.extensions.freshOrResult +import org.mobilenativefoundation.store.store5.impl.extensions.getOrNull +import org.mobilenativefoundation.store.store5.impl.extensions.getOrResult +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class SafeExtensionsDeviceTest { + + @Test + fun safeGetAndFreshExtensionsRunOnAndroidDevice() = runTest { + val store = StoreBuilder.from( + Fetcher.of { + if (it == "fail") throw IllegalStateException("Device error") else "android_$it" + }, + ).build() + + val successResult = store.getOrResult("success") + assertTrue(successResult.isSuccess) + assertEquals("android_success", successResult.getOrNull()) + + val failureResult = store.getOrResult("fail") + assertTrue(failureResult.isFailure) + assertTrue(failureResult.exceptionOrNull() is IllegalStateException) + + val nullValue = store.getOrNull("fail") + assertNull(nullValue) + + val freshVal = store.freshOrNull("freshKey") + assertEquals("android_freshKey", freshVal) + } +}