Skip to content

Commit cfbb06e

Browse files
committed
refactor: update coroutine dispatcher checks and improve code style settings
1 parent 65a05f1 commit cfbb06e

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

.idea/codeStyles/Project.xml

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package com.hoc.flowmvi.core_ui
22

3-
import kotlin.coroutines.ContinuationInterceptor
3+
import kotlinx.coroutines.CoroutineDispatcher
44
import kotlinx.coroutines.Dispatchers
55
import kotlinx.coroutines.currentCoroutineContext
66
import timber.log.Timber
77

8-
suspend fun debugCheckImmediateMainDispatcher() {
8+
@OptIn(ExperimentalStdlibApi::class)
9+
suspend inline fun debugCheckImmediateMainDispatcher() {
910
if (BuildConfig.DEBUG) {
10-
val interceptor = currentCoroutineContext()[ContinuationInterceptor]
11-
Timber.d("debugCheckImmediateMainDispatcher: interceptor=$interceptor")
11+
val dispatcher = checkNotNull(currentCoroutineContext()[CoroutineDispatcher]) {
12+
"Expected CoroutineDispatcher in current CoroutineContext but was null"
13+
}.also { Timber.d("debugCheckImmediateMainDispatcher: dispatcher=$it") }
1214

13-
check(interceptor === Dispatchers.Main.immediate) {
14-
"Expected ContinuationInterceptor to be Dispatchers.Main.immediate but was $interceptor"
15+
check(
16+
dispatcher === Dispatchers.Main.immediate ||
17+
!dispatcher.isDispatchNeeded(Dispatchers.Main.immediate),
18+
) {
19+
"Expected ContinuationInterceptor to be Dispatchers.Main.immediate but was $dispatcher"
1520
}
1621
}
1722
}

mvi/mvi-base/src/main/java/com/hoc/flowmvi/mvi_base/AbstractMviViewModel.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
77
import com.hoc.flowmvi.core_ui.debugCheckImmediateMainDispatcher
88
import java.util.concurrent.atomic.AtomicInteger
99
import kotlin.LazyThreadSafetyMode.PUBLICATION
10+
import kotlinx.coroutines.ExperimentalForInheritanceCoroutinesApi
1011
import kotlinx.coroutines.channels.Channel
1112
import kotlinx.coroutines.channels.onFailure
1213
import kotlinx.coroutines.flow.Flow
@@ -36,7 +37,7 @@ abstract class AbstractMviViewModel<I : MviIntent, S : MviViewState, E : MviSing
3637
}
3738

3839
private val eventChannel = Channel<E>(Channel.UNLIMITED)
39-
private val intentMutableFlow = MutableSharedFlow<I>(extraBufferCapacity = SubscriberBufferSize)
40+
private val intentMutableFlow = MutableSharedFlow<I>(extraBufferCapacity = SUBSCRIBER_BUFFER_SIZE)
4041

4142
final override val singleEvent: Flow<E> = eventChannel.receiveAsFlow()
4243

@@ -85,6 +86,8 @@ abstract class AbstractMviViewModel<I : MviIntent, S : MviViewState, E : MviSing
8586
if (BuildConfig.DEBUG) {
8687
val self = this
8788

89+
@Suppress("UnnecessaryOptInAnnotation")
90+
@OptIn(ExperimentalForInheritanceCoroutinesApi::class)
8891
object : SharedFlow<T> by self {
8992
val subscriberCount = AtomicInteger(0)
9093

@@ -125,7 +128,7 @@ abstract class AbstractMviViewModel<I : MviIntent, S : MviViewState, E : MviSing
125128
* The internally allocated buffer is replay + extraBufferCapacity but always allocates 2^n space.
126129
* We use replay=0 so buffer = 64.
127130
*/
128-
private const val SubscriberBufferSize = 64
131+
private const val SUBSCRIBER_BUFFER_SIZE = 64
129132

130133
private const val MAX_TAG_LENGTH = 23
131134
}

0 commit comments

Comments
 (0)