From 1c98a41499a051954cf984da3dc9db5d22ad1a33 Mon Sep 17 00:00:00 2001 From: Sergey Okatov Date: Thu, 11 Apr 2024 20:45:35 +0500 Subject: [PATCH] Configuration transferring to the workers --- .gitignore | 1 + README.md | 2 +- build.gradle.kts | 14 ++++---- gradle.properties | 8 ++--- gradle/wrapper/gradle-wrapper.properties | 2 +- src/commonMain/kotlin/base/BaseCorChainDsl.kt | 9 ++--- .../kotlin/base/BaseCorWorkerDsl.kt | 5 +-- src/commonMain/kotlin/cor.kt | 18 ++++++---- src/commonMain/kotlin/handlers/Chain.kt | 6 ++-- src/commonMain/kotlin/handlers/Loop.kt | 17 ++++----- src/commonMain/kotlin/handlers/Parallel.kt | 6 ++-- src/commonMain/kotlin/handlers/SubChain.kt | 7 ++-- src/commonMain/kotlin/handlers/Worker.kt | 12 +++---- src/commonTest/kotlin/CorBaseTest.kt | 2 +- src/commonTest/kotlin/CorConfigTest.kt | 36 +++++++++++++++++++ src/commonTest/kotlin/CorTest.kt | 2 -- .../kotlin/subChain/CorSubChainTest.kt | 2 +- .../kotlin/subChain/SubChainSequentialTest.kt | 9 ++--- 18 files changed, 100 insertions(+), 58 deletions(-) create mode 100644 src/commonTest/kotlin/CorConfigTest.kt diff --git a/.gitignore b/.gitignore index c4eec31..9a77c5a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.idea /build /kotlin-js-store/yarn.lock +/xx diff --git a/README.md b/README.md index 1ed8418..3d1448a 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ dependencies { #### **`gradle.properties`** ```properties -kotlinCorVersion=0.5.5+ +kotlinCorVersion=0.6.0+ ``` ## Usage diff --git a/build.gradle.kts b/build.gradle.kts index adefa09..eaf82b2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl + plugins { kotlin("multiplatform") `maven-publish` @@ -7,7 +9,7 @@ plugins { } group = "com.crowdproj" -version = "0.5.7" +version = "0.6.0" repositories { mavenCentral() @@ -31,23 +33,23 @@ kotlin { jvm() linuxX64() linuxArm64() - ios() iosX64() iosArm64() // iosSimulatorArm64() macosX64() macosArm64() - tvos() tvosArm64() tvosSimulatorArm64() tvosX64() - watchos() watchosArm32() watchosSimulatorArm64() watchosArm64() watchosX64() -// wasm() -// wasm32() + @OptIn(ExperimentalWasmDsl::class) + wasmJs { + browser() + nodejs() + } mingwX64() sourceSets { diff --git a/gradle.properties b/gradle.properties index 064844c..f2ad8de 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,9 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m -kotlinVersion=1.9.10 -dokkaVersion=1.8.20 +kotlinVersion=1.9.23 +dokkaVersion=1.9.20 -coroutinesVersion=1.7.3 -atomicfuVersion=0.21.0 +coroutinesVersion=1.8.0 +atomicfuVersion=0.23.2 nexusStagingVersion=0.30.0 # -native-mt diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 98debb8..48c0a02 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/commonMain/kotlin/base/BaseCorChainDsl.kt b/src/commonMain/kotlin/base/BaseCorChainDsl.kt index d922f54..ff41158 100644 --- a/src/commonMain/kotlin/base/BaseCorChainDsl.kt +++ b/src/commonMain/kotlin/base/BaseCorChainDsl.kt @@ -2,17 +2,18 @@ package com.crowdproj.kotlin.cor.base import com.crowdproj.kotlin.cor.* -abstract class BaseCorChainDsl( +abstract class BaseCorChainDsl( + override val config: C, override var title: String = "", override var description: String = "", - protected val workers: MutableList> = mutableListOf(), + protected val workers: MutableList> = mutableListOf(), protected var blockOn: suspend T.() -> Boolean = { true }, protected var blockExcept: suspend T.(e: Throwable) -> Unit = { e: Throwable -> throw e }, -) : ICorExecDsl, ICorOnDsl, ICorExceptDsl, ICorAddExecDsl { +) : ICorExecDsl, ICorOnDsl, ICorExceptDsl, ICorAddExecDsl { abstract override fun build(): ICorExec - override fun add(worker: ICorExecDsl) { + override fun add(worker: ICorExecDsl) { workers.add(worker) } diff --git a/src/commonMain/kotlin/base/BaseCorWorkerDsl.kt b/src/commonMain/kotlin/base/BaseCorWorkerDsl.kt index 3702b7f..308eab7 100644 --- a/src/commonMain/kotlin/base/BaseCorWorkerDsl.kt +++ b/src/commonMain/kotlin/base/BaseCorWorkerDsl.kt @@ -2,13 +2,14 @@ package com.crowdproj.kotlin.cor.base import com.crowdproj.kotlin.cor.* -abstract class BaseCorWorkerDsl( +abstract class BaseCorWorkerDsl( + override val config: C, override var title: String = "", override var description: String = "", protected var blockOn: suspend T.() -> Boolean = { true }, protected var blockHandle: suspend T.() -> Unit = {}, protected var blockExcept: suspend T.(e: Throwable) -> Unit = { e: Throwable -> throw e }, -) : ICorExecDsl, ICorOnDsl, ICorExceptDsl, ICorHandleDsl { +) : ICorExecDsl, ICorOnDsl, ICorExceptDsl, ICorHandleDsl { abstract override fun build(): ICorExec diff --git a/src/commonMain/kotlin/cor.kt b/src/commonMain/kotlin/cor.kt index c6f368e..213875e 100644 --- a/src/commonMain/kotlin/cor.kt +++ b/src/commonMain/kotlin/cor.kt @@ -3,7 +3,11 @@ package com.crowdproj.kotlin.cor import com.crowdproj.kotlin.cor.handlers.CorChainDsl import com.crowdproj.kotlin.cor.handlers.CorWorkerDsl -interface ICorExecDsl { +interface ICorConfigurable { + val config: C +} + +interface ICorExecDsl: ICorConfigurable { var title: String var description: String fun build(): ICorExec @@ -17,11 +21,11 @@ interface ICorExceptDsl { fun except(function: suspend T.(e: Throwable) -> Unit) } -interface ICorAddExecDsl { - fun add(worker: ICorExecDsl) +interface ICorAddExecDsl: ICorConfigurable { + fun add(worker: ICorExecDsl) } -interface ICorHandleDsl { +interface ICorHandleDsl { fun handle(function: suspend T.() -> Unit) } @@ -47,6 +51,8 @@ interface ICorWorker : ICorExec { } } -fun rootChain(function: CorChainDsl.() -> Unit) = CorChainDsl().apply(function) -fun rootWorker(function: CorWorkerDsl.() -> Unit) = CorWorkerDsl().apply(function) +fun rootChain(function: CorChainDsl.() -> Unit) = CorChainDsl(Unit).apply(function) +fun rootChain(config: C, function: CorChainDsl.() -> Unit) = CorChainDsl(config).apply(function) +fun rootWorker(function: CorWorkerDsl.() -> Unit) = CorWorkerDsl(Unit).apply(function) +fun rootWorker(config: C, function: CorWorkerDsl.() -> Unit) = CorWorkerDsl(config).apply(function) diff --git a/src/commonMain/kotlin/handlers/Chain.kt b/src/commonMain/kotlin/handlers/Chain.kt index f10ab76..cc8d1be 100644 --- a/src/commonMain/kotlin/handlers/Chain.kt +++ b/src/commonMain/kotlin/handlers/Chain.kt @@ -7,8 +7,8 @@ import com.crowdproj.kotlin.cor.base.BaseCorChain import com.crowdproj.kotlin.cor.base.BaseCorChainDsl @CorDslMarker -fun ICorAddExecDsl.chain(function: CorChainDsl.() -> Unit) { - add(CorChainDsl().apply(function)) +fun ICorAddExecDsl.chain(function: CorChainDsl.() -> Unit) { + add(CorChainDsl(this.config).apply(function)) } class CorChain( @@ -34,7 +34,7 @@ class CorChain( * The chains are executed sequentially. */ @CorDslMarker -class CorChainDsl() : BaseCorChainDsl() { +class CorChainDsl(config: C) : BaseCorChainDsl(config) { override fun build(): ICorExec = CorChain( title = title, description = description, diff --git a/src/commonMain/kotlin/handlers/Loop.kt b/src/commonMain/kotlin/handlers/Loop.kt index ebe7073..1540a47 100644 --- a/src/commonMain/kotlin/handlers/Loop.kt +++ b/src/commonMain/kotlin/handlers/Loop.kt @@ -7,20 +7,20 @@ import com.crowdproj.kotlin.cor.base.BaseCorChain import com.crowdproj.kotlin.cor.base.BaseCorChainDsl @CorDslMarker -fun ICorAddExecDsl.loopWhile( - function: CorLoopDsl.() -> Unit +fun ICorAddExecDsl.loopWhile( + function: CorLoopDsl.() -> Unit ) { add( - CorLoopDsl(checkBefore = true).apply(function) + CorLoopDsl(this.config, checkBefore = true).apply(function) ) } @CorDslMarker -fun ICorAddExecDsl.loopUntil( - function: CorLoopDsl.() -> Unit +fun ICorAddExecDsl.loopUntil( + function: CorLoopDsl.() -> Unit ) { add( - CorLoopDsl(checkBefore = false).apply(function) + CorLoopDsl(this.config, checkBefore = false).apply(function) ) } @@ -69,10 +69,11 @@ class CorLoop( } @CorDslMarker -class CorLoopDsl( +class CorLoopDsl( + config: C, private val checkBefore: Boolean, var blockCheck: suspend T.() -> Boolean = { true }, -) : BaseCorChainDsl() { +) : BaseCorChainDsl(config) { override fun build(): ICorExec = CorLoop( checkBefore, title = title, diff --git a/src/commonMain/kotlin/handlers/Parallel.kt b/src/commonMain/kotlin/handlers/Parallel.kt index c1644aa..c38be53 100644 --- a/src/commonMain/kotlin/handlers/Parallel.kt +++ b/src/commonMain/kotlin/handlers/Parallel.kt @@ -9,8 +9,8 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch @CorDslMarker -fun ICorAddExecDsl.parallel(function: CorParallelDsl.() -> Unit) { - add(CorParallelDsl().apply(function)) +fun ICorAddExecDsl.parallel(function: CorParallelDsl.() -> Unit) { + add(CorParallelDsl(this.config).apply(function)) } class CorParallel( @@ -40,7 +40,7 @@ class CorParallel( * Chains are started simultaneously and executed in parallel. */ @CorDslMarker -class CorParallelDsl(): BaseCorChainDsl() { +class CorParallelDsl(config: C): BaseCorChainDsl(config) { override fun build(): ICorExec = CorParallel( title = title, description = description, diff --git a/src/commonMain/kotlin/handlers/SubChain.kt b/src/commonMain/kotlin/handlers/SubChain.kt index dcbec62..ddee54a 100644 --- a/src/commonMain/kotlin/handlers/SubChain.kt +++ b/src/commonMain/kotlin/handlers/SubChain.kt @@ -13,8 +13,8 @@ import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.map @CorDslMarker -fun ICorAddExecDsl.subChain(function: CorSubChainDsl.() -> Unit) { - add(CorSubChainDsl().apply(function)) +fun ICorAddExecDsl.subChain(function: CorSubChainDsl.() -> Unit) { + add(CorSubChainDsl(this.config).apply(function)) } class CorSubChain( @@ -47,8 +47,7 @@ class CorSubChain( * It can be expanded by other chains. */ @CorDslMarker -class CorSubChainDsl( -) : BaseCorChainDsl() { +class CorSubChainDsl(config: C) : BaseCorChainDsl(config) { private var blockSplit: suspend T.() -> Flow = { emptyFlow() } private var blockJoin: suspend T.(K) -> Unit = {} private var bufferSize: Int = 0 diff --git a/src/commonMain/kotlin/handlers/Worker.kt b/src/commonMain/kotlin/handlers/Worker.kt index af32f42..750792a 100644 --- a/src/commonMain/kotlin/handlers/Worker.kt +++ b/src/commonMain/kotlin/handlers/Worker.kt @@ -4,22 +4,22 @@ import com.crowdproj.kotlin.cor.* import com.crowdproj.kotlin.cor.base.BaseCorWorkerDsl @CorDslMarker -fun ICorAddExecDsl.worker( - function: CorWorkerDsl.() -> Unit +fun ICorAddExecDsl.worker( + function: CorWorkerDsl.() -> Unit ) { add( - CorWorkerDsl().apply(function) + CorWorkerDsl(this.config).apply(function) ) } @CorDslMarker -fun ICorAddExecDsl.worker( +fun ICorAddExecDsl.worker( title: String, description: String = "", function: suspend T.() -> Unit ) { add( - CorWorkerDsl().apply { + CorWorkerDsl(this.config).apply { this.title = title this.description = description this.handle(function) @@ -43,7 +43,7 @@ class CorWorker( * DLS context of a single execution. Cannot be expanded by other chains. */ @CorDslMarker -class CorWorkerDsl() : BaseCorWorkerDsl() { +class CorWorkerDsl(config: C) : BaseCorWorkerDsl(config) { override fun build(): ICorExec = CorWorker( title = title, diff --git a/src/commonTest/kotlin/CorBaseTest.kt b/src/commonTest/kotlin/CorBaseTest.kt index 1218991..0104952 100644 --- a/src/commonTest/kotlin/CorBaseTest.kt +++ b/src/commonTest/kotlin/CorBaseTest.kt @@ -58,7 +58,7 @@ class CorBaseTest { } } -private fun ICorAddExecDsl.printResult() = worker(title = "Print example") { +private fun ICorAddExecDsl.printResult() = worker(title = "Print example") { println("some = $some") } diff --git a/src/commonTest/kotlin/CorConfigTest.kt b/src/commonTest/kotlin/CorConfigTest.kt new file mode 100644 index 0000000..0514f09 --- /dev/null +++ b/src/commonTest/kotlin/CorConfigTest.kt @@ -0,0 +1,36 @@ +package com.crowdproj.kotlin.cor + +import com.crowdproj.kotlin.cor.handlers.worker +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals + +class CorConfigTest { + + @Test + fun corConfigTest() = runTest { + val ctx = TestContext() + + chain.exec(ctx) + + assertEquals("my-setting", ctx.history) + } + companion object { + val chain = rootChain(TestConfig("my-setting")) { + worker { + title = "configTest" + description = "testing for work with initial config" + handle { + history += this@rootChain.config.setting + } + } + }.build() + + data class TestConfig( + val setting: String = "" + ) + data class TestContext( + var history: String = "" + ) + } +} diff --git a/src/commonTest/kotlin/CorTest.kt b/src/commonTest/kotlin/CorTest.kt index fbd1eb3..f93ad13 100644 --- a/src/commonTest/kotlin/CorTest.kt +++ b/src/commonTest/kotlin/CorTest.kt @@ -2,13 +2,11 @@ package com.crowdproj.kotlin.cor import com.crowdproj.kotlin.cor.helper.CorStatuses import com.crowdproj.kotlin.cor.helper.TestContext -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals -@OptIn(ExperimentalCoroutinesApi::class) class CorTest { @Test diff --git a/src/commonTest/kotlin/subChain/CorSubChainTest.kt b/src/commonTest/kotlin/subChain/CorSubChainTest.kt index 11147e5..3d0f4ca 100644 --- a/src/commonTest/kotlin/subChain/CorSubChainTest.kt +++ b/src/commonTest/kotlin/subChain/CorSubChainTest.kt @@ -29,7 +29,7 @@ class CorSubChainTest { worker("init") { some = 0 } - subChain { + subChain { buffer(20) on { status == CorStatuses.RUNNING } split { (1..10).asFlow().map { TestSubContext(temp = it) } } diff --git a/src/commonTest/kotlin/subChain/SubChainSequentialTest.kt b/src/commonTest/kotlin/subChain/SubChainSequentialTest.kt index 9d8c1d6..0ca11ef 100644 --- a/src/commonTest/kotlin/subChain/SubChainSequentialTest.kt +++ b/src/commonTest/kotlin/subChain/SubChainSequentialTest.kt @@ -7,7 +7,6 @@ import com.crowdproj.kotlin.cor.helper.TestSubContext import com.crowdproj.kotlin.cor.rootChain import kotlinx.atomicfu.update import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.test.runTest @@ -15,10 +14,8 @@ import kotlinx.coroutines.withContext import kotlin.test.Test import kotlin.test.assertContains import kotlin.test.assertEquals -import kotlin.time.ExperimentalTime import kotlin.time.measureTime -@OptIn(ExperimentalCoroutinesApi::class, ExperimentalTime::class) class SubChainSequentialTest { @Test fun sequentialWorkers() = runTest { @@ -63,7 +60,7 @@ class SubChainSequentialTest { companion object { val chain = rootChain { - subChain { + subChain { title = "Check sequential execution of workers" on { some == 1 } split { @@ -77,7 +74,7 @@ class SubChainSequentialTest { text += it.str } } - subChain { + subChain { title = "Check sequential execution of data" on { some == 2 } buffer(0) @@ -91,7 +88,7 @@ class SubChainSequentialTest { worker("") { parent.atomicText.update { it + str } } worker("") { println("STOP: $str") } } - subChain { + subChain { title = "Check parallel execution of data" buffer(11) on { some == 3 }