Skip to content

Commit c3950a1

Browse files
authored
Expose converter via StoreBuilder.from() function (#594)
Signed-off-by: William Brawner <me@wbrawner.com>
1 parent 912a390 commit c3950a1

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreBuilder.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.mobilenativefoundation.store.cache5.Cache
2020
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcher
2121
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherAndSourceOfTruth
2222
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherSourceOfTruthAndMemoryCache
23+
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter
2324

2425
/**
2526
* Main entry point for creating a [Store].
@@ -83,5 +84,28 @@ interface StoreBuilder<Key : Any, Output : Any> {
8384
sourceOfTruth,
8485
memoryCache
8586
)
87+
88+
fun <Key : Any, Network : Any, Output : Any, Local: Any> from(
89+
fetcher: Fetcher<Key, Network>,
90+
sourceOfTruth: SourceOfTruth<Key, Local, Output>,
91+
converter: Converter<Network, Local, Output>
92+
): StoreBuilder<Key, Output> = storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter(
93+
fetcher,
94+
sourceOfTruth,
95+
null,
96+
converter
97+
)
98+
99+
fun <Key : Any, Network : Any, Output : Any, Local: Any> from(
100+
fetcher: Fetcher<Key, Network>,
101+
sourceOfTruth: SourceOfTruth<Key, Local, Output>,
102+
memoryCache: Cache<Key, Output>,
103+
converter: Converter<Network, Local, Output>
104+
): StoreBuilder<Key, Output> = storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter(
105+
fetcher,
106+
sourceOfTruth,
107+
memoryCache,
108+
converter
109+
)
86110
}
87111
}

store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealStoreBuilder.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ fun <Key : Any, Network : Any, Output : Any> storeBuilderFromFetcherSourceOfTrut
3333
sourceOfTruth: SourceOfTruth<Key, Network, Output>,
3434
memoryCache: Cache<Key, Output>,
3535
): StoreBuilder<Key, Output> =
36-
RealStoreBuilder(fetcher, sourceOfTruth, memoryCache)
36+
RealStoreBuilder<Key, Network, Output, Network>(fetcher, sourceOfTruth, memoryCache)
37+
38+
fun <Key : Any, Network : Any, Output : Any, Local: Any> storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter(
39+
fetcher: Fetcher<Key, Network>,
40+
sourceOfTruth: SourceOfTruth<Key, Local, Output>,
41+
memoryCache: Cache<Key, Output>?,
42+
converter: Converter<Network, Local, Output>,
43+
): StoreBuilder<Key, Output> = RealStoreBuilder(fetcher, sourceOfTruth, memoryCache, converter)
3744

3845
internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local : Any>(
3946
private val fetcher: Fetcher<Key, Network>,
4047
private val sourceOfTruth: SourceOfTruth<Key, Local, Output>? = null,
4148
private val memoryCache: Cache<Key, Output>? = null,
42-
private val converter: Converter<Network, Local, Output> = object :
43-
Converter<Network, Local, Output> {
44-
override fun fromOutputToLocal(output: Output): Local =
45-
throw IllegalStateException("non mutable store never call this function")
46-
47-
override fun fromNetworkToLocal(network: Network): Local = network as Local
48-
}
49+
private val converter: Converter<Network, Local, Output>? = null
4950
) : StoreBuilder<Key, Output> {
5051
private var scope: CoroutineScope? = null
5152
private var cachePolicy: MemoryPolicy<Key, Output>? = StoreDefaults.memoryPolicy
@@ -75,7 +76,7 @@ internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local :
7576
scope = scope ?: GlobalScope,
7677
sourceOfTruth = sourceOfTruth,
7778
fetcher = fetcher,
78-
converter = converter,
79+
converter = converter?: defaultConverter(),
7980
validator = validator,
8081
memCache = memoryCache ?: cachePolicy?.let {
8182
CacheBuilder<Key, Output>().apply {
@@ -132,4 +133,11 @@ internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local :
132133
}
133134
}
134135
}
136+
137+
private fun <Network: Any, Local: Any, Output: Any> defaultConverter() = object : Converter<Network, Local, Output> {
138+
override fun fromOutputToLocal(output: Output): Local =
139+
throw IllegalStateException("non mutable store never call this function")
140+
141+
override fun fromNetworkToLocal(network: Network): Local = network as Local
142+
}
135143
}

0 commit comments

Comments
 (0)