Skip to content

Commit 9f307d7

Browse files
Introduce ReusableMainScope factory function
1 parent cf7ec5a commit 9f307d7

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

common/kotlinx-coroutines-core-common/src/CoroutineScope.kt

+4
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,8 @@ fun ReusableCoroutineScope(
185185
): ReusableCoroutineScope =
186186
ReusableContextScope(if (context[Job] != null) context else context + newJob(), newJob)
187187

188+
@Suppress("FunctionName")
189+
fun ReusableMainScope(): CoroutineScope =
190+
ReusableContextScope(Dispatchers.Main) { SupervisorJob() }
191+
188192
public val CoroutineScope.isReusable: Boolean get() = this is ReusableCoroutineScope

common/kotlinx-coroutines-core-common/test/CoroutineScopeTest.kt

+5
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ class CoroutineScopeTest : TestBase() {
323323
assertTrue(reusableScope.isRestarted)
324324
}
325325

326+
@Test
327+
fun testMainReusableScope() {
328+
assertSame(ReusableMainScope().coroutineContext[ContinuationInterceptor], Dispatchers.Main)
329+
}
330+
326331
private fun scopePlusContext(c1: CoroutineContext, c2: CoroutineContext) =
327332
(ContextScope(c1) + c2).coroutineContext
328333

0 commit comments

Comments
 (0)