Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading docs on MutableStateFlow.emit and MutableStateFlow.tryEmit methods #2561

Closed
rmiller-glrealitylabs opened this issue Mar 2, 2021 · 3 comments
Labels
docs KDoc and API reference

Comments

@rmiller-glrealitylabs
Copy link

The Problem

The documentation for the MutableStateFlow.emit and MutableStateFlow.tryEmit functions are misleading. For example, the docs for MutableStateFlow.tryEmit appear as

/**
* Tries to emit a [value] to this shared flow without suspending. It returns `true` if the value was
* emitted successfully. When this function returns `false`, it means that the call to a plain [emit]
* function will suspend until there is a buffer space available.
*
* A shared flow configured with a [BufferOverflow] strategy other than [SUSPEND][BufferOverflow.SUSPEND]
* (either [DROP_OLDEST][BufferOverflow.DROP_OLDEST] or [DROP_LATEST][BufferOverflow.DROP_LATEST]) never
* suspends on [emit], and thus `tryEmit` to such a shared flow always returns `true`.
*
* This method is **thread-safe** and can be safely invoked from concurrent coroutines without
* external synchronization.
*/

but in fact that method never blocks and always returns true, regardless of the BufferOverflow setting.

Why Does the Problem Exist?

In the source code for the actual definition of MutableStateFlow

override fun tryEmit(value: T): Boolean {
this.value = value
return true
}
override suspend fun emit(value: T) {
this.value = value
}

the tryEmit and emit methods are overridden so that (as far as I can tell) they never suspend.

However, when the MutableStateFlow interface is defined, it inherits from the StateFlow and MutableSharedFlow interfaces:

public interface MutableStateFlow<T> : StateFlow<T>, MutableSharedFlow<T> {

The tryEmit and emit functions inherit the documentation from MutableSharedFlow despite having their behavior changed in the actual implementation of MutableStateFlow

A Possible Solution:

Add the overridden functions to the MutableSharedState interface with accurate documentation.

@elizarov elizarov added the docs KDoc and API reference label Mar 3, 2021
@qwwdfsad
Copy link
Contributor

Fixed with #2560

@trevorhackman
Copy link

This still seems a bit confusing. Does this mean there's no reason to ever use emit with a StateFlow, you can always use tryEmit and get the same functionality with less restrictions?

@Burtan
Copy link

Burtan commented Dec 24, 2022

Is there any reason technical restriction that emit does not suspend until all collectors finished? Always using a SharedFlow when you want this functionality is a little cumbersome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

5 participants