0

I am using Mono's filterWhen() with multiple conditions like below but don't know how to implement the correct way.

Mono.just("a").filterWhen(item -> conditionA && conditionB)...
// or Mono.just("a").filterWhen(item -> conditionA || conditionB)...

// both is reactive call
Mono<Boolean> conditionA(String a) {
     return webClient.call(....).map(Boolean);//sample return Boolean here}
Mono<Boolean> conditionB(String a) {
     return webClient.call(....).map(Boolean);//sample return Boolean here}

When use conditionA && conditionB, is it imediately return when conditionA (or conditionB) return false?

Also when use conditionA || conditionB, is it imediately return when conditionA (or conditionB) return true?

lkatiforis
  • 5,703
  • 2
  • 16
  • 35

1 Answers1

1

The operators Flux#all and Flux#any are what you are looking for.

First, merge the responses to a Flux of Boolean and use Flux#all to emit a single boolean true if all responses are evaluated to true:

Flux.merge(conditionA(), conditionB()).all(condition -> condition)

In this way, if conditionA is false the value false is emitted without waiting for the contitionB to be evaluated.

As a side note, Flux.merge subscribes to the publishers eagerly (all publishers are subscribed together). If you like to execute the conditions sequentially use Flux.concat instead.

lkatiforis
  • 5,703
  • 2
  • 16
  • 35