Skip to content

Commit

Permalink
Expose converter via StoreBuilder.from() function (#594)
Browse files Browse the repository at this point in the history
Signed-off-by: William Brawner <me@wbrawner.com>
  • Loading branch information
wbrawner authored Jan 8, 2024
1 parent 912a390 commit c3950a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.mobilenativefoundation.store.cache5.Cache
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcher
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherAndSourceOfTruth
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherSourceOfTruthAndMemoryCache
import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter

/**
* Main entry point for creating a [Store].
Expand Down Expand Up @@ -83,5 +84,28 @@ interface StoreBuilder<Key : Any, Output : Any> {
sourceOfTruth,
memoryCache
)

fun <Key : Any, Network : Any, Output : Any, Local: Any> from(
fetcher: Fetcher<Key, Network>,
sourceOfTruth: SourceOfTruth<Key, Local, Output>,
converter: Converter<Network, Local, Output>
): StoreBuilder<Key, Output> = storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter(
fetcher,
sourceOfTruth,
null,
converter
)

fun <Key : Any, Network : Any, Output : Any, Local: Any> from(
fetcher: Fetcher<Key, Network>,
sourceOfTruth: SourceOfTruth<Key, Local, Output>,
memoryCache: Cache<Key, Output>,
converter: Converter<Network, Local, Output>
): StoreBuilder<Key, Output> = storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter(
fetcher,
sourceOfTruth,
memoryCache,
converter
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ fun <Key : Any, Network : Any, Output : Any> storeBuilderFromFetcherSourceOfTrut
sourceOfTruth: SourceOfTruth<Key, Network, Output>,
memoryCache: Cache<Key, Output>,
): StoreBuilder<Key, Output> =
RealStoreBuilder(fetcher, sourceOfTruth, memoryCache)
RealStoreBuilder<Key, Network, Output, Network>(fetcher, sourceOfTruth, memoryCache)

fun <Key : Any, Network : Any, Output : Any, Local: Any> storeBuilderFromFetcherSourceOfTruthMemoryCacheAndConverter(
fetcher: Fetcher<Key, Network>,
sourceOfTruth: SourceOfTruth<Key, Local, Output>,
memoryCache: Cache<Key, Output>?,
converter: Converter<Network, Local, Output>,
): StoreBuilder<Key, Output> = RealStoreBuilder(fetcher, sourceOfTruth, memoryCache, converter)

internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local : Any>(
private val fetcher: Fetcher<Key, Network>,
private val sourceOfTruth: SourceOfTruth<Key, Local, Output>? = null,
private val memoryCache: Cache<Key, Output>? = null,
private val converter: Converter<Network, Local, Output> = object :
Converter<Network, Local, Output> {
override fun fromOutputToLocal(output: Output): Local =
throw IllegalStateException("non mutable store never call this function")

override fun fromNetworkToLocal(network: Network): Local = network as Local
}
private val converter: Converter<Network, Local, Output>? = null
) : StoreBuilder<Key, Output> {
private var scope: CoroutineScope? = null
private var cachePolicy: MemoryPolicy<Key, Output>? = StoreDefaults.memoryPolicy
Expand Down Expand Up @@ -75,7 +76,7 @@ internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local :
scope = scope ?: GlobalScope,
sourceOfTruth = sourceOfTruth,
fetcher = fetcher,
converter = converter,
converter = converter?: defaultConverter(),
validator = validator,
memCache = memoryCache ?: cachePolicy?.let {
CacheBuilder<Key, Output>().apply {
Expand Down Expand Up @@ -132,4 +133,11 @@ internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local :
}
}
}

private fun <Network: Any, Local: Any, Output: Any> defaultConverter() = object : Converter<Network, Local, Output> {
override fun fromOutputToLocal(output: Output): Local =
throw IllegalStateException("non mutable store never call this function")

override fun fromNetworkToLocal(network: Network): Local = network as Local
}
}

0 comments on commit c3950a1

Please sign in to comment.