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

ursus

02/13/2020, 2:44 AM
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

pg

02/25/2020, 6:37 AM
Why are you wrapping {val credit = ....} Into single to flatmap it immediately ?
u

ursus

02/26/2020, 3:47 AM
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

pg

02/26/2020, 9:22 AM
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

ursus

02/26/2020, 8:47 PM
well, since its synchronous it doesnt matter