[Proposal] Primitive or Channel that guarantees the delivery and processing of items #2886
Description
Due to the prompt cancellation guarantee changes that landed in Coroutines 1.4, Channels cannot be used as mechanism that guarantees delivery and acknowledges successful processing between producer and consumer in order to guarantee that an item was handled exactly once.
There's a need for another primitive (or improvements to Channel) that also guarantees that the item was successfully processed by the consumer.
Use case: UI Events in Android
In Android development, ViewModel-like classes sometimes need to inform the View that it needs to perform an action. For example, showing a Toast. This is needed, for example, when stateful Views don't expose its internal state—as it happens with most View APIs.
Channels cannot be used as a way to communicate these types of events to the View because those events could be lost under certain circumstances. That is: 1) the producer (ViewModel) sends the event, 2) the consumer (View) receives the event which is scheduled for dispatch, but then, 3) the consumer is cancelled. The event was received but never processed.
The onUndelivered
handler support recently added to Channels doesn't work in this case because it messes up with the events ordering.