mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-18 14:59:24 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
8b43d785cc | |||
b62d3a0b7d | |||
fad73c7213 | |||
2403c7c2b0 | |||
204955bcce |
@@ -1,5 +1,10 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.16.4
|
||||||
|
|
||||||
|
* `Coroutines`:
|
||||||
|
* Create `launchInCurrentThread`
|
||||||
|
|
||||||
## 0.16.3
|
## 0.16.3
|
||||||
|
|
||||||
* `Startup`:
|
* `Startup`:
|
||||||
|
@@ -22,6 +22,7 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
api libs.kt.coroutines.android
|
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
|
var result: Result<T>? = null
|
||||||
val objectToSynchronize = Object()
|
val objectToSynchronize = Object()
|
||||||
synchronized(objectToSynchronize) {
|
synchronized(objectToSynchronize) {
|
||||||
launch {
|
launch(start = CoroutineStart.UNDISPATCHED) {
|
||||||
result = safelyWithResult(block)
|
result = safelyWithResult(block)
|
||||||
}.invokeOnCompletion {
|
}.invokeOnCompletion {
|
||||||
synchronized(objectToSynchronize) {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.16.3
|
version=0.16.4
|
||||||
android_code_version=171
|
android_code_version=172
|
||||||
|
10
startup/template/README.md
Normal file
10
startup/template/README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# How to use
|
||||||
|
|
||||||
|
In case you have multiplatform project and wish to use startup plugin, this template may help you to create new modules.
|
||||||
|
|
||||||
|
1. Copy-paste whole template folder (you may clone this folder to your project and actualize some data to copy your prepared template)
|
||||||
|
2. Replace `group_name` by your project (or root module) group name
|
||||||
|
3. Replace `module_name` by the name of your new module name
|
||||||
|
|
||||||
|
You may read about the `build.gradle` structure in these templates in project
|
||||||
|
[KotlinMultiplatformProjectTemplate](https://github.com/InsanusMokrassar/KotlinMultiplatformProjectTemplate).
|
18
startup/template/client/build.gradle
Normal file
18
startup/template/client/build.gradle
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
alias(libs.plugins.compose)
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":${rootProject.name}.module_name.common")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package group_name.module_name.client
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object ClientPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
20
startup/template/client/src/jsMain/kotlin/ClientJSPlugin.kt
Normal file
20
startup/template/client/src/jsMain/kotlin/ClientJSPlugin.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package group_name.module_name.client
|
||||||
|
|
||||||
|
import group_name.module_name.common.CommonJSPlugin
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object ClientJSPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with(CommonJSPlugin) { setupDI(config) }
|
||||||
|
with(ClientPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonJSPlugin.startPlugin(koin)
|
||||||
|
ClientPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,9 @@
|
|||||||
|
package group_name.module_name.client
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.createStartupPluginAndRegister
|
||||||
|
|
||||||
|
@ExperimentalStdlibApi
|
||||||
|
@EagerInitialization
|
||||||
|
@JsExport
|
||||||
|
@ExperimentalJsExport
|
||||||
|
private val jsModuleLoader = createStartupPluginAndRegister("template.ClientJSPlugin") { ClientJSPlugin }
|
@@ -0,0 +1,21 @@
|
|||||||
|
package group_name.module_name.client
|
||||||
|
|
||||||
|
import group_name.module_name.common.CommonJVMPlugin
|
||||||
|
import group_name.module_name.common.CommonJVMPlugin.setupDI
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object ClientJVMPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with(CommonJVMPlugin) { setupDI(config) }
|
||||||
|
with(ClientPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonJVMPlugin.startPlugin(koin)
|
||||||
|
ClientPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
1
startup/template/client/src/main/AndroidManifest.xml
Normal file
1
startup/template/client/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="group_name.module_name.client"/>
|
@@ -0,0 +1,21 @@
|
|||||||
|
package group_name.module_name.client
|
||||||
|
|
||||||
|
import group_name.module_name.common.CommonAndroidPlugin
|
||||||
|
import group_name.module_name.common.CommonAndroidPlugin.setupDI
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object ClientAndroidPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with(CommonAndroidPlugin) { setupDI(config) }
|
||||||
|
with(ClientPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonAndroidPlugin.startPlugin(koin)
|
||||||
|
ClientPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
7
startup/template/common/build.gradle
Normal file
7
startup/template/common/build.gradle
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppProjectWithSerializationPresetPath"
|
@@ -0,0 +1,11 @@
|
|||||||
|
package group_name.module_name.common
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object CommonPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1 @@
|
|||||||
|
package group_name.module_name.common
|
17
startup/template/common/src/jsMain/kotlin/CommonJSPlugin.kt
Normal file
17
startup/template/common/src/jsMain/kotlin/CommonJSPlugin.kt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package group_name.module_name.common
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object CommonJSPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with (CommonPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,9 @@
|
|||||||
|
package group_name.module_name.common
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.createStartupPluginAndRegister
|
||||||
|
|
||||||
|
@ExperimentalStdlibApi
|
||||||
|
@EagerInitialization
|
||||||
|
@JsExport
|
||||||
|
@ExperimentalJsExport
|
||||||
|
private val jsModuleLoader = createStartupPluginAndRegister("template.CommonJSPlugin") { CommonJSPlugin }
|
@@ -0,0 +1,17 @@
|
|||||||
|
package group_name.module_name.common
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object CommonJVMPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with (CommonPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
1
startup/template/common/src/main/AndroidManifest.xml
Normal file
1
startup/template/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="group_name.module_name.common"/>
|
@@ -0,0 +1,17 @@
|
|||||||
|
package group_name.module_name.common
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object CommonAndroidPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with (CommonPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
16
startup/template/server/build.gradle
Normal file
16
startup/template/server/build.gradle
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppJavaProjectPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":${rootProject.name}.module_name.common")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package group_name.module_name.server
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object ServerPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,20 @@
|
|||||||
|
package group_name.module_name.server
|
||||||
|
|
||||||
|
import group_name.module_name.common.CommonJVMPlugin
|
||||||
|
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object ServerJVMPlugin : StartPlugin {
|
||||||
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
|
with(CommonJVMPlugin) { setupDI(config) }
|
||||||
|
with(ServerPlugin) { setupDI(config) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startPlugin(koin: Koin) {
|
||||||
|
super.startPlugin(koin)
|
||||||
|
CommonJVMPlugin.startPlugin(koin)
|
||||||
|
ServerPlugin.startPlugin(koin)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user