Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
12.84k stars 1.83k forks source link

SharedFlow last() never returns #3275

Open Burtan opened 2 years ago

Burtan commented 2 years ago

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!

psteiger commented 2 years ago

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()
qwwdfsad commented 2 years ago

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