RJ.*_*ang 1 kotlin project-reactor
当我使用 时Mono.thenMany,Flux 数据丢失,为什么?
@Test
fun thenManyLostFluxDataTest() {
Mono.empty<Int>()
.thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
.subscribe { println(it) } // why not output item 1, 2
}
Run Code Online (Sandbox Code Playgroud)
如果我更改为使用blockLast()进行订阅,则测试方法将永远运行。好害怕:
@Test
fun thenManyRunForeverTest() {
Mono.empty<Int>()
.thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
.blockLast() // why run forever
}
Run Code Online (Sandbox Code Playgroud)
现在我使用另一种方式来完成该thenMany方法应该做的事情:
// this method output item 1, 2
@Test
fun flatMapIterableTest() {
Mono.empty<Int>()
.then(Mono.just(listOf(1, 2)))
.flatMapIterable { it.asIterable() }
.subscribe { println(it) } // output item 1, 2 correctly
}ed
Run Code Online (Sandbox Code Playgroud)
您正在使用 Kotlin 的“lambda 作为最后一个参数”短格式语法。问题是,如果你查看thenMany方法签名,它不接受 a Function,而是接受Publisher.
那么为什么 lambda 被接受,它代表什么?
它似乎实际上被解释为 a Publisher(因为它只有 1 个方法,subscribe(Subscriber))!
更换{ }为( ),一切都会恢复正常。
| 归档时间: |
|
| 查看次数: |
310 次 |
| 最近记录: |