From 53458fbb7dbeb6f443b9dd72a0266ebd96aaa8d9 Mon Sep 17 00:00:00 2001 From: matt-ramotar Date: Wed, 16 Oct 2024 23:34:21 -0400 Subject: [PATCH] Format Signed-off-by: matt-ramotar --- .../store/cache5/MonotonicTicker.kt | 2 - .../store/cache5/StoreMultiCache.kt | 10 +- .../store/cache5/StoreMultiCacheAccessor.kt | 2 + .../store5/StoreWithInMemoryCacheTests.kt | 104 ++++++++++-------- 4 files changed, 67 insertions(+), 51 deletions(-) diff --git a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MonotonicTicker.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MonotonicTicker.kt index 30e346448..28968d476 100644 --- a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MonotonicTicker.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/MonotonicTicker.kt @@ -1,7 +1,5 @@ package org.mobilenativefoundation.store.cache5 -import kotlin.time.ExperimentalTime import kotlin.time.TimeSource -@OptIn(ExperimentalTime::class) internal val MonotonicTicker: Ticker = TimeSource.Monotonic.markNow().let { timeMark -> { timeMark.elapsedNow().inWholeNanoseconds } } diff --git a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt index 880b73042..1323aefc0 100644 --- a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCache.kt @@ -2,6 +2,7 @@ package org.mobilenativefoundation.store.cache5 +import org.mobilenativefoundation.store.core5.ExperimentalStoreApi import org.mobilenativefoundation.store.core5.KeyProvider import org.mobilenativefoundation.store.core5.StoreData import org.mobilenativefoundation.store.core5.StoreKey @@ -12,7 +13,14 @@ import org.mobilenativefoundation.store.core5.StoreKey * Depends on [StoreMultiCacheAccessor] for internal data management. * @see [Cache]. */ -class StoreMultiCache, Single : StoreData.Single, Collection : StoreData.Collection, Output : StoreData>( +@OptIn(ExperimentalStoreApi::class) +class StoreMultiCache< + Id : Any, + Key : StoreKey, + Single : StoreData.Single, + Collection : StoreData.Collection, + Output : StoreData, + >( private val keyProvider: KeyProvider, singlesCache: Cache, Single> = CacheBuilder, Single>().build(), collectionsCache: Cache, Collection> = CacheBuilder, Collection>().build(), diff --git a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt index d7a69e0b5..eb8535e45 100644 --- a/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt +++ b/cache/src/commonMain/kotlin/org/mobilenativefoundation/store/cache5/StoreMultiCacheAccessor.kt @@ -2,6 +2,7 @@ package org.mobilenativefoundation.store.cache5 import kotlinx.atomicfu.locks.SynchronizedObject import kotlinx.atomicfu.locks.synchronized +import org.mobilenativefoundation.store.core5.ExperimentalStoreApi import org.mobilenativefoundation.store.core5.StoreData import org.mobilenativefoundation.store.core5.StoreKey @@ -20,6 +21,7 @@ import org.mobilenativefoundation.store.core5.StoreKey * @property singlesCache The cache used to store single data items. * @property collectionsCache The cache used to store collections of data items. */ +@OptIn(ExperimentalStoreApi::class) class StoreMultiCacheAccessor, Single : StoreData.Single>( private val singlesCache: Cache, Single>, private val collectionsCache: Cache, Collection>, diff --git a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreWithInMemoryCacheTests.kt b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreWithInMemoryCacheTests.kt index ba3a03289..7a397d236 100644 --- a/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreWithInMemoryCacheTests.kt +++ b/store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/StoreWithInMemoryCacheTests.kt @@ -5,17 +5,15 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job -import kotlinx.coroutines.async -import kotlinx.coroutines.awaitAll -import kotlinx.coroutines.cancel -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest import org.mobilenativefoundation.store.store5.impl.extensions.get import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertIs -import kotlin.test.assertNotNull import kotlin.time.Duration.Companion.hours @FlowPreview @@ -56,77 +54,87 @@ class StoreWithInMemoryCacheTests { val store = StoreBuilder .from( - fetcher = Fetcher.of { key: Int -> "fetcher_${key}" }, - sourceOfTruth = SourceOfTruth.Companion.of( - reader = { key -> - flow { - emit("source_of_truth_${key}") - } - }, - writer = { key: Int, local: String -> - - } - ) + fetcher = Fetcher.of { key: Int -> "fetcher_$key" }, + sourceOfTruth = + SourceOfTruth.Companion.of( + reader = { key -> + flow { + emit("source_of_truth_$key") + } + }, + writer = { key: Int, local: String -> + }, + ), ) .disableCache() .toMutableStoreBuilder( - converter = object : Converter { - override fun fromNetworkToLocal(network: String): String { - return network - } + converter = + object : Converter { + override fun fromNetworkToLocal(network: String): String { + return network + } - override fun fromOutputToLocal(output: String): String { - return output - } - }, + override fun fromOutputToLocal(output: String): String { + return output + } + }, ) .build( - updater = object : Updater { - var callCount = -1 - override suspend fun post(key: Int, value: String): UpdaterResult { - callCount += 1 - if (callCount % 2 == 0) { - throw IllegalArgumentException(key.toString() + "value:$value") - } else { - return UpdaterResult.Success.Untyped("") - } - } + updater = + object : Updater { + var callCount = -1 - override val onCompletion: OnUpdaterCompletion? - get() = null + override suspend fun post( + key: Int, + value: String, + ): UpdaterResult { + callCount += 1 + if (callCount % 2 == 0) { + throw IllegalArgumentException(key.toString() + "value:$value") + } else { + return UpdaterResult.Success.Untyped("") + } + } - } + override val onCompletion: OnUpdaterCompletion? + get() = null + }, ) val jobs = mutableListOf() jobs.add( store.stream(StoreReadRequest.cached(1, refresh = true)) .mapNotNull { it.dataOrNull() } - .launchIn(CoroutineScope(Dispatchers.Default)) + .launchIn(CoroutineScope(Dispatchers.Default)), ) - val job1 = store.stream(StoreReadRequest.cached(0, refresh = true)) - .mapNotNull { it.dataOrNull() } - .launchIn(CoroutineScope(Dispatchers.Default)) + val job1 = + store.stream(StoreReadRequest.cached(0, refresh = true)) + .mapNotNull { it.dataOrNull() } + .launchIn(CoroutineScope(Dispatchers.Default)) jobs.add( store.stream(StoreReadRequest.cached(2, refresh = true)) .mapNotNull { it.dataOrNull() } - .launchIn(CoroutineScope(Dispatchers.Default))) + .launchIn(CoroutineScope(Dispatchers.Default)), + ) jobs.add( store.stream(StoreReadRequest.cached(3, refresh = true)) .mapNotNull { it.dataOrNull() } - .launchIn(CoroutineScope(Dispatchers.Default))) + .launchIn(CoroutineScope(Dispatchers.Default)), + ) job1.cancel() assertEquals( expected = "source_of_truth_0", - actual = store.stream(StoreReadRequest.cached(0, refresh = true)) - .mapNotNull { it.dataOrNull() }.first() + actual = + store.stream(StoreReadRequest.cached(0, refresh = true)) + .mapNotNull { it.dataOrNull() }.first(), ) jobs.forEach { it.cancel() assertEquals( expected = "source_of_truth_0", - actual = store.stream(StoreReadRequest.cached(0, refresh = true)) - .mapNotNull { it.dataOrNull() }.first() + actual = + store.stream(StoreReadRequest.cached(0, refresh = true)) + .mapNotNull { it.dataOrNull() }.first(), ) } }