https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

sasikanth

03/08/2020, 1:14 PM
Hello everyone. Is there a alternative to
ObservableTransformer
when using
Flow
or
Channel
?
p

Paul Woitaschek

03/08/2020, 6:56 PM
An extension function
s

streetsofboston

03/08/2020, 7:03 PM
For Flows, transformers, or just custom operators, look more or less like this:
Copy code
fun <T> Flow<T>.myTransformer(...) = flow {
    ...
        this.collect {
            ...
            ... do your stuff whatever it is ...
                emit(it)
            ...
        }
    ...
}
You can add loops, flow control, etc, whatever you want
p

Paul Woitaschek

03/08/2020, 7:06 PM
That's a different concept. An ObservableTransformer is more like an extension function @streetsofboston
s

streetsofboston

03/08/2020, 7:08 PM
The example I gave is an extension function. And Rx Transformers can be used to create your own custom Rx operators. And my example is a custom operator on a Flow.
p

Paul Woitaschek

03/08/2020, 7:09 PM
Hm Imo a observable transformer just lets you combine existing operators
s

streetsofboston

03/08/2020, 9:34 PM
You can just combine other operators in the
apply
of the Transformer, like you said, but you can do a lot more funky stuff as well. 🙂
s

sasikanth

03/09/2020, 2:19 AM
Oh yeah, this makes a lot of sense. I was trying to find a 1:1 replacement for
ObservableTransformer
. Thanks 👍🏽
My use case was mainly implementing the transformer as a interface for a object/class. But extension functions also works I think.
31 Views