I have a Flux of data that I want to transform both in images and in reports. Because I want to reuse the same source of data, I thought of using thepublish method on Flux and concatenate the results like in the code below:
@Test
fun `inside a publish, I can concat multiple fluxes`() {
data class Data(val d: String)
data class Image(val i: String)
data class Report(val r: String)
val result = Flux.just(Data("some data"))
.publish { fluxOfData ->
val fod = fluxOfData //.cache()
val images = fod.flatMap { Flux.just(Image("my image")) }
val reports = fod.flatMap { Flux.just(Report("my report")) }
Flux.concat(images, reports)
}
.collectList()
.block()
Assertions.assertEquals(result, listOf(Image("my image"), Report("my report")))
}
However, the above code only sends [Image(i=my image)] so it's like the fluxOfData is consumed the first time. However I expected that I would be able to subscribe to fluxOfData multiple times.
When caching the fluxOfData by calling fluxOfData.cache(), the results come out as expected.
So what's the use case of Flux.publish()?
Is there another idiomatic way to transform the data into both images and reports?