Skip to content

Commit

Permalink
Configuration transferring to the workers
Browse files Browse the repository at this point in the history
  • Loading branch information
svok committed Apr 11, 2024
1 parent 26f7301 commit 1c98a41
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.idea
/build
/kotlin-js-store/yarn.lock
/xx
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dependencies {
#### **`gradle.properties`**

```properties
kotlinCorVersion=0.5.5+
kotlinCorVersion=0.6.0+
```

## Usage
Expand Down
14 changes: 8 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
`maven-publish`
Expand All @@ -7,7 +9,7 @@ plugins {
}

group = "com.crowdproj"
version = "0.5.7"
version = "0.6.0"

repositories {
mavenCentral()
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions src/commonMain/kotlin/base/BaseCorChainDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package com.crowdproj.kotlin.cor.base

import com.crowdproj.kotlin.cor.*

abstract class BaseCorChainDsl<T,K>(
abstract class BaseCorChainDsl<T,K,C>(
override val config: C,
override var title: String = "",
override var description: String = "",
protected val workers: MutableList<ICorExecDsl<K>> = mutableListOf(),
protected val workers: MutableList<ICorExecDsl<K,C>> = mutableListOf(),
protected var blockOn: suspend T.() -> Boolean = { true },
protected var blockExcept: suspend T.(e: Throwable) -> Unit = { e: Throwable -> throw e },
) : ICorExecDsl<T>, ICorOnDsl<T>, ICorExceptDsl<T>, ICorAddExecDsl<K> {
) : ICorExecDsl<T,C>, ICorOnDsl<T>, ICorExceptDsl<T>, ICorAddExecDsl<K,C> {

abstract override fun build(): ICorExec<T>

override fun add(worker: ICorExecDsl<K>) {
override fun add(worker: ICorExecDsl<K,C>) {
workers.add(worker)
}

Expand Down
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/base/BaseCorWorkerDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.crowdproj.kotlin.cor.base

import com.crowdproj.kotlin.cor.*

abstract class BaseCorWorkerDsl<T>(
abstract class BaseCorWorkerDsl<T,C>(
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<T>, ICorOnDsl<T>, ICorExceptDsl<T>, ICorHandleDsl<T> {
) : ICorExecDsl<T,C>, ICorOnDsl<T>, ICorExceptDsl<T>, ICorHandleDsl<T,C> {

abstract override fun build(): ICorExec<T>

Expand Down
18 changes: 12 additions & 6 deletions src/commonMain/kotlin/cor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
interface ICorConfigurable<C> {
val config: C
}

interface ICorExecDsl<T,C>: ICorConfigurable<C> {
var title: String
var description: String
fun build(): ICorExec<T>
Expand All @@ -17,11 +21,11 @@ interface ICorExceptDsl<T> {
fun except(function: suspend T.(e: Throwable) -> Unit)
}

interface ICorAddExecDsl<T> {
fun add(worker: ICorExecDsl<T>)
interface ICorAddExecDsl<T,C>: ICorConfigurable<C> {
fun add(worker: ICorExecDsl<T,C>)
}

interface ICorHandleDsl<T> {
interface ICorHandleDsl<T,C> {
fun handle(function: suspend T.() -> Unit)
}

Expand All @@ -47,6 +51,8 @@ interface ICorWorker<T> : ICorExec<T> {
}
}

fun <T> rootChain(function: CorChainDsl<T>.() -> Unit) = CorChainDsl<T>().apply(function)
fun <T> rootWorker(function: CorWorkerDsl<T>.() -> Unit) = CorWorkerDsl<T>().apply(function)
fun <T> rootChain(function: CorChainDsl<T,Unit>.() -> Unit) = CorChainDsl<T,Unit>(Unit).apply(function)
fun <T,C> rootChain(config: C, function: CorChainDsl<T,C>.() -> Unit) = CorChainDsl<T,C>(config).apply(function)
fun <T> rootWorker(function: CorWorkerDsl<T,Unit>.() -> Unit) = CorWorkerDsl<T,Unit>(Unit).apply(function)
fun <T,C> rootWorker(config: C, function: CorWorkerDsl<T,C>.() -> Unit) = CorWorkerDsl<T,C>(config).apply(function)

6 changes: 3 additions & 3 deletions src/commonMain/kotlin/handlers/Chain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.crowdproj.kotlin.cor.base.BaseCorChain
import com.crowdproj.kotlin.cor.base.BaseCorChainDsl

@CorDslMarker
fun <T> ICorAddExecDsl<T>.chain(function: CorChainDsl<T>.() -> Unit) {
add(CorChainDsl<T>().apply(function))
fun <T,C> ICorAddExecDsl<T,C>.chain(function: CorChainDsl<T,C>.() -> Unit) {
add(CorChainDsl<T,C>(this.config).apply(function))
}

class CorChain<T>(
Expand All @@ -34,7 +34,7 @@ class CorChain<T>(
* The chains are executed sequentially.
*/
@CorDslMarker
class CorChainDsl<T>() : BaseCorChainDsl<T,T>() {
class CorChainDsl<T,C>(config: C) : BaseCorChainDsl<T,T,C>(config) {
override fun build(): ICorExec<T> = CorChain(
title = title,
description = description,
Expand Down
17 changes: 9 additions & 8 deletions src/commonMain/kotlin/handlers/Loop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import com.crowdproj.kotlin.cor.base.BaseCorChain
import com.crowdproj.kotlin.cor.base.BaseCorChainDsl

@CorDslMarker
fun <T> ICorAddExecDsl<T>.loopWhile(
function: CorLoopDsl<T>.() -> Unit
fun <T,C> ICorAddExecDsl<T,C>.loopWhile(
function: CorLoopDsl<T,C>.() -> Unit
) {
add(
CorLoopDsl<T>(checkBefore = true).apply(function)
CorLoopDsl<T,C>(this.config, checkBefore = true).apply(function)
)
}

@CorDslMarker
fun <T> ICorAddExecDsl<T>.loopUntil(
function: CorLoopDsl<T>.() -> Unit
fun <T,C> ICorAddExecDsl<T,C>.loopUntil(
function: CorLoopDsl<T,C>.() -> Unit
) {
add(
CorLoopDsl<T>(checkBefore = false).apply(function)
CorLoopDsl<T,C>(this.config, checkBefore = false).apply(function)
)
}

Expand Down Expand Up @@ -69,10 +69,11 @@ class CorLoop<T>(
}

@CorDslMarker
class CorLoopDsl<T>(
class CorLoopDsl<T,C>(
config: C,
private val checkBefore: Boolean,
var blockCheck: suspend T.() -> Boolean = { true },
) : BaseCorChainDsl<T,T>() {
) : BaseCorChainDsl<T,T,C>(config) {
override fun build(): ICorExec<T> = CorLoop(
checkBefore,
title = title,
Expand Down
6 changes: 3 additions & 3 deletions src/commonMain/kotlin/handlers/Parallel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

@CorDslMarker
fun <T> ICorAddExecDsl<T>.parallel(function: CorParallelDsl<T>.() -> Unit) {
add(CorParallelDsl<T>().apply(function))
fun <T,C> ICorAddExecDsl<T,C>.parallel(function: CorParallelDsl<T,C>.() -> Unit) {
add(CorParallelDsl<T,C>(this.config).apply(function))
}

class CorParallel<T>(
Expand Down Expand Up @@ -40,7 +40,7 @@ class CorParallel<T>(
* Chains are started simultaneously and executed in parallel.
*/
@CorDslMarker
class CorParallelDsl<T>(): BaseCorChainDsl<T,T>() {
class CorParallelDsl<T,C>(config: C): BaseCorChainDsl<T,T,C>(config) {
override fun build(): ICorExec<T> = CorParallel(
title = title,
description = description,
Expand Down
7 changes: 3 additions & 4 deletions src/commonMain/kotlin/handlers/SubChain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.map

@CorDslMarker
fun <T, K> ICorAddExecDsl<T>.subChain(function: CorSubChainDsl<T, K>.() -> Unit) {
add(CorSubChainDsl<T, K>().apply(function))
fun <T, K, C> ICorAddExecDsl<T,C>.subChain(function: CorSubChainDsl<T, K, C>.() -> Unit) {
add(CorSubChainDsl<T, K, C>(this.config).apply(function))
}

class CorSubChain<T, K>(
Expand Down Expand Up @@ -47,8 +47,7 @@ class CorSubChain<T, K>(
* It can be expanded by other chains.
*/
@CorDslMarker
class CorSubChainDsl<T, K>(
) : BaseCorChainDsl<T, K>() {
class CorSubChainDsl<T, K, C>(config: C) : BaseCorChainDsl<T, K, C>(config) {
private var blockSplit: suspend T.() -> Flow<K> = { emptyFlow() }
private var blockJoin: suspend T.(K) -> Unit = {}
private var bufferSize: Int = 0
Expand Down
12 changes: 6 additions & 6 deletions src/commonMain/kotlin/handlers/Worker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import com.crowdproj.kotlin.cor.*
import com.crowdproj.kotlin.cor.base.BaseCorWorkerDsl

@CorDslMarker
fun <T> ICorAddExecDsl<T>.worker(
function: CorWorkerDsl<T>.() -> Unit
fun <T,C> ICorAddExecDsl<T,C>.worker(
function: CorWorkerDsl<T,C>.() -> Unit
) {
add(
CorWorkerDsl<T>().apply(function)
CorWorkerDsl<T,C>(this.config).apply(function)
)
}

@CorDslMarker
fun <T> ICorAddExecDsl<T>.worker(
fun <T,C> ICorAddExecDsl<T,C>.worker(
title: String,
description: String = "",
function: suspend T.() -> Unit
) {
add(
CorWorkerDsl<T>().apply {
CorWorkerDsl<T,C>(this.config).apply {
this.title = title
this.description = description
this.handle(function)
Expand All @@ -43,7 +43,7 @@ class CorWorker<T>(
* DLS context of a single execution. Cannot be expanded by other chains.
*/
@CorDslMarker
class CorWorkerDsl<T>() : BaseCorWorkerDsl<T>() {
class CorWorkerDsl<T,C>(config: C) : BaseCorWorkerDsl<T,C>(config) {

override fun build(): ICorExec<T> = CorWorker<T>(
title = title,
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/CorBaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CorBaseTest {
}
}

private fun ICorAddExecDsl<TestContext>.printResult() = worker(title = "Print example") {
private fun ICorAddExecDsl<TestContext, Unit>.printResult() = worker(title = "Print example") {
println("some = $some")
}

36 changes: 36 additions & 0 deletions src/commonTest/kotlin/CorConfigTest.kt
Original file line number Diff line number Diff line change
@@ -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<TestContext, TestConfig>(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 = ""
)
}
}
2 changes: 0 additions & 2 deletions src/commonTest/kotlin/CorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/subChain/CorSubChainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CorSubChainTest {
worker("init") {
some = 0
}
subChain<TestContext, TestSubContext> {
subChain<TestContext, TestSubContext, Unit> {
buffer(20)
on { status == CorStatuses.RUNNING }
split { (1..10).asFlow().map { TestSubContext(temp = it) } }
Expand Down
Loading

0 comments on commit 1c98a41

Please sign in to comment.