Coder Social home page Coder Social logo

Comments (6)

digitalbuddha avatar digitalbuddha commented on May 30, 2024

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.

eyalgu avatar eyalgu commented on May 30, 2024

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.

digitalbuddha avatar digitalbuddha commented on May 30, 2024

Sure! I can move the top level ones to extensions, I think the rest are extensions right now

from store.

digitalbuddha avatar digitalbuddha commented on May 30, 2024

How do you feel about just having top level functions
fun rxSingleStore(...)
fun rxFlowableStore(...)

I can change other builders to this too

from store.

eyalgu avatar eyalgu commented on May 30, 2024

from store.

digitalbuddha avatar digitalbuddha commented on May 30, 2024

Yup, totally can, great suggestion!

from store.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.