kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

SharedFlow last() never returns

Open Burtan opened this issue 3 years ago • 2 comments

Hi,

when using SharedFlows the function last() delivered by Reduce.kt through extensions will never return. It collects the flow and suspends until it is completed, but SharedFlows never complete. For intuitive usage last() in SharedFlows should be a shortcut for replayCache.last()

Thanks!

Burtan avatar May 06 '22 06:05 Burtan

Agreed that it must be explicit that last() shouldn't be used in SharedFlow, but instead of having different semantics from Flow.last(), which might be confusing, I'd prefer to have a deprecation error instead, just like it's done on StateFlow.conflate():

@Deprecated(
    level = DeprecationLevel.ERROR,
    message = "Applying 'last' to SharedFlow never returns as SharedFlow never completes normally. Use 'replayCache.last()' for reading the last emitted item in the replay cache.",
    replaceWith = ReplaceWith("replayCache.last()")
)
public fun <T> SharedFlow<T>.last(): T = noImpl()

psteiger avatar May 06 '22 18:05 psteiger

Terminal operators on SharedFlow never return normally.

This is an open question for us whether we want to maintain the set of all terminal operators as deprecated extensions on SharedFlow though

qwwdfsad avatar May 16 '22 07:05 qwwdfsad