Skip to content

Commit

Permalink
Enable conversion from StoreBuilder to MutableStoreBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Ramotar <[email protected]>
  • Loading branch information
matt-ramotar committed Mar 31, 2023
1 parent a22046e commit e8ca4a2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ interface MutableStoreBuilder<Key : Any, Network : Any, Output : Any, Local : An
* @param fetcher a function for fetching a flow of network records.
* @param sourceOfTruth a [SourceOfTruth] for the store.
*/
fun <Key : Any, Network : Any, Output : Any> from(
fun <Key : Any, Network : Any, Output : Any, Local : Any> from(
fetcher: Fetcher<Key, Network>,
sourceOfTruth: SourceOfTruth<Key, Output>
): MutableStoreBuilder<Key, Network, Output, Output> =
sourceOfTruth: SourceOfTruth<Key, Local>
): MutableStoreBuilder<Key, Network, Output, Local> =
mutableStoreBuilderFromFetcherAndSourceOfTruth(fetcher = fetcher, sourceOfTruth = sourceOfTruth)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import org.mobilenativefoundation.store.store5.impl.storeBuilderFromFetcherAndSo
interface StoreBuilder<Key : Any, Output : Any> {
fun build(): Store<Key, Output>


fun <Network : Any, Local : Any> toMutableStoreBuilder(): MutableStoreBuilder<Key, Network, Output, Local>

/**
* A store multicasts same [Output] value to many consumers (Similar to RxJava.share()), by default
* [Store] will open a global scope for management of shared responses, if instead you'd like to control
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ import org.mobilenativefoundation.store.store5.Validator
import org.mobilenativefoundation.store.store5.impl.extensions.asMutableStore


fun <Key : Any, Output : Any, Network : Any, Local : Any> mutableStoreBuilderFromFetcherAndSourceOfTruth(
fun <Key : Any, Network : Any, Output : Any, Local : Any> mutableStoreBuilderFromFetcher(
fetcher: Fetcher<Key, Network>,
): MutableStoreBuilder<Key, Network, Output, Local> = RealMutableStoreBuilder(fetcher)


fun <Key : Any, Network : Any, Output : Any, Local : Any> mutableStoreBuilderFromFetcherAndSourceOfTruth(
fetcher: Fetcher<Key, Network>,
sourceOfTruth: SourceOfTruth<Key, Local>,
): MutableStoreBuilder<Key, Network, Output, Local> = RealMutableStoreBuilder(fetcher, sourceOfTruth)


internal class RealMutableStoreBuilder<Key : Any, Network : Any, Output : Any, Local : Any>(
private val fetcher: Fetcher<Key, Network>,
private val sourceOfTruth: SourceOfTruth<Key, Local>? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
@file:Suppress("UNCHECKED_CAST")

package org.mobilenativefoundation.store.store5.impl

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import org.mobilenativefoundation.store.store5.Converter
import org.mobilenativefoundation.store.store5.Fetcher
import org.mobilenativefoundation.store.store5.MemoryPolicy
import org.mobilenativefoundation.store.store5.MutableStoreBuilder
import org.mobilenativefoundation.store.store5.SourceOfTruth
import org.mobilenativefoundation.store.store5.Store
import org.mobilenativefoundation.store.store5.StoreBuilder
Expand Down Expand Up @@ -60,4 +63,39 @@ internal class RealStoreBuilder<Key : Any, Network : Any, Output : Any, Local :
converter = converter,
validator = validator
)

override fun <Network : Any, Local : Any> toMutableStoreBuilder(): MutableStoreBuilder<Key, Network, Output, Local> {
val storeBuilder = this
fetcher as Fetcher<Key, Network>
return if (sourceOfTruth == null) {
mutableStoreBuilderFromFetcher<Key, Network, Output, Local>(fetcher).apply {
if (storeBuilder.scope != null) {
scope(storeBuilder.scope!!)
}

if (storeBuilder.cachePolicy != null) {
cachePolicy(storeBuilder.cachePolicy)
}

if (storeBuilder.validator != null) {
validator(storeBuilder.validator!!)
}
}
} else {
sourceOfTruth as SourceOfTruth<Key, Local>
mutableStoreBuilderFromFetcherAndSourceOfTruth<Key, Network, Output, Local>(fetcher, sourceOfTruth).apply {
if (storeBuilder.scope != null) {
scope(storeBuilder.scope!!)
}

if (storeBuilder.cachePolicy != null) {
cachePolicy(storeBuilder.cachePolicy)
}

if (storeBuilder.validator != null) {
validator(storeBuilder.validator!!)
}
}
}
}
}

0 comments on commit e8ca4a2

Please sign in to comment.