Skip to content

Commit

Permalink
fix prompt cancellation fallacy Kotlin/kotlinx.coroutines#2886 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek-12 committed Jun 7, 2022
1 parent a740dfb commit 5442266
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions android/src/main/kotlin/com/nek12/flowMVI/android/MVIExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import com.nek12.flowMVI.MVIProvider
import com.nek12.flowMVI.MVIState
import com.nek12.flowMVI.MVISubscriber
import com.nek12.flowMVI.MVIView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* Subscribe to the [provider] lifecycle-aware.
Expand All @@ -28,15 +30,19 @@ inline fun <S: MVIState, I: MVIIntent, A: MVIAction> LifecycleOwner.subscribe(

//using multiple repeatOnLifecycle instead of flowWithLifecycle to avoid creating hot flows

launch {
repeatOnLifecycle(lifecycleState) {
provider.states.collect { render(it) }
//https://github.com/Kotlin/kotlinx.coroutines/issues/2886
//TL;DR: uses immediate dispatcher to circumvent prompt cancellation fallacy (and missed events)
withContext(Dispatchers.Main.immediate) {
launch {
repeatOnLifecycle(lifecycleState) {
provider.states.collect { render(it) }
}
}
}

launch {
repeatOnLifecycle(lifecycleState) {
provider.actions.collect { consume(it) }
launch {
repeatOnLifecycle(lifecycleState) {
provider.actions.collect { consume(it) }
}
}
}
}
Expand Down

0 comments on commit 5442266

Please sign in to comment.