Needed: suspend fun PublisherScope.awaitDemand()

This quesiton is about kotlinx-coroutines-reactive.

The idea with using suspend functions for backpressure is nice, but the current design has a significant shortcoming: a Kotlin-style publisher must have the next element calculated, and only then can it learn that it’s not needed (via send()). This is a problem for “expensive” publishers: those for which it’s costly to calculate elements, and you only want to do it if there’s demand from upstream.

Example use case: a Publisher backed by polling a DB table. In this case, we need to stop polling while there is no demand from upstream. However, the current API doesn’t provide a good way of doing this; the best you can do is to replace DB polling with polling PublisherScope.isFull.

I’d suggest to add PublisherScope.awaitDemand() method that would suspend until there’s demand from upstream.

I think this issue is what you are looking for.

That discussion touches a lot of things, but none of them are about PublisherScope. I do need a reactive stream to interact with non-Kotlin code compliant to reactive stream spec. So even though a generic Kotlin-based solution is great, I don’t immediately see how it would help here. Thanks for the link though, I like the direction they’re taking.