-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Unexpected tryEmit behaviour #2387
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
Comments
Have a look at #2346 |
It's not a bug, it's a feature... :) |
|
@elizarov I would like to renew discussion about this one. First of all, the documentation about default implementation of I'd at least suggest to either move that piece of documentation above the function Second, the default implementation of So I'd suggest to remove the default parameter values at least for What do you think about these suggestions? Also, what do other devs think? |
Just my 2 cents. I'm fairly use to Ctrl+Clicking up the inheritance hierarchy to find docs but maybe that's me. Some links could be nice I guess. When I first used mutable shared flow, I 100% expected that behaviour, at some point I went to check the docs to confirm that it was like this. The fact that I personally prefer if all the arguments are zero by default, if I didn't ask for buffer capacity, don't give me any, seems fair. I hate the fact that in the I feel the only people who might get confused with flow default behaviour, are those coming from some other reactive library and not carefully reading docs/making assumptions. But idk about this. There haven't been too many complaints about it. |
I think people who are new to coroutines and Kotlin Flow in general would be confused about this as well. I myself was surprised when I encountered it first (and I'm not even new to reactive frameworks or coroutines) and so was a colleague of mine. I think this is confusing to anyone with little experience.
You're describing different situation. I am not suggesting to define non-zero default. I'm suggesting to force developer to select values by themselves, to force them to think about it. In my projects a sensible default might be This might not serve everyone, so I don't want this to be the default, but at least to me, I NEVER want |
The above discussion definitely and proposal definitely has merit, but I would like to point out one key observation. The whole conundrum centers around |
I have used coroutines, flows and channels a lot. And still this issue with PS: I'm using |
Looking at this again I guess one could argue that There's a general lack of "rendezvous" when it comes to |
One more solution (I don't like it but still) might be to just crash It is far to easy to just forget to set |
This! |
Same problem here even I used coroutine for several years😂 |
Got bitten by this, I wish the default value for |
Got bitten by this too. I prefer When the default I notificed that - if (bufferSize >= bufferCapacity && minCollectorIndex <= replayIndex) {
when (onBufferOverflow) {
- BufferOverflow.SUSPEND -> return false // will suspend
BufferOverflow.DROP_LATEST -> return true // just drop incoming
BufferOverflow.DROP_OLDEST -> {} // force enqueue & drop oldest instead
}
} Since the default |
Maybe the public fun <T> MutableSharedFlow(
replay: Int = 0,
extraBufferCapacity: Int = 1,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): MutableSharedFlow<T> |
@geek5nan, yes, it should be. but it should be 1 by default, otherwise people encounter unexpected errors like these. |
I just got bitten by this, spend a lot of time figuring out why tryEmit() doesn't work. I think it should be documented properly or throw exception if trying to do tryEmit() with in proper setup because it basically unusable |
Coroutines and Flows are amazing I love them and many many thanks for them 🤩🤩🤩🤩 You guys in JetBrains rock 🤘🤘🤘 🚀🚀🚀 I have a small suggestion: According to this big discussion: Kotlin#2387 and a lot of confusion, including myself, default value of `extraBufferCapacity` should be 1 instead of 0 and I think this parameter also needs better explanation
tryEmit
doesn't attempt to emit a value after first subscriber joined and returnsfalse
.Setting
replay
orextraBufferCapacity
> 0 or replacingtryEmit
byemit
resolves the issueThe text was updated successfully, but these errors were encountered: