Failed to subscribe thenMany item

这一生的挚爱 提交于 2019-12-02 09:37:54

问题


When I use Mono.thenMany, the Flux data is lost, why?

@Test
fun thenManyLostFluxDataTest() {
  Mono.empty<Int>()
    .thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
    .subscribe { println(it) } // why not output item 1, 2
}

If I change to use blockLast() to do the subscribe, the test method run forever. So fearful:

@Test
fun thenManyRunForeverTest() {
  Mono.empty<Int>()
    .thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
    .blockLast() // why run forever
}

Now I use another way to do what the thenMany method should do:

// 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

回答1:


You are using Kotlin's "lambda as a last parameter" short-form syntax. The thing is, if you look at the thenMany method signature, it doesn't accept a Function, but a Publisher.

So why is that lambda accepted, and what does it represent?

It seems to be in fact interpreted as a Publisher (as it has only 1 method, subscribe(Subscriber))!

Replace the { } with ( ) and everything will be back to normal.



来源:https://stackoverflow.com/questions/51837156/failed-to-subscribe-thenmany-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!