mirror of
				https://github.com/InsanusMokrassar/MicroUtils.git
				synced 2025-11-04 06:00:22 +00:00 
			
		
		
		
	Compare commits
	
		
			1 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 3e24cf1768 | 
@@ -2,6 +2,8 @@
 | 
			
		||||
 | 
			
		||||
## 0.16.3
 | 
			
		||||
 | 
			
		||||
* `Coroutines`:
 | 
			
		||||
    * Create `launchInCurrentThread`
 | 
			
		||||
* `Startup`:
 | 
			
		||||
    * `Launcher`:
 | 
			
		||||
        * All starting API have been moved into `StartLauncherPlugin` and do not require serialize/deserialize cycle for now
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@ kotlin {
 | 
			
		||||
            dependencies {
 | 
			
		||||
                api libs.kt.coroutines.android
 | 
			
		||||
            }
 | 
			
		||||
            dependsOn(jvmMain)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,9 @@
 | 
			
		||||
package dev.inmo.micro_utils.coroutines
 | 
			
		||||
 | 
			
		||||
import kotlinx.coroutines.CoroutineScope
 | 
			
		||||
import kotlinx.coroutines.Dispatchers
 | 
			
		||||
 | 
			
		||||
fun <T> launchInCurrentThread(block: suspend CoroutineScope.() -> T): T {
 | 
			
		||||
    val scope = CoroutineScope(Dispatchers.Unconfined)
 | 
			
		||||
    return scope.launchSynchronously(block)
 | 
			
		||||
}
 | 
			
		||||
@@ -6,7 +6,7 @@ fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T
 | 
			
		||||
    var result: Result<T>? = null
 | 
			
		||||
    val objectToSynchronize = Object()
 | 
			
		||||
    synchronized(objectToSynchronize) {
 | 
			
		||||
        launch {
 | 
			
		||||
        launch(start = CoroutineStart.UNDISPATCHED) {
 | 
			
		||||
            result = safelyWithResult(block)
 | 
			
		||||
        }.invokeOnCompletion {
 | 
			
		||||
            synchronized(objectToSynchronize) {
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,47 @@
 | 
			
		||||
package dev.inmo.micro_utils.coroutines
 | 
			
		||||
 | 
			
		||||
import kotlinx.coroutines.Dispatchers
 | 
			
		||||
import kotlinx.coroutines.delay
 | 
			
		||||
import kotlinx.coroutines.withContext
 | 
			
		||||
import kotlin.test.Test
 | 
			
		||||
import kotlin.test.assertEquals
 | 
			
		||||
 | 
			
		||||
class LaunchInCurrentThreadTests {
 | 
			
		||||
    @Test
 | 
			
		||||
    fun simpleTestThatLaunchInCurrentThreadWorks() {
 | 
			
		||||
        val expectedResult = 10
 | 
			
		||||
        val result = launchInCurrentThread {
 | 
			
		||||
            expectedResult
 | 
			
		||||
        }
 | 
			
		||||
        assertEquals(expectedResult, result)
 | 
			
		||||
    }
 | 
			
		||||
    @Test
 | 
			
		||||
    fun simpleTestThatSeveralLaunchInCurrentThreadWorks() {
 | 
			
		||||
        val testData = 0 until 100
 | 
			
		||||
 | 
			
		||||
        testData.forEach {
 | 
			
		||||
            val result = launchInCurrentThread {
 | 
			
		||||
                it
 | 
			
		||||
            }
 | 
			
		||||
            assertEquals(it, result)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    @Test
 | 
			
		||||
    fun simpleTestThatLaunchInCurrentThreadWillCorrectlyHandleSuspensionsWorks() {
 | 
			
		||||
        val testData = 0 until 100
 | 
			
		||||
 | 
			
		||||
        suspend fun test(data: Any): Any {
 | 
			
		||||
            return withContext(Dispatchers.Default) {
 | 
			
		||||
                delay(1)
 | 
			
		||||
                data
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        testData.forEach {
 | 
			
		||||
            val result = launchInCurrentThread {
 | 
			
		||||
                test(it)
 | 
			
		||||
            }
 | 
			
		||||
            assertEquals(it, result)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user