如何避免 .flatMap(x-> reactiveAction(x).thenReturn(x))
how to avoid .flatMap(x-> reactiveAction(x).thenReturn(x))
在 Java 使用项目反应器库进行一些反应式编程时,我偶然发现了一种模式,我想知道是否有开箱即用的支持?
所以我想要下面的代码:
Mono.just("hello")
.flatMap(hello -> reactiveAction(hello).thenReturn(hello))
..
.;
变成类似这样的东西:
Mono.just("hello")
.coolOperation(this::reactiveAction)
..
.;
我不能使用 doOnNext,因为我想在 reactiveAction 中做的不是副作用。
反应动作是:
Mono<Integer> reactiveAction(String text){
return ....
}
我找不到内置解决方案,但您可以创建实用函数:
public static <T> Function<Mono<T>, Publisher<T>> coolOperation(
Function<T, Mono<?>> companionMonoFunction) {
return originalMono -> originalMono
.flatMap(t -> companionMonoFunction.apply(t)
.thenReturn(t));
}
现在您可以将它与 transform
或 transformDeffered
一起使用:
Mono.just("hello")
.transform(coolOperation(this::reactiveAction))
...;
但对我来说,它看起来并不漂亮:)
编辑:查看@bsideup 的回答,看起来 delayUntil
符合要求。
我原来的答案作为备选建议:
I don't think there is any baked-in syntactic sugar to do this, as the "perform an async operation that depends on the original onNext
" is the very definition of flatMap
. We actually added the thenReturn(foo)
as syntactic sugar over .then(Mono.just(foo))
.
If you want to further shorten the code, offer an alternative to reactiveAction
that also returns the original value:
Mono<String> reactiveActionBackground(String text){
return reactiveAction(text).thenReturn(text);
}
which can then be invoked directly on flatMap
rather than through transform
:
Mono.just("hello")
.flatMap(this::reactiveActionBackground);
你考虑过Mono#delayUntil
吗?
Mono.just("hello")
.delayUntil(hello -> reactiveAction(hello))
..
.;
在 Java 使用项目反应器库进行一些反应式编程时,我偶然发现了一种模式,我想知道是否有开箱即用的支持?
所以我想要下面的代码:
Mono.just("hello")
.flatMap(hello -> reactiveAction(hello).thenReturn(hello))
..
.;
变成类似这样的东西:
Mono.just("hello")
.coolOperation(this::reactiveAction)
..
.;
我不能使用 doOnNext,因为我想在 reactiveAction 中做的不是副作用。 反应动作是:
Mono<Integer> reactiveAction(String text){
return ....
}
我找不到内置解决方案,但您可以创建实用函数:
public static <T> Function<Mono<T>, Publisher<T>> coolOperation(
Function<T, Mono<?>> companionMonoFunction) {
return originalMono -> originalMono
.flatMap(t -> companionMonoFunction.apply(t)
.thenReturn(t));
}
现在您可以将它与 transform
或 transformDeffered
一起使用:
Mono.just("hello")
.transform(coolOperation(this::reactiveAction))
...;
但对我来说,它看起来并不漂亮:)
编辑:查看@bsideup 的回答,看起来 delayUntil
符合要求。
我原来的答案作为备选建议:
I don't think there is any baked-in syntactic sugar to do this, as the "perform an async operation that depends on the original
onNext
" is the very definition offlatMap
. We actually added thethenReturn(foo)
as syntactic sugar over.then(Mono.just(foo))
.If you want to further shorten the code, offer an alternative to
reactiveAction
that also returns the original value:Mono<String> reactiveActionBackground(String text){ return reactiveAction(text).thenReturn(text); }
which can then be invoked directly on
flatMap
rather than throughtransform
:Mono.just("hello") .flatMap(this::reactiveActionBackground);
你考虑过Mono#delayUntil
吗?
Mono.just("hello")
.delayUntil(hello -> reactiveAction(hello))
..
.;