Skip to content

Commit 5442266

Browse files
committed
fix prompt cancellation fallacy Kotlin/kotlinx.coroutines#2886 (comment)
1 parent a740dfb commit 5442266

File tree

1 file changed

+13
-7
lines changed
  • android/src/main/kotlin/com/nek12/flowMVI/android

1 file changed

+13
-7
lines changed

android/src/main/kotlin/com/nek12/flowMVI/android/MVIExt.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import com.nek12.flowMVI.MVIProvider
1010
import com.nek12.flowMVI.MVIState
1111
import com.nek12.flowMVI.MVISubscriber
1212
import com.nek12.flowMVI.MVIView
13+
import kotlinx.coroutines.Dispatchers
1314
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.withContext
1416

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

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

31-
launch {
32-
repeatOnLifecycle(lifecycleState) {
33-
provider.states.collect { render(it) }
33+
//https://github.com/Kotlin/kotlinx.coroutines/issues/2886
34+
//TL;DR: uses immediate dispatcher to circumvent prompt cancellation fallacy (and missed events)
35+
withContext(Dispatchers.Main.immediate) {
36+
launch {
37+
repeatOnLifecycle(lifecycleState) {
38+
provider.states.collect { render(it) }
39+
}
3440
}
35-
}
3641

37-
launch {
38-
repeatOnLifecycle(lifecycleState) {
39-
provider.actions.collect { consume(it) }
42+
launch {
43+
repeatOnLifecycle(lifecycleState) {
44+
provider.actions.collect { consume(it) }
45+
}
4046
}
4147
}
4248
}

0 commit comments

Comments
 (0)