diff --git a/sample/src/main/java/com/trendyol/transmission/features/FeaturesModule.kt b/sample/src/main/java/com/trendyol/transmission/features/FeaturesModule.kt index 9c1fceb..62f47e6 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/FeaturesModule.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/FeaturesModule.kt @@ -5,6 +5,7 @@ import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer import com.trendyol.transmission.features.input.InputTransformer import com.trendyol.transmission.features.multioutput.MultiOutputTransformer import com.trendyol.transmission.features.output.OutputTransformer +import com.trendyol.transmission.router.builder.TransmissionRouterBuilder import com.trendyol.transmission.transformer.Transformer import dagger.Binds import dagger.Module @@ -42,7 +43,9 @@ interface FeaturesModule { fun provideRouter( transformerSet: @JvmSuppressWildcards Set ): TransmissionRouter { - return TransmissionRouter(transformerSet) + return TransmissionRouterBuilder.build { + withTransformerSet(transformerSet) + } } } diff --git a/sample/src/main/java/com/trendyol/transmission/features/input/InputTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/input/InputTransformer.kt index 074b208..3c52cbc 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/input/InputTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/input/InputTransformer.kt @@ -11,6 +11,8 @@ import com.trendyol.transmission.transformer.handler.registerSignal import com.trendyol.transmission.transformer.request.buildComputationContract import com.trendyol.transmission.transformer.request.buildComputationContractWithArgs import com.trendyol.transmission.transformer.request.buildDataContract +import com.trendyol.transmission.transformer.request.computation.ComputationRegistry +import com.trendyol.transmission.transformer.request.computation.computationRegistry import com.trendyol.transmission.transformer.request.computation.registerComputation import com.trendyol.transmission.ui.InputUiState import kotlinx.coroutines.CoroutineDispatcher @@ -24,14 +26,14 @@ class InputTransformer @Inject constructor( private val holder = buildDataHolder(InputUiState(), holderContract) - init { - computationRegistry - .registerComputation(writtenInputContract) { - delay(1.seconds) - WrittenInput(holder.getValue().writtenText) - }.registerComputation(writtenInputWithArgs) { - WrittenInput(it) - } + override val computationRegistry: ComputationRegistry = computationRegistry { + registerComputation(writtenInputContract) { + delay(1.seconds) + WrittenInput(holder.getValue().writtenText) + } + registerComputation(writtenInputWithArgs) { + WrittenInput(it) + } } override val handlerRegistry: HandlerRegistry = handlerRegistry { diff --git a/sample/src/main/java/com/trendyol/transmission/features/output/OutputTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/output/OutputTransformer.kt index 132de81..08dc1e9 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/output/OutputTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/output/OutputTransformer.kt @@ -15,7 +15,11 @@ import com.trendyol.transmission.transformer.handler.handlerRegistry import com.trendyol.transmission.transformer.handler.registerEffect import com.trendyol.transmission.transformer.request.buildComputationContract import com.trendyol.transmission.transformer.request.buildExecutionContract +import com.trendyol.transmission.transformer.request.computation.ComputationRegistry +import com.trendyol.transmission.transformer.request.computation.computationRegistry import com.trendyol.transmission.transformer.request.computation.registerComputation +import com.trendyol.transmission.transformer.request.execution.ExecutionRegistry +import com.trendyol.transmission.transformer.request.execution.executionRegistry import com.trendyol.transmission.transformer.request.execution.registerExecution import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.OutputUiState @@ -34,24 +38,25 @@ class OutputTransformer @Inject constructor( private val holder2 = buildDataHolder(ColorPickerUiState(), publishUpdates = false) - init { - computationRegistry - .registerComputation(outputCalculationContract) { - delay(2.seconds) - val data = getData(ColorPickerTransformer.holderContract)?.selectedColorIndex - val writtenOutput = compute(InputTransformer.writtenInputContract) - val result = Random.nextInt(5, 15) * Random.nextInt(5, 15) - OutputCalculationResult("result is $result with ($writtenOutput) and $data") - } - executionRegistry - .registerExecution(outputExecutionContract) { - delay(4.seconds) - communicationScope.publish(ColorPickerEffect.BackgroundColorUpdate(Pink80)) - throw RuntimeException( - "This exception will be properly handled and caught " + - "inside of the onError() function" - ) - } + override val computationRegistry: ComputationRegistry = computationRegistry { + registerComputation(outputCalculationContract) { + delay(2.seconds) + val data = getData(ColorPickerTransformer.holderContract)?.selectedColorIndex + val writtenOutput = compute(InputTransformer.writtenInputContract) + val result = Random.nextInt(5, 15) * Random.nextInt(5, 15) + OutputCalculationResult("result is $result with ($writtenOutput) and $data") + } + } + + override val executionRegistry: ExecutionRegistry = executionRegistry { + registerExecution(outputExecutionContract) { + delay(4.seconds) + communicationScope.publish(ColorPickerEffect.BackgroundColorUpdate(Pink80)) + throw RuntimeException( + "This exception will be properly handled and caught " + + "inside of the onError() function" + ) + } } override val handlerRegistry: HandlerRegistry = handlerRegistry { diff --git a/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt b/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt index 766c114..b29388f 100644 --- a/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt +++ b/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt @@ -60,7 +60,7 @@ class ColorPickerTransformerTest { .test(signal = ColorPickerSignal.SelectColor(3, Color.Blue)) { assertEquals( Color.Blue.copy(alpha = 0.1f), - (effectStream.first().effect as ColorPickerEffect.BackgroundColorUpdate).color + (effectStream.first() as ColorPickerEffect.BackgroundColorUpdate).color ) } } @@ -68,7 +68,7 @@ class ColorPickerTransformerTest { @Test fun `GIVEN inputTransformer, WHEN SelectColor signal is sent, THEN SelectedColorUpdate is sent to MultiOutputTransformer`() { sut.attachToRouter().test(signal = ColorPickerSignal.SelectColor(3, Color.Blue)) { - assertTrue { effectStream.last().effect is ColorPickerEffect.SelectedColorUpdate } + assertTrue { effectStream.last() is ColorPickerEffect.SelectedColorUpdate } } } } diff --git a/sample/src/test/java/com/trendyol/transmission/features/input/InputTransformerTest.kt b/sample/src/test/java/com/trendyol/transmission/features/input/InputTransformerTest.kt index 5b91a81..f52eff8 100644 --- a/sample/src/test/java/com/trendyol/transmission/features/input/InputTransformerTest.kt +++ b/sample/src/test/java/com/trendyol/transmission/features/input/InputTransformerTest.kt @@ -27,7 +27,7 @@ class InputTransformerTest { fun `GIVEN inputTransformer, WHEN inputUpdate signal is sent, THEN inputUpdate effect is published`() { sut.attachToRouter() .test(signal = InputSignal.InputUpdate("test")) { - assertEquals(InputEffect.InputUpdate("test"), effectStream.first().effect) + assertEquals(InputEffect.InputUpdate("test"), effectStream.first()) assertEquals(InputUiState("test"), dataStream.last()) } } diff --git a/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt b/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt index cfb37a1..8c8b3bc 100644 --- a/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt +++ b/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt @@ -49,7 +49,7 @@ class OutputTransformerTest { } .test(effect = InputEffect.InputUpdate("test")) { assertEquals(OutputUiState(outputText = "test"), dataStream[1]) - assertTrue(effectStream.last().effect is RouterEffect) + assertTrue(effectStream.last() is RouterEffect) } } } diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/TestRouter.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/TestRouter.kt deleted file mode 100644 index 24947a4..0000000 --- a/transmission-test/src/main/java/com/trendyol/transmissiontest/TestRouter.kt +++ /dev/null @@ -1,126 +0,0 @@ -package com.trendyol.transmissiontest - -import com.trendyol.transmission.Transmission -import com.trendyol.transmission.effect.EffectWrapper -import com.trendyol.transmission.router.createBroadcast -import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.request.Query -import com.trendyol.transmission.transformer.request.QueryResult -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.cancel -import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.receiveAsFlow -import kotlinx.coroutines.flow.shareIn -import kotlinx.coroutines.launch - -internal class TestRouter( - private val transformer: Transformer, - dispatcher: CoroutineDispatcher -) { - private val testScope = CoroutineScope(dispatcher) - var registry: RegistryScopeImpl = RegistryScopeImpl() - - private val signalBroadcast = testScope.createBroadcast() - private val effectBroadcast = testScope.createBroadcast() - - fun sendSignal(signal: Transmission.Signal) { - signalBroadcast.producer.trySend(signal) - } - - val effectStream: Flow = effectBroadcast.output - - private val dataBroadcast = testScope.createBroadcast() - val dataStream = dataBroadcast.output - - private val outGoingQueryChannel: Channel = Channel(capacity = Channel.BUFFERED) - - private val queryResultChannel: Channel = - Channel(capacity = Channel.BUFFERED) - - private val incomingQueryResponse = queryResultChannel.receiveAsFlow() - .shareIn(testScope, SharingStarted.Lazily) - - init { - initialize() - } - - fun sendEffect(effect: Transmission.Effect) { - effectBroadcast.producer.trySend(EffectWrapper(effect)) - } - - - fun clear() { - testScope.cancel() - } - - private fun initialize() { - testScope.launch { - launch { - outGoingQueryChannel.receiveAsFlow().collect { processQuery(it) } - } - transformer.run { - startSignalCollection(signalBroadcast.output) - startDataPublishing(dataBroadcast.producer) - startEffectProcessing( - producer = effectBroadcast.producer, - incoming = effectBroadcast.output - ) - launch { - startQueryProcessing( - incomingQuery = incomingQueryResponse, - outGoingQuery = outGoingQueryChannel - ) - } - } - } - } - - private fun processQuery(query: Query) = testScope.launch { - when (query) { - is Query.Computation -> processComputationQuery(query) - is Query.Data -> processDataQuery(query) - is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) - is Query.Execution -> {} - is Query.ExecutionWithArgs<*> -> {} - } - } - - private fun processDataQuery( - query: Query.Data - ) = testScope.launch { - val dataToSend = QueryResult.Data( - owner = query.sender, - data = registry.dataMap[query.key], - key = query.key - ) - testScope.launch { - queryResultChannel.trySend(dataToSend) - } - } - - private fun processComputationQuery( - query: Query.Computation - ) = testScope.launch { - val computationToSend = QueryResult.Computation( - owner = query.sender, - data = registry.computationMap[query.key], - key = query.key - ) - queryResultChannel.trySend(computationToSend) - } - - private fun processComputationQueryWithArgs( - query: Query.ComputationWithArgs - ) = testScope.launch { - val computationToSend = QueryResult.Computation( - owner = query.sender, - data = registry.computationMap[query.key], - key = query.key - ) - queryResultChannel.trySend(computationToSend) - } - -} diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt index a28a7ce..f3f9d94 100644 --- a/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt +++ b/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt @@ -1,7 +1,9 @@ package com.trendyol.transmissiontest import com.trendyol.transmission.Transmission -import com.trendyol.transmission.effect.EffectWrapper +import com.trendyol.transmission.TransmissionRouter +import com.trendyol.transmission.router.RegistryScope +import com.trendyol.transmission.router.builder.TransmissionTestingRouterBuilder import com.trendyol.transmission.transformer.Transformer import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList @@ -15,16 +17,16 @@ import kotlinx.coroutines.test.runTest class TestSuite { private var orderedInitialProcessing: List = emptyList() private var transformer: Transformer? = null - private lateinit var router: TestRouter + private var registryScope: RegistryScope.() -> Unit = {} + private lateinit var router: TransmissionRouter fun initialize(transformer: Transformer): TestSuite { this.transformer = transformer - router = TestRouter(transformer, UnconfinedTestDispatcher()) return this } fun register(registry: RegistryScope.() -> Unit = {}): TestSuite { - router.registry = RegistryScopeImpl().apply(registry) + this.registryScope = registry return this } @@ -39,10 +41,15 @@ class TestSuite { transmission: Transmission, scope: suspend TransformerTestScope.(scope: TestScope) -> Unit ) { + router = TransmissionTestingRouterBuilder.build { + withDispatcher(UnconfinedTestDispatcher()) + this@TestSuite.transformer?.let { withTransformerSet(setOf(it)) } + testing(this@TestSuite.registryScope) + } runTest { val dataStream: MutableList = mutableListOf() - val effectStream: MutableList = mutableListOf() + val effectStream: MutableList = mutableListOf() try { backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { router.dataStream.toList(dataStream) @@ -52,20 +59,20 @@ class TestSuite { } val testScope = object : TransformerTestScope { override val dataStream: List = dataStream - override val effectStream: List = effectStream + override val effectStream: List = effectStream } orderedInitialProcessing.forEach { when (it) { is Transmission.Data -> throw IllegalArgumentException("Transmission.Data should not be sent for processing") - is Transmission.Effect -> router.sendEffect(it) - is Transmission.Signal -> router.sendSignal(it) + is Transmission.Effect -> router.processEffect(it) + is Transmission.Signal -> router.processSignal(it) } transformer?.waitProcessingToFinish() } if (transmission is Transmission.Signal) { - router.sendSignal(transmission) + router.processSignal(transmission) } else if (transmission is Transmission.Effect) { - router.sendEffect(transmission) + router.processEffect(transmission) } transformer?.waitProcessingToFinish() testScope.scope(this) diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/TransformerTestScope.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/TransformerTestScope.kt index 6dee996..3fbd639 100644 --- a/transmission-test/src/main/java/com/trendyol/transmissiontest/TransformerTestScope.kt +++ b/transmission-test/src/main/java/com/trendyol/transmissiontest/TransformerTestScope.kt @@ -5,5 +5,5 @@ import com.trendyol.transmission.effect.EffectWrapper interface TransformerTestScope { val dataStream: List - val effectStream: List + val effectStream: List } diff --git a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt index 331843b..96b6eae 100644 --- a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt +++ b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt @@ -1,6 +1,7 @@ package com.trendyol.transmission import com.trendyol.transmission.effect.EffectWrapper +import com.trendyol.transmission.router.RegistryScopeImpl import com.trendyol.transmission.router.RequestDelegate import com.trendyol.transmission.router.createBroadcast import com.trendyol.transmission.transformer.Transformer @@ -19,9 +20,10 @@ import kotlinx.coroutines.launch /** * Throws [IllegalArgumentException] when supplied [Transformer] set is empty */ -class TransmissionRouter( +class TransmissionRouter internal constructor( internal val transformerSet: Set, dispatcher: CoroutineDispatcher = Dispatchers.Default, + registryScope: RegistryScopeImpl? = null ) { private val routerScope = CoroutineScope(SupervisorJob() + dispatcher) @@ -36,7 +38,11 @@ class TransmissionRouter( val effectStream: SharedFlow = effectBroadcast.output.map { it.effect } .shareIn(routerScope, SharingStarted.Lazily) - private val _requestDelegate = RequestDelegate(routerScope, this@TransmissionRouter) + private val _requestDelegate = RequestDelegate( + queryScope = routerScope, + routerRef = this@TransmissionRouter, + registry = registryScope + ) val requestHelper: RequestHandler = _requestDelegate init { @@ -47,6 +53,10 @@ class TransmissionRouter( signalBroadcast.producer.trySend(signal) } + fun processEffect(effect: Transmission.Effect) { + effectBroadcast.producer.trySend(EffectWrapper(effect)) + } + private fun initialize() { require(transformerSet.isNotEmpty()) { "transformerSet should not be empty" diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/RegistryScope.kt b/transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt similarity index 87% rename from transmission-test/src/main/java/com/trendyol/transmissiontest/RegistryScope.kt rename to transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt index 23a12c5..76dfbbe 100644 --- a/transmission-test/src/main/java/com/trendyol/transmissiontest/RegistryScope.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt @@ -1,8 +1,9 @@ -package com.trendyol.transmissiontest +package com.trendyol.transmission.router import com.trendyol.transmission.Transmission interface RegistryScope { + fun addQueryData( data: D, key: String, @@ -14,7 +15,7 @@ interface RegistryScope { ) } -class RegistryScopeImpl : RegistryScope { +internal class RegistryScopeImpl : RegistryScope { val dataMap: MutableMap = mutableMapOf() val computationMap: MutableMap = mutableMapOf() diff --git a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt index bc618a4..a717f54 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -21,7 +21,8 @@ import kotlinx.coroutines.launch internal class RequestDelegate( private val queryScope: CoroutineScope, - private val routerRef: TransmissionRouter + private val routerRef: TransmissionRouter, + private val registry: RegistryScopeImpl? = null, ) : RequestHandler { private val routerQueryResultChannel: MutableSharedFlow = MutableSharedFlow() @@ -35,10 +36,16 @@ internal class RequestDelegate( init { queryScope.launch { - outGoingQuery.consumeAsFlow().collect { processQuery(it) } + if (registry != null) { + outGoingQuery.consumeAsFlow().collect { testQuery(it) } + } else { + outGoingQuery.consumeAsFlow().collect { processQuery(it) } + } } } + // region process queries + private suspend fun processQuery(query: Query) = queryScope.launch { when (query) { is Query.Computation -> processComputationQuery(query) @@ -152,6 +159,57 @@ internal class RequestDelegate( }.onFailure(executionHolder::onError).getOrNull() } + // endregion + + // region test queries + + private fun testQuery(query: Query) = queryScope.launch { + when (query) { + is Query.Computation -> testComputationQuery(query) + is Query.Data -> testDataQuery(query) + is Query.ComputationWithArgs<*> -> testComputationQueryWithArgs(query) + is Query.Execution -> {} + is Query.ExecutionWithArgs<*> -> {} + } + } + + private fun testDataQuery( + query: Query.Data + ) = queryScope.launch { + val dataToSend = QueryResult.Data( + owner = query.sender, + data = registry?.dataMap?.get(query.key), + key = query.key + ) + queryScope.launch { + queryResultChannel.trySend(dataToSend) + } + } + + private fun testComputationQuery( + query: Query.Computation + ) = queryScope.launch { + val computationToSend = QueryResult.Computation( + owner = query.sender, + data = registry?.computationMap?.get(query.key), + key = query.key + ) + queryResultChannel.trySend(computationToSend) + } + + private fun testComputationQueryWithArgs( + query: Query.ComputationWithArgs + ) = queryScope.launch { + val computationToSend = QueryResult.Computation( + owner = query.sender, + data = registry?.computationMap?.get(query.key), + key = query.key + ) + queryResultChannel.trySend(computationToSend) + } + + // region Request Handler + override suspend fun , D : Transmission.Data> getData(contract: C): D? { outGoingQuery.trySend( Query.Data(sender = routerRef.routerName, key = contract.key) @@ -219,4 +277,6 @@ internal class RequestDelegate( ) ) } + + // endregion } diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilder.kt new file mode 100644 index 0000000..6aa3329 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilder.kt @@ -0,0 +1,14 @@ +package com.trendyol.transmission.router.builder + +import com.trendyol.transmission.TransmissionRouter + +object TransmissionRouterBuilder { + + fun build(scope: TransmissionRouterBuilderScope.() -> Unit): TransmissionRouter { + val builder = TransmissionRouterBuilderInternal(scope) + return TransmissionRouter( + transformerSet = builder.transformerSet, + dispatcher = builder.dispatcher, + ) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt new file mode 100644 index 0000000..2e17365 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt @@ -0,0 +1,36 @@ +package com.trendyol.transmission.router.builder + +import com.trendyol.transmission.router.RegistryScope +import com.trendyol.transmission.router.RegistryScopeImpl +import com.trendyol.transmission.transformer.Transformer +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +internal class TransmissionRouterBuilderInternal internal constructor( + scope: TransmissionTestingRouterBuilderScope.() -> Unit +) { + + internal var dispatcher: CoroutineDispatcher = Dispatchers.Default + internal var transformerSet = setOf() + internal var registryScope: RegistryScopeImpl? = null + + private val scopeImpl = object : TransmissionTestingRouterBuilderScope { + + override fun testing(scope: RegistryScope.() -> Unit) { + this@TransmissionRouterBuilderInternal.registryScope = RegistryScopeImpl().apply(scope) + } + + override fun withDispatcher(dispatcher: CoroutineDispatcher) { + this@TransmissionRouterBuilderInternal.dispatcher = dispatcher + + } + + override fun withTransformerSet(transformerSet: Set) { + this@TransmissionRouterBuilderInternal.transformerSet = transformerSet + } + } + + init { + scopeImpl.apply(scope) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderScope.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderScope.kt new file mode 100644 index 0000000..6dc0ea7 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderScope.kt @@ -0,0 +1,9 @@ +package com.trendyol.transmission.router.builder + +import com.trendyol.transmission.transformer.Transformer +import kotlinx.coroutines.CoroutineDispatcher + +interface TransmissionRouterBuilderScope { + fun withDispatcher(dispatcher: CoroutineDispatcher) + fun withTransformerSet(transformerSet: Set) +} diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt new file mode 100644 index 0000000..2b9ff7b --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt @@ -0,0 +1,15 @@ +package com.trendyol.transmission.router.builder + +import com.trendyol.transmission.TransmissionRouter + +object TransmissionTestingRouterBuilder { + + fun build(scope: TransmissionTestingRouterBuilderScope.() -> Unit): TransmissionRouter { + val builder = TransmissionRouterBuilderInternal(scope) + return TransmissionRouter( + transformerSet = builder.transformerSet, + dispatcher = builder.dispatcher, + registryScope = builder.registryScope + ) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt new file mode 100644 index 0000000..8f450df --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt @@ -0,0 +1,7 @@ +package com.trendyol.transmission.router.builder + +import com.trendyol.transmission.router.RegistryScope + +interface TransmissionTestingRouterBuilderScope : TransmissionRouterBuilderScope { + fun testing(scope: RegistryScope.() -> Unit = {}) +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt index 015ebef..bd10978 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -49,8 +49,8 @@ open class Transformer( protected open val handlerRegistry: HandlerRegistry? = null - protected val executionRegistry: ExecutionRegistry by lazy { ExecutionRegistry(this) } - protected val computationRegistry: ComputationRegistry by lazy { ComputationRegistry(this) } + protected open val executionRegistry: ExecutionRegistry? = null + protected open val computationRegistry: ComputationRegistry? = null var currentEffectProcessing: Job? = null var currentSignalProcessing: Job? = null @@ -61,7 +61,7 @@ open class Transformer( requestDelegate = requestDelegate ) - fun startSignalCollection(incoming: SharedFlow) { + internal fun startSignalCollection(incoming: SharedFlow) { transformerScope.launch { incoming.collect { currentSignalProcessing = transformerScope.launch { @@ -72,11 +72,11 @@ open class Transformer( } } - fun startDataPublishing(data: SendChannel) { + internal fun startDataPublishing(data: SendChannel) { transformerScope.launch { dataChannel.receiveAsFlow().collect { data.send(it) } } } - fun startEffectProcessing( + internal fun startEffectProcessing( producer: SendChannel, incoming: SharedFlow ) { @@ -101,7 +101,7 @@ open class Transformer( } } - fun startQueryProcessing( + internal fun startQueryProcessing( incomingQuery: SharedFlow, outGoingQuery: SendChannel ) { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt index ae07bcc..3a34224 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt @@ -3,25 +3,8 @@ package com.trendyol.transmission.transformer.handler import com.trendyol.transmission.Transmission -import com.trendyol.transmission.transformer.Transformer import kotlin.reflect.KClass -class HandlerScope internal constructor(val handlerRegistry: HandlerRegistry) - -fun Transformer.handlerRegistry(scope: HandlerScope.() -> Unit): HandlerRegistry { - val handlerRegistry = HandlerRegistry() - HandlerScope(handlerRegistry).apply(scope) - return handlerRegistry -} - -inline fun HandlerScope.registerEffect(noinline lambda: suspend CommunicationScope.(effect: T) -> Unit) { - handlerRegistry.registerEffect(lambda) -} - -inline fun HandlerScope.registerSignal(noinline lambda: suspend CommunicationScope.(signal: T) -> Unit) { - handlerRegistry.registerSignal(lambda) -} - class HandlerRegistry internal constructor() { @PublishedApi @@ -33,15 +16,18 @@ class HandlerRegistry internal constructor() { mutableMapOf, suspend CommunicationScope.(effect: Transmission.Effect) -> Unit>() @PublishedApi - internal inline fun registerSignal(noinline lambda: suspend CommunicationScope.(signal: T) -> Unit) { + internal inline fun registerSignal( + noinline lambda: suspend CommunicationScope.(signal: T) -> Unit + ) { signalHandlerRegistry[T::class] = lambda as suspend CommunicationScope.(Transmission.Signal) -> Unit } @PublishedApi - internal inline fun registerEffect(noinline lambda: suspend CommunicationScope.(effect: T) -> Unit) { + internal inline fun registerEffect( + noinline lambda: suspend CommunicationScope.(effect: T) -> Unit + ) { effectHandlerRegistry[T::class] = lambda as suspend CommunicationScope.(Transmission.Effect) -> Unit } - } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerScope.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerScope.kt new file mode 100644 index 0000000..c9992b0 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerScope.kt @@ -0,0 +1,24 @@ +package com.trendyol.transmission.transformer.handler + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.Transformer + +class HandlerScope internal constructor(val handlerRegistry: HandlerRegistry) + +fun Transformer.handlerRegistry(scope: HandlerScope.() -> Unit): HandlerRegistry { + val handlerRegistry = HandlerRegistry() + HandlerScope(handlerRegistry).apply(scope) + return handlerRegistry +} + +inline fun HandlerScope.registerEffect( + noinline lambda: suspend CommunicationScope.(effect: T) -> Unit +) { + handlerRegistry.registerEffect(lambda) +} + +inline fun HandlerScope.registerSignal( + noinline lambda: suspend CommunicationScope.(signal: T) -> Unit +) { + handlerRegistry.registerSignal(lambda) +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt index 905067b..3eacaba 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt @@ -1,6 +1,6 @@ package com.trendyol.transmission.transformer.request -sealed interface Query { +internal sealed interface Query { data class Data( val sender: String, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt index f7974ba..2cc8fd3 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt @@ -13,11 +13,11 @@ import com.trendyol.transmission.transformer.request.RequestHandler * Can be queried using [RequestHandler.execute] * @param computation Computation to get the result [Transmission.Data] */ -fun , T : Any> ComputationRegistry.registerComputation( +fun , T : Any> ComputationScope.registerComputation( contract: C, computation: suspend RequestHandler.() -> T?, -): ComputationRegistry { - return this.apply { buildWith(contract.key, contract.useCache, computation) } +) { + this.computationRegistry.buildWith(contract.key, contract.useCache, computation) } /** @@ -28,9 +28,9 @@ fun , T : Any> ComputationRegistry.registerComputati * Can be queried using [RequestHandler.execute] * @param computation Computation to get the result [Transmission.Data] */ -fun , A : Any, T : Any> ComputationRegistry.registerComputation( +fun , A : Any, T : Any> ComputationScope.registerComputation( contract: C, computation: suspend RequestHandler.(args: A) -> T?, -): ComputationRegistry { - return this.apply { buildWith(contract.key, contract.useCache, computation) } +) { + this.computationRegistry.buildWith(contract.key, contract.useCache, computation) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationScope.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationScope.kt new file mode 100644 index 0000000..cc49ed9 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationScope.kt @@ -0,0 +1,11 @@ +package com.trendyol.transmission.transformer.request.computation + +import com.trendyol.transmission.transformer.Transformer + +class ComputationScope internal constructor(internal val computationRegistry: ComputationRegistry) + +fun Transformer.computationRegistry(scope: ComputationScope.() -> Unit): ComputationRegistry { + val computationRegistry = ComputationRegistry(this) + ComputationScope(computationRegistry).apply(scope) + return computationRegistry +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionExt.kt index 98c435d..5910089 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionExt.kt @@ -13,11 +13,11 @@ import com.trendyol.transmission.transformer.request.RequestHandler * Can be queried using [RequestHandler.execute] * @param execution execution to get the result [Transmission.Data] */ -fun ExecutionRegistry.registerExecution( +fun ExecutionScope.registerExecution( contract: C, execution: suspend RequestHandler.() -> Unit, -): ExecutionRegistry { - return this.apply { buildWith(contract.key, execution) } +) { + this.executionRegistry.buildWith(contract.key, execution) } /** @@ -29,9 +29,9 @@ fun ExecutionRegistry.registerExecution( * Can be queried using [RequestHandler.execute] * @param execution execution to get the result [Transmission.Data] */ -fun , A : Any> ExecutionRegistry.registerExecution( +fun , A : Any> ExecutionScope.registerExecution( contract: C, execution: suspend RequestHandler.(args: A) -> Unit, -): ExecutionRegistry { - return this.apply { buildWith(contract.key, execution) } +) { + this.executionRegistry.buildWith(contract.key, execution) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionScope.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionScope.kt new file mode 100644 index 0000000..8084435 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionScope.kt @@ -0,0 +1,11 @@ +package com.trendyol.transmission.transformer.request.execution + +import com.trendyol.transmission.transformer.Transformer + +class ExecutionScope internal constructor(internal val executionRegistry: ExecutionRegistry) + +fun Transformer.executionRegistry(scope: ExecutionScope.() -> Unit): ExecutionRegistry { + val executionRegistry = ExecutionRegistry(this) + ExecutionScope(executionRegistry).apply(scope) + return executionRegistry +}