简体   繁体   English

作为if语句的结果,我如何在另一个反应流中使用一个反应流

[英]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. 我必须将一种方法重构为可响应的方法,该方法返回Enum.VALUE。 The method works like a filter from one if statement to another. 该方法就像从一个if语句到另一个if过滤器一样工作。 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. 当某些if语句返回true(在map()方法上)或通过所有元素的路径时,我使用Pair进行了编码并制动了链。 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. 它可以工作,但是map()if语句内部使用了两种辅助方法,它们实际上是阻塞的,因此我必须使它们成为非阻塞的。 The auxiliary method One returns boolean and It must be called each time when the method called. 辅助方法One返回布尔值,并且每次调用该方法时都必须调用它。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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