Comments (6)
Proposed design
Biggest issue I see is how to model persister read function. If we expect a Maybe<T>
then we force user to return Maybe.empty() since rxjava will crash on Maybe.just(null). @yigit how does Room return when no values?
Builder
interface RxStoreBuilder<Key : Any, Output : Any> {
companion object {
/**
* Creates a new [StoreBuilder] from a non-[Flow] fetcher.
*
* Use when creating a [Store] that fetches objects in an HTTP-like single response per
* request protocol.
*
* @param fetcher a function for fetching network records.
*/
fun <Key : Any, Output : Any> fromSingle(
fetcher: (key: Key) -> Single<Output>
): BuilderImpl<Key, Output> = BuilderImpl { key: Key ->
fetcher.invoke(key).toFlowable().asFlow()
}
/**
* Creates a new [StoreBuilder] from a [Flow] fetcher.
*
* Use when creating a [Store] that fetches objects in an websocket-like multiple responses
* per request protocol.
*
* @param fetcher a function for fetching a flow of network records.
*/
fun <Key : Any, Output : Any> fromFlowable(
fetcher: (key: Key) -> Flowable<Output>
): StoreBuilder<Key, Output> = BuilderImpl { key: Key ->
fetcher.invoke(key).asFlow()
}
}
}
Extension Functions
fun <Key : Any, Output : Any> Store<Key, Output>.asRxStore(): RxStore<Key, Output> =
object : RxStore<Key, Output> {
override fun stream(request: StoreRequest<Key>): Flowable<StoreResponse<Output>> =
this@asRxStore.stream(request).asFlowable()
override fun clear(key: Key): Single<Unit> =
rxSingle { this@asRxStore.clear(key) }
@ExperimentalStoreApi
override fun clearAll(): Single<Unit> = rxSingle { this@asRxStore.clearAll() }
}
fun <Key : Any, Output : Any, NewOutput : Any> BuilderImpl<Key, Output>.withSinglePersister(
reader: (Key) -> Maybe<NewOutput>,
writer: (Key, Output) -> Single<Unit>,
delete: ((Key) -> Unit)? = null,
deleteAll: (() -> Unit)? = null
): BuilderWithSourceOfTruth<Key, Output, NewOutput> {
return nonFlowingPersister(
reader = { key -> reader.invoke(key).await() },
writer = { key, output -> writer.invoke(key, output).await() },
delete = { key -> delete?.invoke(key) },
deleteAll = { deleteAll?.invoke() }
)
}
fun <Key : Any, Output : Any, NewOutput : Any> BuilderImpl<Key, Output>.withFlowablePersister(
reader: (Key) -> Flowable<NewOutput>,
writer: (Key, Output) -> Single<Unit>,
delete: ((Key) -> Unit)? = null,
deleteAll: (() -> Unit)? = null
) {
persister(
reader = { key -> reader.invoke(key).asFlow() },
writer = { key, output -> writer.invoke(key, output).await() },
delete = { key -> delete?.invoke(key) },
deleteAll = { deleteAll?.invoke() }
)
}
Example
val fetcher: (Int) -> Single<String> = { Single.just("three-1") }
val persister=InMemoryRxPersister<Int,String>()
val rxStore: RxStore<Int, String> = RxStoreBuilder.fromSingle(fetcher)
.withSinglePersister(
reader = {key->persister.read(key)},
writer = {key,value->persister.write(key,value)}
)
.build()
.asRxStore()
val testObserver = rxStore.stream(StoreRequest.cached(3, refresh = false))
.test()
testObserver.assertValues(
Loading(
origin = Fetcher
), Data(
value = "three-1",
origin = Fetcher
)
)
from store.
Instead of RxStoreBuilder
and RxStore
can we just use extension functions on StoreBuilder
and Store
?
All we need is
fun StoreBuilder.fromSingle(...)
fun StoreBuilder.fromObservable(...)
fun StoreBuilder.fromFlow(...)
fun StoreBuilder.fromMaybe(...) // not sure if this makes sense
fun Store.observe(...)
fun Store.single(...)
from store.
Sure! I can move the top level ones to extensions, I think the rest are extensions right now
from store.
How do you feel about just having top level functions
fun rxSingleStore(...)
fun rxFlowableStore(...)
I can change other builders to this too
from store.
from store.
Yup, totally can, great suggestion!
from store.
Related Issues (20)
- [Feature Request] Add converter to StoreBuilder HOT 7
- [Feature Request] get() and fresh() extensions on MutableStore
- [Feature Request] HOT 1
- Inconsistent SourceOfTruth argument types HOT 2
- [BUG] Fix breaking changes with source of truth HOT 7
- [Documentation] Update Store documentation HOT 1
- Integrate website with CI
- [Feature Request] The stream of StoreResponse<T> should emit Loading(origin = SourceOfTruth) HOT 1
- [Question] Data with nested keys HOT 1
- [BUG] Stop with the breaking API changes in beta HOT 4
- [BUG] 5.0.0-beta02 breaks StoreReadRequest.cached + Validator HOT 2
- [BUG] Cached(refresh = true) will collect SoT reader twice HOT 3
- [Feature Request] fromLocalToOutput converter method went missing
- [BUG] Deadlock during synchronization in MutableStore
- [BUG] Examples on https://mobilenativefoundation.github.io/Store/mutable-store/building/builder/ are wrong
- [Feature Request] Common Testing Utils
- [BUG] Documentation "Edit this page" button 404's HOT 1
- Store Not Fetching or Read/Writing HOT 6
- CLA HOT 1
- [Question] Why should anyone consider using Store if they are already using NetworkBoundResource concept/framework? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store.