-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Main.immediate dispatcher with fallback to Main dispatcher
- Loading branch information
Showing
5 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...utines/src/commonMain/kotlin/com/arkivanov/essenty/lifecycle/coroutines/DispatchersExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.arkivanov.essenty.lifecycle.coroutines | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
import kotlin.concurrent.Volatile | ||
|
||
@Volatile | ||
private var isImmediateSupported: Boolean = true | ||
|
||
@Suppress("UnusedReceiverParameter") | ||
internal val MainCoroutineDispatcher.immediateOrFallback: MainCoroutineDispatcher | ||
get() { | ||
if (isImmediateSupported) { | ||
try { | ||
return Dispatchers.Main.immediate | ||
} catch (ignored: UnsupportedOperationException) { | ||
} catch (ignored: NotImplementedError) { | ||
} | ||
|
||
isImmediateSupported = false | ||
} | ||
|
||
return Dispatchers.Main | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...es/src/commonTest/kotlin/com/arkivanov/essenty/lifecycle/coroutines/DispatchersExtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.arkivanov.essenty.lifecycle.coroutines | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.StandardTestDispatcher | ||
import kotlinx.coroutines.test.TestDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.setMain | ||
import kotlin.test.* | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@Suppress("TestFunctionName") | ||
class DispatchersExtTest { | ||
|
||
@AfterTest | ||
fun after() { | ||
Dispatchers.resetMain() | ||
} | ||
|
||
@Test | ||
fun WHEN_immediateOrDefault_called_multiple_times_THEN_returns_same_dispatcher() { | ||
val dispatcher1 = Dispatchers.Main.immediateOrFallback | ||
val dispatcher2 = Dispatchers.Main.immediateOrFallback | ||
|
||
assertSame(dispatcher1, dispatcher2) | ||
} | ||
|
||
@Test | ||
fun GIVEN_Main_dispatcher_changed_WHEN_immediateOrDefault_called_THEN_returns_updated_dispatcher() { | ||
val oldDispatcher = Dispatchers.Main.immediateOrFallback | ||
val testDispatcher = StandardTestDispatcher() | ||
Dispatchers.setMain(testDispatcher) | ||
|
||
val newDispatcher = Dispatchers.Main.immediateOrFallback | ||
|
||
assertNotSame(oldDispatcher, newDispatcher) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters