Skip to content

Commit

Permalink
fix: add contract for in place invocation to FlowGraphConfigScope
Browse files Browse the repository at this point in the history
  • Loading branch information
silenium-dev committed Aug 10, 2024
1 parent 08af1a9 commit f0d9d51
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/main/kotlin/dev/silenium/libs/flows/impl/FlowGraphImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package dev.silenium.libs.flows.impl
import dev.silenium.libs.flows.api.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.map
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.coroutines.CoroutineContext
import kotlin.reflect.KClass

Expand Down Expand Up @@ -147,10 +150,14 @@ internal fun FlowGraph.builder() = FlowGraphConfigScopeImpl(this)
* @see FlowGraph
* @see CoroutineContext
*/
@OptIn(ExperimentalContracts::class)
suspend fun FlowGraph(
coroutineContext: CoroutineContext = Dispatchers.Default,
block: FlowGraphConfigScope.() -> Unit,
): FlowGraph = FlowGraphImpl(coroutineContext).builder().apply(block).configure().getOrThrow()
): FlowGraph {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return FlowGraphImpl(coroutineContext).builder().apply(block).configure().getOrThrow()
}

/**
* Creates a new [FlowGraph] with the given [coroutineScope] and [block] configuration.
Expand All @@ -163,7 +170,11 @@ suspend fun FlowGraph(
* @see FlowGraph
* @see CoroutineScope
*/
@OptIn(ExperimentalContracts::class)
suspend fun FlowGraph(
coroutineScope: CoroutineScope,
block: FlowGraphConfigScope.() -> Unit,
): FlowGraph = FlowGraphImpl(coroutineScope).builder().apply(block).configure().getOrThrow()
): FlowGraph {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return FlowGraphImpl(coroutineScope).builder().apply(block).configure().getOrThrow()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.silenium.libs.flows.impl

import dev.silenium.libs.flows.api.SourceFlowGraphElement
import dev.silenium.libs.flows.buffer.BufferSink
import dev.silenium.libs.flows.buffer.BufferSource
import dev.silenium.libs.flows.test.Base64Buffer
Expand All @@ -16,16 +17,16 @@ import kotlinx.coroutines.flow.firstOrNull

class FlowGraphImplTest : FunSpec({
test("FlowGraphBuilder") {
val source: SourceFlowGraphElement<Base64Buffer, DataType, BufferSource<Base64Buffer, DataType>>
val graph = FlowGraph(CoroutineScope(Dispatchers.Default)) {
val source = source(BufferSource<Base64Buffer, DataType>(0u to DataType.BASE64), "buffer-source")
source = source(BufferSource(0u to DataType.BASE64), "buffer-source")
val sink = sink(BufferSink<ByteArray, DataType>(), "buffer-sink")
val decoder = transformer(Base64Decoder(), "base64-decoder")
connect(source to decoder)
connect(decoder to sink) { _, _, sourcePad, _ ->
sourcePad + 1u
}
}
val source = graph.source<BufferSource<Base64Buffer, DataType>>("buffer-source")!!
val sink = graph.sink<BufferSink<ByteArray, DataType>>("buffer-sink")!!

val input = "test"
Expand Down

0 comments on commit f0d9d51

Please sign in to comment.