英文:
how to avoid .flatMap(x-> reactiveAction(x).thenReturn(x))
问题
在使用Project Reactor库进行Java中的一些响应式编程时,我遇到了一种模式,想知道是否有现成的支持?
所以我希望将下面的代码:
Mono.just("hello")
.flatMap(hello -> reactiveAction(hello).thenReturn(hello))
..
.;
转换成类似这样的形式:
Mono.just("hello")
.coolOperation(this::reactiveAction)
..
.;
我不能使用doOnNext,因为我想在reactiveAction内部执行的内容不是副作用。
而reactiveAction的定义是:
Mono<Integer> reactiveAction(String text){
return ....
}
英文:
During some reactive programming in Java using project reactor library, I stumbled upon a pattern for which I'm wondering if there is out of box support?
So I want the code below:
Mono.just("hello")
.flatMap(hello -> reactiveAction(hello).thenReturn(hello))
..
.;
to be turned into something like:
Mono.just("hello")
.coolOperation(this::reactiveAction)
..
.;
I can not use doOnNext because what I want to do inside reactiveAction is not side effect.
and reactive action is:
Mono<Integer> reactiveAction(String text){
return ....
}
答案1
得分: 9
你考虑过 Mono#delayUntil 吗?
Mono.just("hello")
.delayUntil(hello -> reactiveAction(hello))
..
.;
英文:
Have you considered Mono#delayUntil?
Mono.just("hello")
.delayUntil(hello -> reactiveAction(hello))
..
.;
答案2
得分: 1
我找不到内置的解决方案,但你可以创建实用函数:
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))
...;
但对我来说,它看起来不太美观 :)
英文:
I can't find built-in solution, but you could create utility function:
public static <T> Function<Mono<T>, Publisher<T>> coolOperation(
Function<T, Mono<?>> companionMonoFunction) {
return originalMono -> originalMono
.flatMap(t -> companionMonoFunction.apply(t)
.thenReturn(t));
}
And now you can use it with transform or transformDeffered:
Mono.just("hello")
.transform(coolOperation(this::reactiveAction))
...;
But for me it doesn't look much prettier ![]()
答案3
得分: 1
EDIT: 请看 @bsideup 的回答,似乎 delayUntil 可以满足要求。
作为另一种替代建议的我的原始回答:
> 我认为没有内置的语法糖可以做到这一点,因为“执行一个依赖于原始 onNext 的异步操作”恰恰就是 flatMap 的定义。实际上,我们添加了 thenReturn(foo) 作为 .then(Mono.just(foo)) 的语法糖。
>
> 如果你想进一步缩短代码,可以为 reactiveAction 提供另一种返回原始值的替代方案:
>
> Mono<String> reactiveActionBackground(String text){
> return reactiveAction(text).thenReturn(text);
> }
>
> 然后可以直接在 flatMap 上调用它,而不是通过 transform:
>
> Mono.just("hello")
> .flatMap(this::reactiveActionBackground);
英文:
EDIT: see @bsideup answer, looks like delayUntil could fit the bill.
My original answer as an alternative suggestion:
> 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);
专注分享java语言的经验与见解,让所有开发者获益!

评论