简体   繁体   中英

How can I use one reactive stream inside another reactive stream as result of if statement

I have to refactor one method to reactive which returns Enum.VALUE. The method works like a filter from one if statement to another. I made it using Pair and code brake the chain when some if statement returns true (on map() method) or path through all the elements. It works but there are two auxiliary methods which are used inside of map() if statement and they are actually blocking, so I must make them non-blocking. The auxiliary method One returns boolean and It must be called each time when the method called. Here's example code:


public Mono<Enum> method(String param) {
return Mono.just(param)
   .map(
      //filter if-statement
    )
 .map(
      //filter if-statement
    )
 .map(
    pair -> {
       if (auxiliaryMethodOne(param)) {
      //filter if-statement
        }
      }
    )
 .map(
      //filter if-statement
    )
}

private Mono <Boolean> auxiliaryMethodOne(String param) {
    //some logic returns Mono<Boolean>
}

您可以使用Mono#filterWhen

myMono.filterWhen(pair -> auxiliaryMethodOne(param))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM