When using reactive frameworks I have seen both solutions below to make a mutually exclusive selection of which stream will be forwarded (rxjava in this case)
Observable.merge(
Observable.just(aBoolStatement).filter(aBoolean -> aBoolean).flatMap(aBoolean -> signal1),
Observable.just(aBoolStatement).filter(aBoolean -> !aBoolean).flatMap(aBoolean -> signal2)
)
-
Observable.just(aBoolStatement).flatMap(aBoolean -> {
if(aBoolean)
return signal1
else
return signal2
}
The first code is all rx which is nice but it also evaluates the statement twice which is easy to miss if you would ever need to update the statement. Is there a preferred way of doing this or any other way to achieve the same result?