Mono <Boolean> 列表中的任何匹配项
anyMatch on list of Mono<Boolean>
我从响应中得到 Mono<User>
,用户有一个订阅列表,我必须检查(通过另一个 api)是否至少有一个符合条件,然后基于那个我必须发送进一步的事件。
Mono<User> user = userFacade.getUser();
user.flatMap(this::hasSub)
.flatMap(this::sendEvent(Pair.getLeft, Pair.getRight))
Mono<Pair<User,Boolean>> hasSub(User user) {
Stream<Mono<Boolean>> listOfBooleans= user.getSubscriptions.stream
.map(sub -> SubFacade.isPrioSub(sub.getId))//SubFacade returns Mono<Boolean>
//At this point I am stuck, I need to check if at least one of Stream<Mono<Boolean>> is true, but anyMatch() is arguing about passing Mono<Boolean> into it
//also to optimize requests it would be nice to have analogue of break to exit iteration once we get Mono<true>, but dropWith() also is not accepting Mono<Boolean>
return Mono.just(Pair.of(user, 'get boolean from listOfBooleans'))
}
知道如何解决这个问题吗?
Mono<Pair<User, Boolean>> hasSub(User user) {
return Flux.fromIterable(user.getSubscriptions())
.filterWhen(sub -> SubFacade.isPrioSub(sub.getId()))
.next() // takes the first element passing the filter
.map(x -> Pair.of(user, true))
.defaultIfEmpty(Pair.of(user, false));
}
注意:这将顺序检查给定用户的订阅并在第一个满意的项目处停止。如果需要并发,则 filterWhen
应替换为 flatMap
和 filter
.
我从响应中得到 Mono<User>
,用户有一个订阅列表,我必须检查(通过另一个 api)是否至少有一个符合条件,然后基于那个我必须发送进一步的事件。
Mono<User> user = userFacade.getUser();
user.flatMap(this::hasSub)
.flatMap(this::sendEvent(Pair.getLeft, Pair.getRight))
Mono<Pair<User,Boolean>> hasSub(User user) {
Stream<Mono<Boolean>> listOfBooleans= user.getSubscriptions.stream
.map(sub -> SubFacade.isPrioSub(sub.getId))//SubFacade returns Mono<Boolean>
//At this point I am stuck, I need to check if at least one of Stream<Mono<Boolean>> is true, but anyMatch() is arguing about passing Mono<Boolean> into it
//also to optimize requests it would be nice to have analogue of break to exit iteration once we get Mono<true>, but dropWith() also is not accepting Mono<Boolean>
return Mono.just(Pair.of(user, 'get boolean from listOfBooleans'))
}
知道如何解决这个问题吗?
Mono<Pair<User, Boolean>> hasSub(User user) {
return Flux.fromIterable(user.getSubscriptions())
.filterWhen(sub -> SubFacade.isPrioSub(sub.getId()))
.next() // takes the first element passing the filter
.map(x -> Pair.of(user, true))
.defaultIfEmpty(Pair.of(user, false));
}
注意:这将顺序检查给定用户的订阅并在第一个满意的项目处停止。如果需要并发,则 filterWhen
应替换为 flatMap
和 filter
.