Skip to content

ConflatedBroadcastChannel losing most recent Event #969

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

Closed
ScottPierce opened this issue Feb 5, 2019 · 1 comment
Closed

ConflatedBroadcastChannel losing most recent Event #969

ScottPierce opened this issue Feb 5, 2019 · 1 comment

Comments

@ScottPierce
Copy link
Contributor

ScottPierce commented Feb 5, 2019

I'm using kotlinx.coroutines version 1.1.1, and Kotlin 1.3.20.

I'm using a ConflatedBroadcastChannel to send View events in Android (from the main thread) to the presenter that that receives them (also on the main thread).

The documentation for ConflatedBroadcastChannel says:

only the the most recently sent value is received, while previously sent elements are lost.

In this case, the reverse is actually true. The most recent value is lost, while previously sent values make it through.

Relevant View code:

val broadcastChannel: BroadcastChannel<AuthEmailEvent> = ConflatedBroadcastChannel()
val selectedCredential = state.previousCredentials[position]
broadcastChannel.offer(AuthEmailEvent.EmailUpdate(selectedCredential.first))
broadcastChannel.offer(AuthEmailEvent.PasswordUpdate(selectedCredential.second))
actual fun events(): ReceiveChannel<AuthEmailEvent> = broadcastChannel.openSubscription()

Relevant Presenter code:

for (event in view.events()) {
  when (event) {
    is AuthEmailEvent.EmailUpdate -> state = state.copy(
      email = event.email,
      isEmailValid = !event.email.isBlank()
    )
    is AuthEmailEvent.PasswordUpdate -> state = state.copy(
      password = event.password,
      isPasswordValid = !event.password.isBlank()
    )
  }
}

The first event, AuthEmailEvent.EmailUpdate is received and processed, but the AuthEmailEvent.PasswordUpdate, which is the most recent event is lost, and never received.

@ScottPierce
Copy link
Contributor Author

After further debugging, I realized that the first event triggers more offered events. That's what's causing the Password event to be lost.

I've instead expressed my support for an Unlimited BroadcastChannel:
#736 (comment)

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

No branches or pull requests

1 participant