-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate MutableStoreBuilder from StoreBuilder (#542)
* Separate MutableStoreBuilder from StoreBuilder Signed-off-by: Matt Ramotar <[email protected]> * Enable conversion from StoreBuilder to MutableStoreBuilder Signed-off-by: Matt Ramotar <[email protected]> * Clean up Signed-off-by: Matt Ramotar <[email protected]> * Fix tests Signed-off-by: Matt Ramotar <[email protected]> --------- Signed-off-by: Matt Ramotar <[email protected]>
- Loading branch information
1 parent
fc249e1
commit e050a15
Showing
10 changed files
with
200 additions
and
72 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/MutableStoreBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.mobilenativefoundation.store.store5 | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import org.mobilenativefoundation.store.store5.impl.mutableStoreBuilderFromFetcherAndSourceOfTruth | ||
|
||
interface MutableStoreBuilder<Key : Any, Network : Any, Output : Any, Local : Any> { | ||
|
||
fun <Response : Any> build( | ||
updater: Updater<Key, Output, Response>, | ||
bookkeeper: Bookkeeper<Key>? = null | ||
): MutableStore<Key, Output> | ||
|
||
/** | ||
* 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 | ||
* the scope that sharing/multicasting happens in you can pass a @param [scope] | ||
* | ||
* @param scope - scope to use for sharing | ||
*/ | ||
fun scope(scope: CoroutineScope): MutableStoreBuilder<Key, Network, Output, Local> | ||
|
||
/** | ||
* controls eviction policy for a store cache, use [MemoryPolicy.MemoryPolicyBuilder] to configure a TTL | ||
* or size based eviction | ||
* Example: MemoryPolicy.builder().setExpireAfterWrite(10.seconds).build() | ||
*/ | ||
fun cachePolicy(memoryPolicy: MemoryPolicy<Key, Output>?): MutableStoreBuilder<Key, Network, Output, Local> | ||
|
||
/** | ||
* by default a Store caches in memory with a default policy of max items = 100 | ||
*/ | ||
fun disableCache(): MutableStoreBuilder<Key, Network, Output, Local> | ||
|
||
fun converter(converter: Converter<Network, Output, Local>): | ||
MutableStoreBuilder<Key, Network, Output, Local> | ||
|
||
fun validator(validator: Validator<Output>): MutableStoreBuilder<Key, Network, Output, Local> | ||
|
||
companion object { | ||
/** | ||
* Creates a new [MutableStoreBuilder] from a [Fetcher] and a [SourceOfTruth]. | ||
* | ||
* @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, Local : Any> from( | ||
fetcher: Fetcher<Key, Network>, | ||
sourceOfTruth: SourceOfTruth<Key, Local> | ||
): MutableStoreBuilder<Key, Network, Output, Local> = | ||
mutableStoreBuilderFromFetcherAndSourceOfTruth(fetcher = fetcher, sourceOfTruth = sourceOfTruth) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStoreBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.mobilenativefoundation.store.store5.impl | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.GlobalScope | ||
import org.mobilenativefoundation.store.store5.Bookkeeper | ||
import org.mobilenativefoundation.store.store5.Converter | ||
import org.mobilenativefoundation.store.store5.Fetcher | ||
import org.mobilenativefoundation.store.store5.MemoryPolicy | ||
import org.mobilenativefoundation.store.store5.MutableStore | ||
import org.mobilenativefoundation.store.store5.MutableStoreBuilder | ||
import org.mobilenativefoundation.store.store5.SourceOfTruth | ||
import org.mobilenativefoundation.store.store5.Store | ||
import org.mobilenativefoundation.store.store5.StoreDefaults | ||
import org.mobilenativefoundation.store.store5.Updater | ||
import org.mobilenativefoundation.store.store5.Validator | ||
import org.mobilenativefoundation.store.store5.impl.extensions.asMutableStore | ||
|
||
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, | ||
) : MutableStoreBuilder<Key, Network, Output, Local> { | ||
private var scope: CoroutineScope? = null | ||
private var cachePolicy: MemoryPolicy<Key, Output>? = StoreDefaults.memoryPolicy | ||
private var converter: Converter<Network, Output, Local>? = null | ||
private var validator: Validator<Output>? = null | ||
|
||
override fun scope(scope: CoroutineScope): MutableStoreBuilder<Key, Network, Output, Local> { | ||
this.scope = scope | ||
return this | ||
} | ||
|
||
override fun cachePolicy(memoryPolicy: MemoryPolicy<Key, Output>?): MutableStoreBuilder<Key, Network, Output, Local> { | ||
cachePolicy = memoryPolicy | ||
return this | ||
} | ||
|
||
override fun disableCache(): MutableStoreBuilder<Key, Network, Output, Local> { | ||
cachePolicy = null | ||
return this | ||
} | ||
|
||
override fun validator(validator: Validator<Output>): MutableStoreBuilder<Key, Network, Output, Local> { | ||
this.validator = validator | ||
return this | ||
} | ||
|
||
override fun converter(converter: Converter<Network, Output, Local>): MutableStoreBuilder<Key, Network, Output, Local> { | ||
this.converter = converter | ||
return this | ||
} | ||
|
||
fun build(): Store<Key, Output> = RealStore( | ||
scope = scope ?: GlobalScope, | ||
sourceOfTruth = sourceOfTruth, | ||
fetcher = fetcher, | ||
memoryPolicy = cachePolicy, | ||
converter = converter, | ||
validator = validator | ||
) | ||
|
||
override fun <UpdaterResult : Any> build( | ||
updater: Updater<Key, Output, UpdaterResult>, | ||
bookkeeper: Bookkeeper<Key>? | ||
): MutableStore<Key, Output> = | ||
build().asMutableStore<Key, Network, Output, Local, UpdaterResult>( | ||
updater = updater, | ||
bookkeeper = bookkeeper | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.