```fun resetService(service: Service, usage: Usage...
# rx
u
Copy code
fun resetService(service: Service, usage: Usage): Single<Foo> =
        getSubscriber()
            .map { it.subscriber }
            .flatMap { subscriber ->
                Single
                    .fromCallable {
                        val credit = usage.credit ?: 0.0
                        if (subscriber.type == Type.CREDIT && creditAmount < service.resetPrice) {
                            throw InsufficientCreditException(creditAmount, service.resetPrice)
                        }

                        val product = getProduct(cache, service.productId)
                        val usageManagement = product?.usageManagement ?: error("UsageManagent is null")
                        val resetProductId = product.resetProductIdOverride ?: usageManagement.resetProductId

                        tupleOf(usageManagement.action, resetProductId)
                    }
                    .doOnSuccess {
                        store.setProcessingSubscriber(subscriber.id, isProcessing = true)
                    }
                    .flatMap { (action, resetProductId) ->
                        setService(action, resetProductId, null, null, null, null)
                    }
                    .doOnErrorOrCancel {
                        store.setProcessingSubscriber(subscriber.id, isProcessing = false)
                    }
            }
            .doOnSuccess { updateManager.enqueueUpdate() }
p
Why are you wrapping {val credit = ....} Into single to flatmap it immediately ?
u
well, thats kind of my point, its too mucj stuff which hides the intent. essentially you are correct its not needed since its synchronous
however, splitting it like that does make each part cancelable
whereas if it was all one big synch block, the block would always continue until completion
p
good point at cancelability, but in that particular case isn't ::map enough? it will be still a node in rx chain keeping process flow in smaller steps. Just guessing, I don't know deeper details of cancelation mechanism 😆
u
well, since its synchronous it doesnt matter