https://kotlinlang.org logo
#rx
Title
# rx
f

filipradon

07/30/2019, 9:08 AM
Hi guys, quick question about Rx doOnNext/doOnComplete etc callbacks. We often battle during PR reviews whether we should use doOnNext/doOnComplete to do some simple “side” logic or whether we should incorporate it into the chain. Main argument for incorporating it into the chain was that we should avoid side effects and all the data mutation should be in the chain so we are sure all is called in a synchronous manner. I tried to write some tests to prove that but I didn’t manage to do so. (Probably because of schedulers behaviour in tests) Do you guys have an idea for the test that proves that for example doOnComplete can finish it’s operation waaay long after the whole chain completes and we could already modified its results. Snippet shows those two approches we are talking about:
Copy code
@Test
    fun `verify completable side effects`() {

        Completable.fromAction {
            doSomething()
        }.doOnComplete {
            doSomethingElse()
        }

        Completable.fromAction {
            doSomething()
        }.andThen {
            doSomethingElse()
            it.onComplete()
        }
    }

    private fun doSomething() {
        println("Doing...")
    }

    private fun doSomethingElse() {
        println("Doing something else...")
    }
3 Views