From b426b88c123e388af6ee0ac13db410548fde21b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Sun, 9 Jun 2024 16:50:20 +0300 Subject: [PATCH 01/21] | Update OutputTransformer test on sample --- .../transmission/features/output/OutputTransformer.kt | 2 +- .../transmission/features/output/OutputTransformerTest.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 c7ebe1b..ce6e7e5 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 @@ -46,12 +46,12 @@ class OutputTransformer @Inject constructor( holder.update { it.copy(outputText = it.outputText + " and Selected color index is ${selectedColor.selectedColorIndex}") } - publish(effect = RouterEffect(holder.getValue())) delay(1.seconds) send( effect = ColorPickerEffect.BackgroundColorUpdate(holder2.getValue().backgroundColor), to = ColorPickerTransformer::class ) + publish(effect = RouterEffect(holder.getValue())) } is ColorPickerEffect.BackgroundColorUpdate -> { 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 c5a9063..d24e070 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 @@ -1,6 +1,6 @@ package com.trendyol.transmission.features.output -import com.trendyol.transmission.features.colorpicker.ColorPickerEffect +import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.features.input.InputEffect import com.trendyol.transmission.transformer.util.TestCoroutineRule import com.trendyol.transmission.ui.ColorPickerUiState @@ -44,7 +44,7 @@ class OutputTransformerTest { addQueryData(ColorPickerUiState(), key = "ColorPickerUiState") }) { assertEquals(OutputUiState(outputText = "test"), dataStream[1]) - assertTrue(effectStream.last().effect is ColorPickerEffect.BackgroundColorUpdate) + assertTrue(effectStream.last().effect is RouterEffect) } } } From 50e6472dfa7e92aa6e6e8ff00866b85146909483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Mon, 24 Jun 2024 09:00:00 +0300 Subject: [PATCH 02/21] Implement computation with arguments --- .../features/input/InputTransformer.kt | 3 ++ .../transmissiontesting/TestRouter.kt | 12 +++++ .../transmission/router/QueryDelegate.kt | 44 +++++++++++++++++++ .../transformer/CommunicationScopeBuilder.kt | 10 ++++- .../transformer/TransformerStorage.kt | 15 +++++++ .../transformer/query/ComputationExt.kt | 9 ++++ .../transformer/query/DataQuery.kt | 7 +++ .../transformer/query/QuerySender.kt | 6 +++ .../query/TransformerQueryDelegate.kt | 19 ++++++++ .../withargs/ComputationBuilderWithArgs.kt | 19 ++++++++ .../withargs/ComputationDelegateWithArgs.kt | 25 +++++++++++ .../withargs/ComputationOwnerWithArgs.kt | 12 +++++ 12 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt 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 7fabb9b..8b12ef3 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 @@ -24,6 +24,9 @@ class InputTransformer @Inject constructor( delay(1.seconds) WrittenInput(holder.getValue().writtenText) } + registerComputation(key = "WrittenInputWithArgs") { + WrittenInput(it) + } } override val signalHandler = buildTypedSignalHandler { signal -> diff --git a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt index e7ae12c..789b689 100644 --- a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt +++ b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt @@ -82,6 +82,7 @@ internal class TestRouter( when (query) { is Query.Computation -> processComputationQuery(query) is Query.Data -> processDataQuery(query) + is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) } } @@ -109,4 +110,15 @@ internal class TestRouter( 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/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt index 745c976..cd28040 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt @@ -44,6 +44,7 @@ internal class QueryDelegate( when (query) { is Query.Computation -> processComputationQuery(query) is Query.Data -> processDataQuery(query) + is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) } } @@ -89,6 +90,30 @@ internal class QueryDelegate( } } + private suspend fun processComputationQueryWithArgs( + query: Query.ComputationWithArgs + ) = queryScope.launch { + val computationHolder = routerRef.transformerSet + .find { it.storage.hasComputation(query.key) } + val computationToSend = queryScope.async { + QueryResult.Computation( + owner = query.sender, + key = query.key, + data = computationHolder?.storage?.getComputationWithArgsByKey(query.key) + ?.getResult(computationHolder.communicationScope, query.invalidate, query.args), + ) + } + if (query.sender == routerRef.routerName) { + queryScope.launch { + routerQueryResultChannel.emit(computationToSend.await()) + } + } else { + queryScope.launch { + queryResultChannel.trySend(computationToSend.await()) + } + } + } + override suspend fun queryData(key: String): D? { outGoingQuery.trySend( Query.Data(sender = routerRef.routerName, key = key) @@ -115,4 +140,23 @@ internal class QueryDelegate( .filter { it.key == key && it.owner == routerRef.routerName } .first().data } + + override suspend fun queryComputationWithArgs( + args: A, + key: String, + invalidate: Boolean + ): D? { + outGoingQuery.trySend( + Query.ComputationWithArgs( + sender = routerRef.routerName, + key = key, + args = args, + invalidate = invalidate + ) + ) + return routerQueryResultChannel + .filterIsInstance>() + .filter { it.key == key && it.owner == routerRef.routerName } + .first().data + } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt index ebe1c2f..05daa30 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt @@ -35,4 +35,12 @@ internal class CommunicationScopeBuilder( ): D? { return queryDelegate.interactor.queryComputation(key, invalidate) } -} \ No newline at end of file + + override suspend fun queryComputationWithArgs( + args: A, + key: String, + invalidate: Boolean + ): D? { + return queryDelegate.interactor.queryComputationWithArgs(args, key, invalidate) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt index 4b2ec4a..e832a52 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt @@ -4,6 +4,7 @@ import com.trendyol.transmission.Transmission import com.trendyol.transmission.identifier import com.trendyol.transmission.transformer.dataholder.HolderState import com.trendyol.transmission.transformer.query.ComputationOwner +import com.trendyol.transmission.transformer.query.withargs.ComputationOwnerWithArgs import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.update @@ -17,6 +18,9 @@ internal class TransformerStorage { private val internalComputationMap: MutableMap = mutableMapOf() + private val internalComputationMapWithArgs: MutableMap> = + mutableMapOf() + fun isHolderStateInitialized(): Boolean { return internalTransmissionHolderSet is HolderState.Initialized } @@ -57,6 +61,13 @@ internal class TransformerStorage { internalComputationMap[key] = delegate } + fun registerComputationWithArgs(key: String, delegate: ComputationOwnerWithArgs) { + require(!internalComputationMapWithArgs.containsKey(key)) { + "Multiple computations with the same key is not allowed: $key" + } + internalComputationMapWithArgs[key] = delegate + } + fun hasComputation(type: String): Boolean { return internalComputationMap.containsKey(type) } @@ -65,6 +76,10 @@ internal class TransformerStorage { return internalComputationMap[type] } + fun getComputationWithArgsByKey(type: String): ComputationOwnerWithArgs? { + return internalComputationMapWithArgs[type] as? ComputationOwnerWithArgs + } + fun getHolderDataByKey(key: String): Transmission.Data? { return holderDataReference.value[key] } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt index fb0698f..fb09f03 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt @@ -2,6 +2,7 @@ package com.trendyol.transmission.transformer.query import com.trendyol.transmission.Transmission import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.query.withargs.ComputationBuilderWithArgs /** * Throws [IllegalArgumentException] when multiple computations with the same return type @@ -18,3 +19,11 @@ inline fun Transformer.registerComputation( ) { ComputationBuilder().buildWith(key, useCache, this, computation) } + +inline fun Transformer.registerComputation( + key: String, + useCache: Boolean = false, + noinline computation: suspend QuerySender.(args: A) -> T?, +) { + ComputationBuilderWithArgs().buildWith(key, useCache, this, computation) +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt index 4e583f8..c361d8a 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt @@ -12,4 +12,11 @@ sealed interface Query { val key: String, val invalidate: Boolean = false, ) : Query + + data class ComputationWithArgs( + val sender: String, + val key: String, + val args: A, + val invalidate: Boolean = false, + ) : Query } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt index 7331542..aacbdab 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt @@ -10,4 +10,10 @@ interface QuerySender { key: String, invalidate: Boolean = false, ): D? + + suspend fun queryComputationWithArgs( + args: A, + key: String, + invalidate: Boolean = false, + ): D? } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt index 8a5b60a..b666c98 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt @@ -38,5 +38,24 @@ internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: Strin .filter { it.key == key && it.owner == identifier } .first().data } + + override suspend fun queryComputationWithArgs( + args: A, + key: String, + invalidate: Boolean + ): D? { + outGoingQuery.trySend( + Query.ComputationWithArgs( + sender = identifier, + key = key, + args = args, + invalidate = invalidate + ) + ) + return resultBroadcast.output + .filterIsInstance>() + .filter { it.key == key && it.owner == identifier } + .first().data + } } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt new file mode 100644 index 0000000..5b130db --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt @@ -0,0 +1,19 @@ +package com.trendyol.transmission.transformer.query.withargs + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.query.QuerySender + +class ComputationBuilderWithArgs { + fun buildWith( + key: String, + useCache: Boolean = false, + transformer: Transformer, + computation: suspend QuerySender.(args: A) -> T? + ) { + transformer.storage.registerComputationWithArgs( + key = key, + delegate = ComputationDelegateWithArgs(useCache = useCache, computation = computation) + ) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt new file mode 100644 index 0000000..aa23c1b --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt @@ -0,0 +1,25 @@ +package com.trendyol.transmission.transformer.query.withargs + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.query.QuerySender + +internal class ComputationDelegateWithArgs( + private val useCache: Boolean = false, + private val computation: suspend QuerySender.(args: A) -> Transmission.Data? +) : ComputationOwnerWithArgs { + + private var result: Transmission.Data? = null + + override suspend fun getResult( + scope: QuerySender, + invalidate: Boolean, + args: A + ): Transmission.Data? { + return if (useCache && invalidate.not()) { + result ?: computation(scope, args).also { result = it } + } else { + result = null + computation(scope, args) + } + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt new file mode 100644 index 0000000..da389a8 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt @@ -0,0 +1,12 @@ +package com.trendyol.transmission.transformer.query.withargs + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.query.QuerySender + +internal interface ComputationOwnerWithArgs { + suspend fun getResult( + scope: QuerySender, + invalidate: Boolean = false, + args: A + ): Transmission.Data? +} From 4ea442cf1af003610fb1bfac5082e98b4bf43fdf Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 24 Jun 2024 11:13:26 +0300 Subject: [PATCH 03/21] Add contract API --- .../transformer/query/Contract.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt new file mode 100644 index 0000000..e5509fd --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt @@ -0,0 +1,20 @@ +package com.trendyol.transmission.transformer.query + +import com.trendyol.transmission.Transmission + +sealed interface Contract { + + abstract class Data : Contract { + abstract val key: String + } + + abstract class Computation : Contract { + abstract val key: String + open val useCache: Boolean = false + } + + abstract class ComputationWithArgs : Contract { + abstract val key: String + open val useCache: Boolean = false + } +} From 4e6c1f136a62698ee62dd383211ef490983e3204 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 24 Jun 2024 11:13:45 +0300 Subject: [PATCH 04/21] Convert query method to be executed via defined contracts --- .../trendyol/transmission/SampleViewModel.kt | 6 ++-- .../colorpicker/ColorPickerTransformer.kt | 9 +++++- .../features/input/InputTransformer.kt | 19 ++++++++++-- .../multioutput/MultiOutputTransformer.kt | 3 +- .../features/output/OutputTransformer.kt | 17 +++++++---- .../transmission/router/QueryDelegate.kt | 23 +++++++------- .../transformer/CommunicationScopeBuilder.kt | 18 ++++++----- .../transformer/dataholder/DataHolderExt.kt | 9 +++--- .../transformer/query/ComputationExt.kt | 30 ++++++++++++------- .../transformer/query/QuerySender.kt | 10 +++---- .../query/TransformerQueryDelegate.kt | 23 +++++++------- 11 files changed, 105 insertions(+), 62 deletions(-) diff --git a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt index c727c43..970f982 100644 --- a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt +++ b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt @@ -3,6 +3,8 @@ package com.trendyol.transmission import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.trendyol.transmission.effect.RouterEffect +import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer +import com.trendyol.transmission.features.input.InputTransformer import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.InputUiState import com.trendyol.transmission.ui.MultiOutputUiState @@ -53,10 +55,10 @@ class SampleViewModel @Inject constructor( } } } - val inputData = transmissionRouter.queryHelper.queryData("InputUiState") + val inputData = transmissionRouter.queryHelper.queryData(InputTransformer.holderContract) delay(1.seconds) val colorPicker = - transmissionRouter.queryHelper.queryData("ColorPickerUiState") + transmissionRouter.queryHelper.queryData(ColorPickerTransformer.holderContract) _transmissionList.update { it.plus("Current InputData: $inputData") } _transmissionList.update { it.plus("Current ColorPickerData: $colorPicker") } } diff --git a/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt index 117c750..8ea0066 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt @@ -6,6 +6,7 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.handler.buildGenericSignalHandler +import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.ui.ColorPickerUiState import kotlinx.coroutines.CoroutineDispatcher import javax.inject.Inject @@ -14,7 +15,7 @@ class ColorPickerTransformer @Inject constructor( @DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher ) : Transformer(defaultDispatcher) { - private val holder = buildDataHolder(ColorPickerUiState(), key = "ColorPickerUiState") + private val holder = buildDataHolder(ColorPickerUiState(), holderContract) override val signalHandler = buildGenericSignalHandler { signal -> when (signal) { @@ -40,4 +41,10 @@ class ColorPickerTransformer @Inject constructor( } } } + + companion object { + val holderContract = object : Contract.Data() { + override val key: String = "ColorPickerUiState" + } + } } 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 8b12ef3..d26bf50 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 @@ -6,6 +6,7 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.handler.buildTypedSignalHandler +import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.transformer.query.registerComputation import com.trendyol.transmission.ui.InputUiState import kotlinx.coroutines.CoroutineDispatcher @@ -17,14 +18,14 @@ class InputTransformer @Inject constructor( @DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher ) : Transformer(defaultDispatcher) { - private val holder = buildDataHolder(InputUiState(), key = "InputUiState") + private val holder = buildDataHolder(InputUiState(), holderContract) init { - registerComputation(key = "WrittenInput") { + registerComputation(writtenInput) { delay(1.seconds) WrittenInput(holder.getValue().writtenText) } - registerComputation(key = "WrittenInputWithArgs") { + registerComputation(writtenInputWithArgs) { WrittenInput(it) } } @@ -45,4 +46,16 @@ class InputTransformer @Inject constructor( } } } + + companion object { + val writtenInputWithArgs = object : Contract.ComputationWithArgs() { + override val key: String = "WrittenInputWithArgs" + } + val writtenInput = object : Contract.Computation() { + override val key: String = "WrittenInput" + } + val holderContract = object : Contract.Data() { + override val key: String = "InputUiState" + } + } } diff --git a/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt index bc85da8..5ad5211 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt @@ -4,6 +4,7 @@ import com.trendyol.transmission.DefaultDispatcher import com.trendyol.transmission.features.colorpicker.ColorPickerEffect import com.trendyol.transmission.features.input.InputEffect import com.trendyol.transmission.features.output.OutputCalculationResult +import com.trendyol.transmission.features.output.OutputTransformer import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler @@ -21,7 +22,7 @@ class MultiOutputTransformer @Inject constructor( when (effect) { is InputEffect.InputUpdate -> { holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) } - val result = queryComputation("OutputCalculation") + val result = queryComputation(OutputTransformer.outputCalculationContract) holder.update { it.copy(writtenUppercaseText = it.writtenUppercaseText + " ${result?.result}") } 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 ce6e7e5..baa05b4 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 @@ -5,10 +5,11 @@ import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.features.colorpicker.ColorPickerEffect import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer import com.trendyol.transmission.features.input.InputEffect -import com.trendyol.transmission.features.input.WrittenInput +import com.trendyol.transmission.features.input.InputTransformer import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler +import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.transformer.query.registerComputation import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.OutputUiState @@ -27,10 +28,10 @@ class OutputTransformer @Inject constructor( private val holder2 = buildDataHolder(ColorPickerUiState(), publishUpdates = false) init { - registerComputation(key = "OutputCalculation") { + registerComputation(outputCalculationContract) { delay(2.seconds) - val data = queryData("ColorPickerUiState")?.selectedColorIndex - val writtenOutput = queryComputation(key = "WrittenInput") + val data = queryData(ColorPickerTransformer.holderContract)?.selectedColorIndex + val writtenOutput = queryComputation(InputTransformer.writtenInput) val result = Random.nextInt(5, 15) * Random.nextInt(5, 15) OutputCalculationResult("result is $result with ($writtenOutput) and $data") } @@ -41,7 +42,7 @@ class OutputTransformer @Inject constructor( is InputEffect.InputUpdate -> { holder.update { it.copy(outputText = effect.value) } delay(3.seconds) - val selectedColor = queryData("ColorPickerUiState") + val selectedColor = queryData(ColorPickerTransformer.holderContract) selectedColor ?: return@buildGenericEffectHandler holder.update { it.copy(outputText = it.outputText + " and Selected color index is ${selectedColor.selectedColorIndex}") @@ -59,4 +60,10 @@ class OutputTransformer @Inject constructor( } } } + + companion object { + val outputCalculationContract = object : Contract.Computation() { + override val key: String = "OutputCalculation" + } + } } diff --git a/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt index cd28040..2e46ba5 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt @@ -2,6 +2,7 @@ package com.trendyol.transmission.router import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter +import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.transformer.query.Query import com.trendyol.transmission.transformer.query.QueryResult import com.trendyol.transmission.transformer.query.QuerySender @@ -114,49 +115,49 @@ internal class QueryDelegate( } } - override suspend fun queryData(key: String): D? { + override suspend fun , D : Transmission.Data> queryData(contract: C): D? { outGoingQuery.trySend( - Query.Data(sender = routerRef.routerName, key = key) + Query.Data(sender = routerRef.routerName, key = contract.key) ) return routerQueryResultChannel .filterIsInstance>() - .filter { it.key == key && it.owner == routerRef.routerName } + .filter { it.key == contract.key && it.owner == routerRef.routerName } .first().data } - override suspend fun queryComputation( - key: String, + override suspend fun , D : Transmission.Data> queryComputation( + contract: C, invalidate: Boolean ): D? { outGoingQuery.trySend( Query.Computation( sender = routerRef.routerName, - key = key, + key = contract.key, invalidate = invalidate ) ) return routerQueryResultChannel .filterIsInstance>() - .filter { it.key == key && it.owner == routerRef.routerName } + .filter { it.key == contract.key && it.owner == routerRef.routerName } .first().data } - override suspend fun queryComputationWithArgs( + override suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + contract: C, args: A, - key: String, invalidate: Boolean ): D? { outGoingQuery.trySend( Query.ComputationWithArgs( sender = routerRef.routerName, - key = key, + key = contract.key, args = args, invalidate = invalidate ) ) return routerQueryResultChannel .filterIsInstance>() - .filter { it.key == key && it.owner == routerRef.routerName } + .filter { it.key == contract.key && it.owner == routerRef.routerName } .first().data } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt index 05daa30..e84c779 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt @@ -3,6 +3,7 @@ package com.trendyol.transmission.transformer import com.trendyol.transmission.Transmission import com.trendyol.transmission.effect.EffectWrapper import com.trendyol.transmission.transformer.handler.CommunicationScope +import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.transformer.query.TransformerQueryDelegate import kotlinx.coroutines.channels.Channel import kotlin.reflect.KClass @@ -25,22 +26,23 @@ internal class CommunicationScopeBuilder( effectChannel.trySend(EffectWrapper(effect)) } - override suspend fun queryData(key: String): D? { - return queryDelegate.interactor.queryData(key) + override suspend fun , D : Transmission.Data> queryData(contract: C): D? { + return queryDelegate.interactor.queryData(contract) } - override suspend fun queryComputation( - key: String, + override suspend fun , D : Transmission.Data> queryComputation( + contract: C, invalidate: Boolean ): D? { - return queryDelegate.interactor.queryComputation(key, invalidate) + return queryDelegate.interactor.queryComputation(contract, invalidate) } - override suspend fun queryComputationWithArgs( + override suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + contract: C, args: A, - key: String, invalidate: Boolean ): D? { - return queryDelegate.interactor.queryComputationWithArgs(args, key, invalidate) + return queryDelegate.interactor.queryComputationWithArgs(contract, args, invalidate) } + } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt index bc37ed2..d8d7b2d 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt @@ -3,6 +3,7 @@ package com.trendyol.transmission.transformer.dataholder import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.transformer.query.QuerySender /** @@ -14,15 +15,15 @@ import com.trendyol.transmission.transformer.query.QuerySender * @param [key] When defined, data inside the holder can be accessed by other Transformers in the * network using [QuerySender.queryData] * */ -inline fun Transformer.buildDataHolder( +fun Transformer.buildDataHolder( initialValue: T, - publishUpdates: Boolean = true, - key: String? = null, + contract: Contract.Data? = null, + publishUpdates: Boolean = true ): TransmissionDataHolder { return TransmissionDataHolderBuilder().buildWith( initialValue = initialValue, publishUpdates = publishUpdates, transformer = this, - holderKey = key, + holderKey = contract?.key, ) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt index fb09f03..70f21f0 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt @@ -5,25 +5,33 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.query.withargs.ComputationBuilderWithArgs /** - * Throws [IllegalArgumentException] when multiple computations with the same return type + * Throws [IllegalArgumentException] when multiple computations with the same key * are defined inside the [Transformer]. * * Adds a computation to [Transformer] to be queried. + * Can be queried using [QuerySender.queryComputation] * @param useCache Stores the result after first computation * @param computation Computation to get the result [Transmission.Data] */ -inline fun Transformer.registerComputation( - key: String, - useCache: Boolean = false, - noinline computation: suspend QuerySender.() -> T?, +fun , T : Transmission.Data> Transformer.registerComputation( + contract: C, + computation: suspend QuerySender.() -> T?, ) { - ComputationBuilder().buildWith(key, useCache, this, computation) + ComputationBuilder().buildWith(contract.key, contract.useCache, this, computation) } -inline fun Transformer.registerComputation( - key: String, - useCache: Boolean = false, - noinline computation: suspend QuerySender.(args: A) -> T?, +/** + * Throws [IllegalArgumentException] when multiple computations with the same key + * are defined inside the [Transformer]. + * + * Adds a computation to [Transformer] to be queried. This computation accepts any class as Argument. + * Can be queried using [QuerySender.queryComputationWithArgs] + * @param useCache Stores the result after first computation + * @param computation Computation to get the result [Transmission.Data] + */ +fun , A : Any, T : Transmission.Data> Transformer.registerComputation( + contract: C, + computation: suspend QuerySender.(args: A) -> T?, ) { - ComputationBuilderWithArgs().buildWith(key, useCache, this, computation) + ComputationBuilderWithArgs().buildWith(contract.key, contract.useCache, this, computation) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt index aacbdab..0b03359 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt @@ -4,16 +4,16 @@ import com.trendyol.transmission.Transmission interface QuerySender { - suspend fun queryData(key: String): D? + suspend fun , D : Transmission.Data> queryData(contract: C): D? - suspend fun queryComputation( - key: String, + suspend fun , D : Transmission.Data> queryComputation( + contract: C, invalidate: Boolean = false, ): D? - suspend fun queryComputationWithArgs( + suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + contract: C, args: A, - key: String, invalidate: Boolean = false, ): D? } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt index b666c98..c883889 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt @@ -14,47 +14,48 @@ internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: Strin val resultBroadcast = scope.createBroadcast>() val interactor: QuerySender = object : QuerySender { - override suspend fun queryData(key: String): D? { - outGoingQuery.trySend(Query.Data(sender = identifier, key = key)) + + override suspend fun , D : Transmission.Data> queryData(contract: C): D? { + outGoingQuery.trySend(Query.Data(sender = identifier, key = contract.key)) return resultBroadcast.output .filterIsInstance>() - .filter { it.key == key && it.owner == identifier } + .filter { it.key == contract.key && it.owner == identifier } .first().data } - override suspend fun queryComputation( - key: String, + override suspend fun , D : Transmission.Data> queryComputation( + contract: C, invalidate: Boolean ): D? { outGoingQuery.trySend( Query.Computation( sender = identifier, - key = key, + key = contract.key, invalidate = invalidate ) ) return resultBroadcast.output .filterIsInstance>() - .filter { it.key == key && it.owner == identifier } + .filter { it.key == contract.key && it.owner == identifier } .first().data } - override suspend fun queryComputationWithArgs( + override suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + contract: C, args: A, - key: String, invalidate: Boolean ): D? { outGoingQuery.trySend( Query.ComputationWithArgs( sender = identifier, - key = key, + key = contract.key, args = args, invalidate = invalidate ) ) return resultBroadcast.output .filterIsInstance>() - .filter { it.key == key && it.owner == identifier } + .filter { it.key == contract.key && it.owner == identifier } .first().data } } From 194903260b6bb4ee7ec62b8f38cb0e25110e1892 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 24 Jun 2024 15:55:54 +0300 Subject: [PATCH 05/21] Initialize new contract API and refactor QuerySender to RequestHandler --- .../transformer/query/Contract.kt | 15 ++++++++-- .../query/{DataQuery.kt => Query.kt} | 11 +++++++ .../transformer/query/QuerySender.kt | 19 ------------ .../transformer/query/RequestHandler.kt | 30 +++++++++++++++++++ 4 files changed, 53 insertions(+), 22 deletions(-) rename transmission/src/main/java/com/trendyol/transmission/transformer/query/{DataQuery.kt => Query.kt} (68%) delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt index e5509fd..e12f86b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt @@ -4,17 +4,26 @@ import com.trendyol.transmission.Transmission sealed interface Contract { - abstract class Data : Contract { + abstract class Data : Contract { abstract val key: String } - abstract class Computation : Contract { + abstract class Computation: Contract { abstract val key: String open val useCache: Boolean = false } - abstract class ComputationWithArgs : Contract { + abstract class ComputationWithArgs : Contract { abstract val key: String open val useCache: Boolean = false } + + abstract class Execution : Contract { + abstract val key: String + } + + abstract class ExecutionWithArgs : Contract { + abstract val key: String + } + } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/Query.kt similarity index 68% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/query/Query.kt index c361d8a..8161591 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/DataQuery.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/Query.kt @@ -19,4 +19,15 @@ sealed interface Query { val args: A, val invalidate: Boolean = false, ) : Query + + data class Execution( + val sender: String, + val key: String, + ) : Query + + data class ExecutionWithArgs( + val sender: String, + val key: String, + val args: A, + ) : Query } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt deleted file mode 100644 index 0b03359..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QuerySender.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.trendyol.transmission.transformer.query - -import com.trendyol.transmission.Transmission - -interface QuerySender { - - suspend fun , D : Transmission.Data> queryData(contract: C): D? - - suspend fun , D : Transmission.Data> queryComputation( - contract: C, - invalidate: Boolean = false, - ): D? - - suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( - contract: C, - args: A, - invalidate: Boolean = false, - ): D? -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt new file mode 100644 index 0000000..1ff1a82 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt @@ -0,0 +1,30 @@ +package com.trendyol.transmission.transformer.query + +import com.trendyol.transmission.Transmission + +interface RequestHandler { + + suspend fun , D : Transmission.Data> getData(contract: C): D? + + suspend fun , D : Any> compute( + contract: C, + invalidate: Boolean = false, + ): D? + + suspend fun , A : Any, D : Any> compute( + contract: C, + args: A, + invalidate: Boolean = false, + ): D? + + suspend fun compute( + contract: C, + invalidate: Boolean = false, + ) + + suspend fun , A : Any> compute( + contract: C, + args: A, + invalidate: Boolean = false, + ) +} From 9613b0459ee234a14ffe159f2853276c89f708d0 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 24 Jun 2024 16:00:16 +0300 Subject: [PATCH 06/21] Fix request handler API naming --- .../trendyol/transmission/transformer/query/RequestHandler.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt index 1ff1a82..861e778 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt @@ -17,12 +17,12 @@ interface RequestHandler { invalidate: Boolean = false, ): D? - suspend fun compute( + suspend fun execute( contract: C, invalidate: Boolean = false, ) - suspend fun , A : Any> compute( + suspend fun , A : Any> execute( contract: C, args: A, invalidate: Boolean = false, From 9ee141bd8aa463437788ada4842724b9166f1ca2 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Tue, 25 Jun 2024 09:09:45 +0300 Subject: [PATCH 07/21] Rename queryDelegate to requestDelegate --- .../transmission/TransmissionRouter.kt | 12 ++++----- .../{QueryDelegate.kt => RequestDelegate.kt} | 26 ++++++++++++++----- .../transformer/dataholder/DataHolderExt.kt | 4 +-- .../transformer/handler/CommunicationScope.kt | 4 +-- .../transformer/query/ComputationBuilder.kt | 2 +- .../transformer/query/ComputationDelegate.kt | 4 +-- .../transformer/query/ComputationExt.kt | 8 +++--- 7 files changed, 37 insertions(+), 23 deletions(-) rename transmission/src/main/java/com/trendyol/transmission/router/{QueryDelegate.kt => RequestDelegate.kt} (89%) diff --git a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt index 6094781..391bb69 100644 --- a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt +++ b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt @@ -1,10 +1,10 @@ package com.trendyol.transmission import com.trendyol.transmission.effect.EffectWrapper -import com.trendyol.transmission.router.QueryDelegate +import com.trendyol.transmission.router.RequestDelegate import com.trendyol.transmission.router.createBroadcast import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -36,8 +36,8 @@ class TransmissionRouter( val effectStream: SharedFlow = effectBroadcast.output.map { it.effect } .shareIn(routerScope, SharingStarted.Lazily) - private val _queryDelegate = QueryDelegate(routerScope, this@TransmissionRouter) - val queryHelper: QuerySender = _queryDelegate + private val _requestDelegate = RequestDelegate(routerScope, this@TransmissionRouter) + val requestHelper: RequestHandler = _requestDelegate init { initialize() @@ -61,8 +61,8 @@ class TransmissionRouter( incoming = effectBroadcast.output ) startQueryProcessing( - incomingQuery = _queryDelegate.incomingQueryResponse, - outGoingQuery = _queryDelegate.outGoingQuery + incomingQuery = _requestDelegate.incomingQueryResponse, + outGoingQuery = _requestDelegate.outGoingQuery ) } } diff --git a/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt similarity index 89% rename from transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt rename to transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt index 2e46ba5..18f4443 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/QueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -5,7 +5,7 @@ import com.trendyol.transmission.TransmissionRouter import com.trendyol.transmission.transformer.query.Contract import com.trendyol.transmission.transformer.query.Query import com.trendyol.transmission.transformer.query.QueryResult -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel @@ -19,10 +19,10 @@ import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.launch -internal class QueryDelegate( +internal class RequestDelegate( private val queryScope: CoroutineScope, private val routerRef: TransmissionRouter -) : QuerySender { +) : RequestHandler { private val routerQueryResultChannel: MutableSharedFlow> = MutableSharedFlow() @@ -46,6 +46,8 @@ internal class QueryDelegate( is Query.Computation -> processComputationQuery(query) is Query.Data -> processDataQuery(query) is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) + is Query.Execution -> TODO() + is Query.ExecutionWithArgs<*> -> TODO() } } @@ -115,7 +117,7 @@ internal class QueryDelegate( } } - override suspend fun , D : Transmission.Data> queryData(contract: C): D? { + override suspend fun , D : Transmission.Data> getData(contract: C): D? { outGoingQuery.trySend( Query.Data(sender = routerRef.routerName, key = contract.key) ) @@ -125,7 +127,7 @@ internal class QueryDelegate( .first().data } - override suspend fun , D : Transmission.Data> queryComputation( + override suspend fun , D : Any> compute( contract: C, invalidate: Boolean ): D? { @@ -142,7 +144,7 @@ internal class QueryDelegate( .first().data } - override suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + override suspend fun , A : Any, D : Any> compute( contract: C, args: A, invalidate: Boolean @@ -160,4 +162,16 @@ internal class QueryDelegate( .filter { it.key == contract.key && it.owner == routerRef.routerName } .first().data } + + override suspend fun execute(contract: C, invalidate: Boolean) { + TODO("Not yet implemented") + } + + override suspend fun , A : Any> execute( + contract: C, + args: A, + invalidate: Boolean + ) { + TODO("Not yet implemented") + } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt index d8d7b2d..842abc4 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt @@ -4,7 +4,7 @@ import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.query.Contract -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler /** * Throws [IllegalArgumentException] when multiple data holders with same type @@ -13,7 +13,7 @@ import com.trendyol.transmission.transformer.query.QuerySender * Must be a type extended from [Transmission.Data] * @param [publishUpdates] Controls sending updates to the [TransmissionRouter] * @param [key] When defined, data inside the holder can be accessed by other Transformers in the - * network using [QuerySender.queryData] + * network using [RequestHandler.query] * */ fun Transformer.buildDataHolder( initialValue: T, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt index 40306f6..7f18340 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt @@ -3,10 +3,10 @@ package com.trendyol.transmission.transformer.handler import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler import kotlin.reflect.KClass -interface CommunicationScope: QuerySender { +interface CommunicationScope: RequestHandler { /** * Sends data to [TransmissionRouter] * @param data of type [Transmission.Data] diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt index 29113cd..1036a7a 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt @@ -8,7 +8,7 @@ class ComputationBuilder { key: String, useCache: Boolean = false, transformer: Transformer, - computation: suspend QuerySender.() -> T? + computation: suspend RequestHandler.() -> T? ) { transformer.storage.registerComputation( key = key, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt index 101e0cd..eb68d87 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt @@ -4,12 +4,12 @@ import com.trendyol.transmission.Transmission internal class ComputationDelegate( private val useCache: Boolean = false, - private val computation: suspend QuerySender.() -> Transmission.Data? + private val computation: suspend RequestHandler.() -> Transmission.Data? ) : ComputationOwner { private var result: Transmission.Data? = null - override suspend fun getResult(scope: QuerySender, invalidate: Boolean): Transmission.Data? { + override suspend fun getResult(scope: RequestHandler, invalidate: Boolean): Transmission.Data? { return if (useCache && invalidate.not()) { result ?: computation(scope).also { result = it } } else { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt index 70f21f0..da8b387 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt @@ -9,13 +9,13 @@ import com.trendyol.transmission.transformer.query.withargs.ComputationBuilderWi * are defined inside the [Transformer]. * * Adds a computation to [Transformer] to be queried. - * Can be queried using [QuerySender.queryComputation] + * Can be queried using [RequestHandler.execute] * @param useCache Stores the result after first computation * @param computation Computation to get the result [Transmission.Data] */ fun , T : Transmission.Data> Transformer.registerComputation( contract: C, - computation: suspend QuerySender.() -> T?, + computation: suspend RequestHandler.() -> T?, ) { ComputationBuilder().buildWith(contract.key, contract.useCache, this, computation) } @@ -25,13 +25,13 @@ fun , T : Transmission.Data> Transformer.registerCom * are defined inside the [Transformer]. * * Adds a computation to [Transformer] to be queried. This computation accepts any class as Argument. - * Can be queried using [QuerySender.queryComputationWithArgs] + * Can be queried using [RequestHandler.execute] * @param useCache Stores the result after first computation * @param computation Computation to get the result [Transmission.Data] */ fun , A : Any, T : Transmission.Data> Transformer.registerComputation( contract: C, - computation: suspend QuerySender.(args: A) -> T?, + computation: suspend RequestHandler.(args: A) -> T?, ) { ComputationBuilderWithArgs().buildWith(contract.key, contract.useCache, this, computation) } From ea4a435dc3254f712dc6489e3c3703995cb84ca2 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Tue, 25 Jun 2024 09:11:34 +0300 Subject: [PATCH 08/21] Move old implementation to new API --- .../transformer/CommunicationScopeBuilder.kt | 23 ++++++++++++++----- .../query/TransformerQueryDelegate.kt | 20 ++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt index e84c779..27cdacd 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt @@ -26,23 +26,34 @@ internal class CommunicationScopeBuilder( effectChannel.trySend(EffectWrapper(effect)) } - override suspend fun , D : Transmission.Data> queryData(contract: C): D? { - return queryDelegate.interactor.queryData(contract) + override suspend fun , D : Transmission.Data> getData(contract: C): D? { + return queryDelegate.interactor.getData(contract) } - override suspend fun , D : Transmission.Data> queryComputation( + override suspend fun , D : Any> compute( contract: C, invalidate: Boolean ): D? { - return queryDelegate.interactor.queryComputation(contract, invalidate) + return queryDelegate.interactor.compute(contract, invalidate) } - override suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + override suspend fun , A : Any, D : Any> compute( contract: C, args: A, invalidate: Boolean ): D? { - return queryDelegate.interactor.queryComputationWithArgs(contract, args, invalidate) + return queryDelegate.interactor.compute(contract, args, invalidate) } + override suspend fun execute(contract: C, invalidate: Boolean) { + TODO("Not yet implemented") + } + + override suspend fun , A : Any> execute( + contract: C, + args: A, + invalidate: Boolean + ) { + TODO("Not yet implemented") + } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt index c883889..9b7a1e7 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt @@ -13,9 +13,9 @@ internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: Strin val outGoingQuery: Channel = Channel(capacity = Channel.BUFFERED) val resultBroadcast = scope.createBroadcast>() - val interactor: QuerySender = object : QuerySender { + val interactor: RequestHandler = object : RequestHandler { - override suspend fun , D : Transmission.Data> queryData(contract: C): D? { + override suspend fun , D : Transmission.Data> getData(contract: C): D? { outGoingQuery.trySend(Query.Data(sender = identifier, key = contract.key)) return resultBroadcast.output .filterIsInstance>() @@ -23,7 +23,7 @@ internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: Strin .first().data } - override suspend fun , D : Transmission.Data> queryComputation( + override suspend fun , D : Any> compute( contract: C, invalidate: Boolean ): D? { @@ -40,7 +40,7 @@ internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: Strin .first().data } - override suspend fun , A : Any, D : Transmission.Data> queryComputationWithArgs( + override suspend fun , A : Any, D : Any> compute( contract: C, args: A, invalidate: Boolean @@ -58,5 +58,17 @@ internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: Strin .filter { it.key == contract.key && it.owner == identifier } .first().data } + + override suspend fun execute(contract: C, invalidate: Boolean) { + TODO("Not yet implemented") + } + + override suspend fun , A : Any> execute( + contract: C, + args: A, + invalidate: Boolean + ) { + TODO("Not yet implemented") + } } } From 690758ce1bc668484f0cdd66b12a343d46f29d81 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Tue, 25 Jun 2024 09:47:52 +0300 Subject: [PATCH 09/21] Make sample and refactor work --- .../com/trendyol/transmission/SampleViewModel.kt | 4 ++-- .../transmission/features/input/InputTransformer.kt | 11 ++++++----- .../features/multioutput/MultiOutputTransformer.kt | 3 +-- .../features/output/OutputTransformer.kt | 6 +++--- .../trendyol/transmission/router/RequestDelegate.kt | 6 ++---- .../transmission/transformer/Transformer.kt | 2 +- .../transformer/query/ComputationBuilder.kt | 3 +-- .../transformer/query/ComputationDelegate.kt | 8 +++----- .../transformer/query/ComputationExt.kt | 2 +- .../transformer/query/ComputationOwner.kt | 4 +--- .../transmission/transformer/query/QueryResult.kt | 13 ++++++------- .../transformer/query/TransformerQueryDelegate.kt | 2 +- .../query/withargs/ComputationBuilderWithArgs.kt | 7 +++---- .../query/withargs/ComputationDelegateWithArgs.kt | 11 +++++------ .../query/withargs/ComputationOwnerWithArgs.kt | 6 +++--- 15 files changed, 39 insertions(+), 49 deletions(-) diff --git a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt index 970f982..58acec1 100644 --- a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt +++ b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt @@ -55,10 +55,10 @@ class SampleViewModel @Inject constructor( } } } - val inputData = transmissionRouter.queryHelper.queryData(InputTransformer.holderContract) + val inputData = transmissionRouter.requestHelper.getData(InputTransformer.holderContract) delay(1.seconds) val colorPicker = - transmissionRouter.queryHelper.queryData(ColorPickerTransformer.holderContract) + transmissionRouter.requestHelper.getData(ColorPickerTransformer.holderContract) _transmissionList.update { it.plus("Current InputData: $inputData") } _transmissionList.update { it.plus("Current ColorPickerData: $colorPicker") } } 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 d26bf50..5c1e4b2 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 @@ -21,7 +21,7 @@ class InputTransformer @Inject constructor( private val holder = buildDataHolder(InputUiState(), holderContract) init { - registerComputation(writtenInput) { + registerComputation(writtenInputContract) { delay(1.seconds) WrittenInput(holder.getValue().writtenText) } @@ -48,10 +48,11 @@ class InputTransformer @Inject constructor( } companion object { - val writtenInputWithArgs = object : Contract.ComputationWithArgs() { - override val key: String = "WrittenInputWithArgs" - } - val writtenInput = object : Contract.Computation() { + val writtenInputWithArgs = + object : Contract.ComputationWithArgs() { + override val key: String = "WrittenInputWithArgs" + } + val writtenInputContract = object : Contract.Computation() { override val key: String = "WrittenInput" } val holderContract = object : Contract.Data() { diff --git a/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt index 5ad5211..1a9eb6a 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/multioutput/MultiOutputTransformer.kt @@ -3,7 +3,6 @@ package com.trendyol.transmission.features.multioutput import com.trendyol.transmission.DefaultDispatcher import com.trendyol.transmission.features.colorpicker.ColorPickerEffect import com.trendyol.transmission.features.input.InputEffect -import com.trendyol.transmission.features.output.OutputCalculationResult import com.trendyol.transmission.features.output.OutputTransformer import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder @@ -22,7 +21,7 @@ class MultiOutputTransformer @Inject constructor( when (effect) { is InputEffect.InputUpdate -> { holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) } - val result = queryComputation(OutputTransformer.outputCalculationContract) + val result = compute(OutputTransformer.outputCalculationContract) holder.update { it.copy(writtenUppercaseText = it.writtenUppercaseText + " ${result?.result}") } 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 baa05b4..ca1a2d4 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 @@ -30,8 +30,8 @@ class OutputTransformer @Inject constructor( init { registerComputation(outputCalculationContract) { delay(2.seconds) - val data = queryData(ColorPickerTransformer.holderContract)?.selectedColorIndex - val writtenOutput = queryComputation(InputTransformer.writtenInput) + 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") } @@ -42,7 +42,7 @@ class OutputTransformer @Inject constructor( is InputEffect.InputUpdate -> { holder.update { it.copy(outputText = effect.value) } delay(3.seconds) - val selectedColor = queryData(ColorPickerTransformer.holderContract) + val selectedColor = getData(ColorPickerTransformer.holderContract) selectedColor ?: return@buildGenericEffectHandler holder.update { it.copy(outputText = it.outputText + " and Selected color index is ${selectedColor.selectedColorIndex}") 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 18f4443..6204272 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -24,13 +24,11 @@ internal class RequestDelegate( private val routerRef: TransmissionRouter ) : RequestHandler { - private val routerQueryResultChannel: MutableSharedFlow> = - MutableSharedFlow() + private val routerQueryResultChannel: MutableSharedFlow = MutableSharedFlow() val outGoingQuery: Channel = Channel(capacity = Channel.BUFFERED) - private val queryResultChannel: Channel> = - Channel(capacity = Channel.BUFFERED) + private val queryResultChannel: Channel = Channel(capacity = Channel.BUFFERED) val incomingQueryResponse = queryResultChannel.receiveAsFlow() .shareIn(queryScope, SharingStarted.Lazily) 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 965b8bf..88ccdf1 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -93,7 +93,7 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) { } fun startQueryProcessing( - incomingQuery: SharedFlow>, + incomingQuery: SharedFlow, outGoingQuery: SendChannel ) { transformerScope.launch { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt index 1036a7a..6a790e8 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt @@ -1,9 +1,8 @@ package com.trendyol.transmission.transformer.query -import com.trendyol.transmission.Transmission import com.trendyol.transmission.transformer.Transformer -class ComputationBuilder { +class ComputationBuilder { fun buildWith( key: String, useCache: Boolean = false, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt index eb68d87..bc2544d 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt @@ -1,15 +1,13 @@ package com.trendyol.transmission.transformer.query -import com.trendyol.transmission.Transmission - internal class ComputationDelegate( private val useCache: Boolean = false, - private val computation: suspend RequestHandler.() -> Transmission.Data? + private val computation: suspend RequestHandler.() -> Any? ) : ComputationOwner { - private var result: Transmission.Data? = null + private var result: Any? = null - override suspend fun getResult(scope: RequestHandler, invalidate: Boolean): Transmission.Data? { + override suspend fun getResult(scope: RequestHandler, invalidate: Boolean): Any? { return if (useCache && invalidate.not()) { result ?: computation(scope).also { result = it } } else { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt index da8b387..e85bcf7 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt @@ -29,7 +29,7 @@ fun , T : Transmission.Data> Transformer.registerCom * @param useCache Stores the result after first computation * @param computation Computation to get the result [Transmission.Data] */ -fun , A : Any, T : Transmission.Data> Transformer.registerComputation( +fun , A : Any, T : Any> Transformer.registerComputation( contract: C, computation: suspend RequestHandler.(args: A) -> T?, ) { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt index 694123f..e3741c2 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt @@ -1,7 +1,5 @@ package com.trendyol.transmission.transformer.query -import com.trendyol.transmission.Transmission - internal interface ComputationOwner { - suspend fun getResult(scope: QuerySender, invalidate: Boolean = false): Transmission.Data? + suspend fun getResult(scope: RequestHandler, invalidate: Boolean = false): Any? } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt index beb671b..f1b7e7b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt @@ -2,20 +2,19 @@ package com.trendyol.transmission.transformer.query import com.trendyol.transmission.Transmission -sealed class QueryResult( +sealed class QueryResult( open val owner: String, open val key: String, - open val data: D?, ) { data class Data( override val owner: String, override val key: String, - override val data: D?, - ) : QueryResult(owner, key, data) + val data: D?, + ) : QueryResult(owner, key) - data class Computation( + data class Computation( override val owner: String, override val key: String, - override val data: D?, - ) : QueryResult(owner, key, data) + val data: D?, + ) : QueryResult(owner, key) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt index 9b7a1e7..daade1a 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt @@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.first internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: String) { val outGoingQuery: Channel = Channel(capacity = Channel.BUFFERED) - val resultBroadcast = scope.createBroadcast>() + val resultBroadcast = scope.createBroadcast() val interactor: RequestHandler = object : RequestHandler { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt index 5b130db..cd85a48 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt @@ -1,15 +1,14 @@ package com.trendyol.transmission.transformer.query.withargs -import com.trendyol.transmission.Transmission import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler -class ComputationBuilderWithArgs { +class ComputationBuilderWithArgs { fun buildWith( key: String, useCache: Boolean = false, transformer: Transformer, - computation: suspend QuerySender.(args: A) -> T? + computation: suspend RequestHandler.(args: A) -> T? ) { transformer.storage.registerComputationWithArgs( key = key, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt index aa23c1b..d270ab4 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt @@ -1,20 +1,19 @@ package com.trendyol.transmission.transformer.query.withargs -import com.trendyol.transmission.Transmission -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler internal class ComputationDelegateWithArgs( private val useCache: Boolean = false, - private val computation: suspend QuerySender.(args: A) -> Transmission.Data? + private val computation: suspend RequestHandler.(args: A) -> Any? ) : ComputationOwnerWithArgs { - private var result: Transmission.Data? = null + private var result: Any? = null override suspend fun getResult( - scope: QuerySender, + scope: RequestHandler, invalidate: Boolean, args: A - ): Transmission.Data? { + ): Any? { return if (useCache && invalidate.not()) { result ?: computation(scope, args).also { result = it } } else { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt index da389a8..644a522 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt @@ -1,12 +1,12 @@ package com.trendyol.transmission.transformer.query.withargs import com.trendyol.transmission.Transmission -import com.trendyol.transmission.transformer.query.QuerySender +import com.trendyol.transmission.transformer.query.RequestHandler internal interface ComputationOwnerWithArgs { suspend fun getResult( - scope: QuerySender, + scope: RequestHandler, invalidate: Boolean = false, args: A - ): Transmission.Data? + ): Any? } From f5ace7c727be728a98abcf188d972f63c341e184 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Tue, 25 Jun 2024 12:12:00 +0300 Subject: [PATCH 10/21] Convert compute api to work with current system --- .../colorpicker/ColorPickerTransformer.kt | 2 +- .../features/input/InputTransformer.kt | 4 +- .../features/output/OutputTransformer.kt | 4 +- .../transmissiontesting/TestRouter.kt | 4 +- .../transmission/TransmissionRouter.kt | 2 +- .../transmission/router/RequestDelegate.kt | 11 +++--- .../transformer/CommunicationScopeBuilder.kt | 4 +- .../transmission/transformer/Transformer.kt | 6 +-- .../transformer/TransformerStorage.kt | 21 +++-------- .../transformer/dataholder/DataHolderExt.kt | 4 +- .../transformer/handler/CommunicationScope.kt | 2 +- .../transformer/query/ComputationBuilder.kt | 17 --------- .../transformer/query/ComputationDelegate.kt | 18 --------- .../transformer/query/ComputationOwner.kt | 5 --- .../withargs/ComputationBuilderWithArgs.kt | 18 --------- .../withargs/ComputationDelegateWithArgs.kt | 24 ------------ .../withargs/ComputationOwnerWithArgs.kt | 12 ------ .../{query => request}/Contract.kt | 2 +- .../transformer/{query => request}/Query.kt | 2 +- .../{query => request}/QueryResult.kt | 2 +- .../{query => request}/RequestHandler.kt | 2 +- .../TransformerQueryDelegate.kt | 2 +- .../request/computation/ComputationBuilder.kt | 31 ++++++++++++++++ .../computation/ComputationDelegateImpl.kt | 37 +++++++++++++++++++ .../computation}/ComputationExt.kt | 9 +++-- .../request/computation/ComputationOwner.kt | 18 +++++++++ 26 files changed, 123 insertions(+), 140 deletions(-) delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt rename transmission/src/main/java/com/trendyol/transmission/transformer/{query => request}/Contract.kt (92%) rename transmission/src/main/java/com/trendyol/transmission/transformer/{query => request}/Query.kt (92%) rename transmission/src/main/java/com/trendyol/transmission/transformer/{query => request}/QueryResult.kt (89%) rename transmission/src/main/java/com/trendyol/transmission/transformer/{query => request}/RequestHandler.kt (93%) rename transmission/src/main/java/com/trendyol/transmission/transformer/{query => request}/TransformerQueryDelegate.kt (98%) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationBuilder.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationDelegateImpl.kt rename transmission/src/main/java/com/trendyol/transmission/transformer/{query => request/computation}/ComputationExt.kt (76%) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationOwner.kt diff --git a/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt index 8ea0066..1f6541b 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt @@ -6,7 +6,7 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.handler.buildGenericSignalHandler -import com.trendyol.transmission.transformer.query.Contract +import com.trendyol.transmission.transformer.request.Contract import com.trendyol.transmission.ui.ColorPickerUiState import kotlinx.coroutines.CoroutineDispatcher import javax.inject.Inject 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 5c1e4b2..71187ef 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 @@ -6,8 +6,8 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.handler.buildTypedSignalHandler -import com.trendyol.transmission.transformer.query.Contract -import com.trendyol.transmission.transformer.query.registerComputation +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.computation.registerComputation import com.trendyol.transmission.ui.InputUiState import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.delay 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 ca1a2d4..15371d1 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 @@ -9,8 +9,8 @@ import com.trendyol.transmission.features.input.InputTransformer import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler -import com.trendyol.transmission.transformer.query.Contract -import com.trendyol.transmission.transformer.query.registerComputation +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.computation.registerComputation import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.OutputUiState import kotlinx.coroutines.CoroutineDispatcher diff --git a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt index 789b689..80b0ca2 100644 --- a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt +++ b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt @@ -4,8 +4,8 @@ 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.query.Query -import com.trendyol.transmission.transformer.query.QueryResult +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 diff --git a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt index 391bb69..aaca9e2 100644 --- a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt +++ b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt @@ -4,7 +4,7 @@ import com.trendyol.transmission.effect.EffectWrapper import com.trendyol.transmission.router.RequestDelegate import com.trendyol.transmission.router.createBroadcast import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.RequestHandler +import com.trendyol.transmission.transformer.request.RequestHandler import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers 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 6204272..170e741 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -2,10 +2,11 @@ package com.trendyol.transmission.router import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter -import com.trendyol.transmission.transformer.query.Contract -import com.trendyol.transmission.transformer.query.Query -import com.trendyol.transmission.transformer.query.QueryResult -import com.trendyol.transmission.transformer.query.RequestHandler +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.Query +import com.trendyol.transmission.transformer.request.QueryResult +import com.trendyol.transmission.transformer.request.RequestHandler +import com.trendyol.transmission.transformer.request.computation.ComputationOwner import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel @@ -100,7 +101,7 @@ internal class RequestDelegate( QueryResult.Computation( owner = query.sender, key = query.key, - data = computationHolder?.storage?.getComputationWithArgsByKey(query.key) + data = computationHolder?.storage?.getComputationByKey(query.key) ?.getResult(computationHolder.communicationScope, query.invalidate, query.args), ) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt index 27cdacd..91668a4 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt @@ -3,8 +3,8 @@ package com.trendyol.transmission.transformer import com.trendyol.transmission.Transmission import com.trendyol.transmission.effect.EffectWrapper import com.trendyol.transmission.transformer.handler.CommunicationScope -import com.trendyol.transmission.transformer.query.Contract -import com.trendyol.transmission.transformer.query.TransformerQueryDelegate +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.TransformerQueryDelegate import kotlinx.coroutines.channels.Channel import kotlin.reflect.KClass 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 88ccdf1..94ca9b3 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -7,9 +7,9 @@ import com.trendyol.transmission.identifier import com.trendyol.transmission.transformer.handler.CommunicationScope import com.trendyol.transmission.transformer.handler.EffectHandler import com.trendyol.transmission.transformer.handler.SignalHandler -import com.trendyol.transmission.transformer.query.Query -import com.trendyol.transmission.transformer.query.QueryResult -import com.trendyol.transmission.transformer.query.TransformerQueryDelegate +import com.trendyol.transmission.transformer.request.Query +import com.trendyol.transmission.transformer.request.QueryResult +import com.trendyol.transmission.transformer.request.TransformerQueryDelegate import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt index e832a52..e066b4b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt @@ -3,8 +3,7 @@ package com.trendyol.transmission.transformer import com.trendyol.transmission.Transmission import com.trendyol.transmission.identifier import com.trendyol.transmission.transformer.dataholder.HolderState -import com.trendyol.transmission.transformer.query.ComputationOwner -import com.trendyol.transmission.transformer.query.withargs.ComputationOwnerWithArgs +import com.trendyol.transmission.transformer.request.computation.ComputationOwner import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.update @@ -18,9 +17,6 @@ internal class TransformerStorage { private val internalComputationMap: MutableMap = mutableMapOf() - private val internalComputationMapWithArgs: MutableMap> = - mutableMapOf() - fun isHolderStateInitialized(): Boolean { return internalTransmissionHolderSet is HolderState.Initialized } @@ -61,23 +57,16 @@ internal class TransformerStorage { internalComputationMap[key] = delegate } - fun registerComputationWithArgs(key: String, delegate: ComputationOwnerWithArgs) { - require(!internalComputationMapWithArgs.containsKey(key)) { - "Multiple computations with the same key is not allowed: $key" - } - internalComputationMapWithArgs[key] = delegate - } - fun hasComputation(type: String): Boolean { return internalComputationMap.containsKey(type) } - fun getComputationByKey(type: String): ComputationOwner? { - return internalComputationMap[type] + fun getComputationByKey(type: String): ComputationOwner.Default? { + return internalComputationMap[type] as? ComputationOwner.Default } - fun getComputationWithArgsByKey(type: String): ComputationOwnerWithArgs? { - return internalComputationMapWithArgs[type] as? ComputationOwnerWithArgs + fun getComputationByKey(type: String): ComputationOwner.WithArgs? { + return internalComputationMap[type] as? ComputationOwner.WithArgs } fun getHolderDataByKey(key: String): Transmission.Data? { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt index 842abc4..e71f740 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/DataHolderExt.kt @@ -3,8 +3,8 @@ package com.trendyol.transmission.transformer.dataholder import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.Contract -import com.trendyol.transmission.transformer.query.RequestHandler +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.RequestHandler /** * Throws [IllegalArgumentException] when multiple data holders with same type diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt index 7f18340..5759958 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/CommunicationScope.kt @@ -3,7 +3,7 @@ package com.trendyol.transmission.transformer.handler import com.trendyol.transmission.Transmission import com.trendyol.transmission.TransmissionRouter import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.RequestHandler +import com.trendyol.transmission.transformer.request.RequestHandler import kotlin.reflect.KClass interface CommunicationScope: RequestHandler { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt deleted file mode 100644 index 6a790e8..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationBuilder.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.trendyol.transmission.transformer.query - -import com.trendyol.transmission.transformer.Transformer - -class ComputationBuilder { - fun buildWith( - key: String, - useCache: Boolean = false, - transformer: Transformer, - computation: suspend RequestHandler.() -> T? - ) { - transformer.storage.registerComputation( - key = key, - delegate = ComputationDelegate(useCache = useCache, computation = computation) - ) - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt deleted file mode 100644 index bc2544d..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationDelegate.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.trendyol.transmission.transformer.query - -internal class ComputationDelegate( - private val useCache: Boolean = false, - private val computation: suspend RequestHandler.() -> Any? -) : ComputationOwner { - - private var result: Any? = null - - override suspend fun getResult(scope: RequestHandler, invalidate: Boolean): Any? { - return if (useCache && invalidate.not()) { - result ?: computation(scope).also { result = it } - } else { - result = null - computation(scope) - } - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt deleted file mode 100644 index e3741c2..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationOwner.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.trendyol.transmission.transformer.query - -internal interface ComputationOwner { - suspend fun getResult(scope: RequestHandler, invalidate: Boolean = false): Any? -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt deleted file mode 100644 index cd85a48..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationBuilderWithArgs.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.trendyol.transmission.transformer.query.withargs - -import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.RequestHandler - -class ComputationBuilderWithArgs { - fun buildWith( - key: String, - useCache: Boolean = false, - transformer: Transformer, - computation: suspend RequestHandler.(args: A) -> T? - ) { - transformer.storage.registerComputationWithArgs( - key = key, - delegate = ComputationDelegateWithArgs(useCache = useCache, computation = computation) - ) - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt deleted file mode 100644 index d270ab4..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationDelegateWithArgs.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.trendyol.transmission.transformer.query.withargs - -import com.trendyol.transmission.transformer.query.RequestHandler - -internal class ComputationDelegateWithArgs( - private val useCache: Boolean = false, - private val computation: suspend RequestHandler.(args: A) -> Any? -) : ComputationOwnerWithArgs { - - private var result: Any? = null - - override suspend fun getResult( - scope: RequestHandler, - invalidate: Boolean, - args: A - ): Any? { - return if (useCache && invalidate.not()) { - result ?: computation(scope, args).also { result = it } - } else { - result = null - computation(scope, args) - } - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt deleted file mode 100644 index 644a522..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/withargs/ComputationOwnerWithArgs.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.trendyol.transmission.transformer.query.withargs - -import com.trendyol.transmission.Transmission -import com.trendyol.transmission.transformer.query.RequestHandler - -internal interface ComputationOwnerWithArgs { - suspend fun getResult( - scope: RequestHandler, - invalidate: Boolean = false, - args: A - ): Any? -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt similarity index 92% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt index e12f86b..543fb44 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Contract.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt @@ -1,4 +1,4 @@ -package com.trendyol.transmission.transformer.query +package com.trendyol.transmission.transformer.request import com.trendyol.transmission.Transmission diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Query.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt similarity index 92% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/Query.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt index 8161591..905067b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/Query.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Query.kt @@ -1,4 +1,4 @@ -package com.trendyol.transmission.transformer.query +package com.trendyol.transmission.transformer.request sealed interface Query { diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/QueryResult.kt similarity index 89% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/QueryResult.kt index f1b7e7b..89c5fc7 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/QueryResult.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/QueryResult.kt @@ -1,4 +1,4 @@ -package com.trendyol.transmission.transformer.query +package com.trendyol.transmission.transformer.request import com.trendyol.transmission.Transmission diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt similarity index 93% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt index 861e778..9708210 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/RequestHandler.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt @@ -1,4 +1,4 @@ -package com.trendyol.transmission.transformer.query +package com.trendyol.transmission.transformer.request import com.trendyol.transmission.Transmission diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerQueryDelegate.kt similarity index 98% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerQueryDelegate.kt index daade1a..eefa60a 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/TransformerQueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerQueryDelegate.kt @@ -1,4 +1,4 @@ -package com.trendyol.transmission.transformer.query +package com.trendyol.transmission.transformer.request import com.trendyol.transmission.Transmission import com.trendyol.transmission.router.createBroadcast diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationBuilder.kt new file mode 100644 index 0000000..deb2df7 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationBuilder.kt @@ -0,0 +1,31 @@ +package com.trendyol.transmission.transformer.request.computation + +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.request.RequestHandler + +class ComputationBuilder { + + fun buildWith( + key: String, + useCache: Boolean = false, + transformer: Transformer, + computation: suspend RequestHandler.() -> T? + ) { + transformer.storage.registerComputation( + key = key, + delegate = ComputationDelegate(useCache = useCache, computation = computation) + ) + } + + fun buildWith( + key: String, + useCache: Boolean = false, + transformer: Transformer, + computation: suspend RequestHandler.(args: A) -> T? + ) { + transformer.storage.registerComputation( + key = key, + delegate = ComputationDelegateWithArgs(useCache = useCache, computation = computation) + ) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationDelegateImpl.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationDelegateImpl.kt new file mode 100644 index 0000000..c3288d0 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationDelegateImpl.kt @@ -0,0 +1,37 @@ +package com.trendyol.transmission.transformer.request.computation + +import com.trendyol.transmission.transformer.request.RequestHandler + +internal class ComputationDelegate( + private val useCache: Boolean = false, + private val computation: (suspend RequestHandler.() -> Any?)? = null, +) : ComputationOwner.Default { + + private var result: Any? = null + + override suspend fun getResult(scope: RequestHandler, invalidate: Boolean): Any? { + return if (useCache && invalidate.not()) { + result ?: computation?.invoke(scope).also { result = it } + } else { + result = null + computation?.invoke(scope) + } + } +} + +internal class ComputationDelegateWithArgs( + private val useCache: Boolean = false, + private val computation: suspend RequestHandler.(args: A) -> Any?, +) : ComputationOwner.WithArgs { + + private var result: Any? = null + + override suspend fun getResult(scope: RequestHandler, invalidate: Boolean, args: A): Any? { + return if (useCache && invalidate.not()) { + result ?: computation(scope, args).also { result = it } + } else { + result = null + computation(scope, args) + } + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt similarity index 76% rename from transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt index e85bcf7..df49b28 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/query/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt @@ -1,8 +1,9 @@ -package com.trendyol.transmission.transformer.query +package com.trendyol.transmission.transformer.request.computation import com.trendyol.transmission.Transmission import com.trendyol.transmission.transformer.Transformer -import com.trendyol.transmission.transformer.query.withargs.ComputationBuilderWithArgs +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.RequestHandler /** * Throws [IllegalArgumentException] when multiple computations with the same key @@ -17,7 +18,7 @@ fun , T : Transmission.Data> Transformer.registerCom contract: C, computation: suspend RequestHandler.() -> T?, ) { - ComputationBuilder().buildWith(contract.key, contract.useCache, this, computation) + ComputationBuilder().buildWith(contract.key, contract.useCache, this, computation) } /** @@ -33,5 +34,5 @@ fun , A : Any, T : Any> Transformer.regis contract: C, computation: suspend RequestHandler.(args: A) -> T?, ) { - ComputationBuilderWithArgs().buildWith(contract.key, contract.useCache, this, computation) + ComputationBuilder().buildWith(contract.key, contract.useCache, this, computation) } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationOwner.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationOwner.kt new file mode 100644 index 0000000..febfe75 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationOwner.kt @@ -0,0 +1,18 @@ +package com.trendyol.transmission.transformer.request.computation + +import com.trendyol.transmission.transformer.request.RequestHandler + +internal sealed interface ComputationOwner { + interface WithArgs: ComputationOwner { + suspend fun getResult( + scope: RequestHandler, + invalidate: Boolean = false, + args: A + ): Any? + } + + interface Default: ComputationOwner { + suspend fun getResult(scope: RequestHandler, invalidate: Boolean = false): Any? + } + +} From 5ec7551d2a9f0ca22c2d6b62001c723e6e3032f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Tue, 25 Jun 2024 23:12:16 +0300 Subject: [PATCH 11/21] Add Contract extension functions --- .../colorpicker/ColorPickerTransformer.kt | 6 +-- .../features/input/InputTransformer.kt | 16 +++----- .../features/output/OutputTransformer.kt | 7 ++-- .../transformer/request/ContractExt.kt | 37 +++++++++++++++++++ 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt diff --git a/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt b/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt index 1f6541b..07474fc 100644 --- a/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt +++ b/sample/src/main/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformer.kt @@ -6,7 +6,7 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.handler.buildGenericSignalHandler -import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.buildDataContract import com.trendyol.transmission.ui.ColorPickerUiState import kotlinx.coroutines.CoroutineDispatcher import javax.inject.Inject @@ -43,8 +43,6 @@ class ColorPickerTransformer @Inject constructor( } companion object { - val holderContract = object : Contract.Data() { - override val key: String = "ColorPickerUiState" - } + val holderContract = buildDataContract("ColorPickerUiState") } } 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 71187ef..2a7722d 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 @@ -6,7 +6,9 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.handler.buildTypedSignalHandler -import com.trendyol.transmission.transformer.request.Contract +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.registerComputation import com.trendyol.transmission.ui.InputUiState import kotlinx.coroutines.CoroutineDispatcher @@ -49,14 +51,8 @@ class InputTransformer @Inject constructor( companion object { val writtenInputWithArgs = - object : Contract.ComputationWithArgs() { - override val key: String = "WrittenInputWithArgs" - } - val writtenInputContract = object : Contract.Computation() { - override val key: String = "WrittenInput" - } - val holderContract = object : Contract.Data() { - override val key: String = "InputUiState" - } + buildComputationContractWithArgs("WrittenInputWithArgs") + val writtenInputContract = buildComputationContract("WrittenInput") + val holderContract = buildDataContract("InputUiState") } } 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 15371d1..cb10994 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 @@ -9,7 +9,7 @@ import com.trendyol.transmission.features.input.InputTransformer import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler -import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.buildComputationContract import com.trendyol.transmission.transformer.request.computation.registerComputation import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.OutputUiState @@ -62,8 +62,7 @@ class OutputTransformer @Inject constructor( } companion object { - val outputCalculationContract = object : Contract.Computation() { - override val key: String = "OutputCalculation" - } + val outputCalculationContract = + buildComputationContract("OutputCalculationResult") } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt new file mode 100644 index 0000000..8a9f56b --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt @@ -0,0 +1,37 @@ +package com.trendyol.transmission.transformer.request + +import com.trendyol.transmission.Transmission + +fun buildDataContract( + key: String +) = object : Contract.Data() { + override val key: String = key +} + +fun buildComputationContract( + key: String, + useCache: Boolean = false +) = object : Contract.Computation() { + override val key: String = key + override val useCache: Boolean = useCache +} + +fun buildComputationContractWithArgs( + key: String, + useCache: Boolean = false +) = object : Contract.ComputationWithArgs() { + override val key: String = key + override val useCache: Boolean = useCache +} + +fun buildExecutionContract( + key: String, +) = object : Contract.Execution() { + override val key: String = key +} + +fun buildExecutionContractWithArgs( + key: String, +) = object : Contract.ExecutionWithArgs() { + override val key: String = key +} From 3123a8c40eb1ccefc513499bf5a27fc0856c9a6e Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Wed, 26 Jun 2024 11:43:54 +0300 Subject: [PATCH 12/21] Rename delegates, add stream extension functions, Delete SampleScreenUiState --- .../trendyol/transmission/SampleActivity.kt | 420 +++++++++--------- .../trendyol/transmission/SampleViewModel.kt | 19 +- .../transmission/ui/SampleScreenUiState.kt | 8 - .../trendyol/transmission/router/StreamExt.kt | 26 ++ .../transformer/CommunicationScopeBuilder.kt | 10 +- .../transmission/transformer/Transformer.kt | 10 +- ...egate.kt => TransformerRequestDelegate.kt} | 2 +- 7 files changed, 255 insertions(+), 240 deletions(-) create mode 100644 transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt rename transmission/src/main/java/com/trendyol/transmission/transformer/request/{TransformerQueryDelegate.kt => TransformerRequestDelegate.kt} (96%) diff --git a/sample/src/main/java/com/trendyol/transmission/SampleActivity.kt b/sample/src/main/java/com/trendyol/transmission/SampleActivity.kt index 22fa74a..78ca449 100644 --- a/sample/src/main/java/com/trendyol/transmission/SampleActivity.kt +++ b/sample/src/main/java/com/trendyol/transmission/SampleActivity.kt @@ -54,7 +54,6 @@ import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.compose.collectAsStateWithLifecycle -import com.trendyol.transmission.Transmission import com.trendyol.transmission.features.colorpicker.ColorPickerSignal import com.trendyol.transmission.features.input.InputSignal import com.trendyol.transmission.ui.theme.TransmissionTheme @@ -62,247 +61,250 @@ import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class SampleActivity : ComponentActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - val viewModel: SampleViewModel by viewModels() - enableEdgeToEdge() - setContent { - TransmissionTheme { - SampleScreen(viewModel) - } - } - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val viewModel: SampleViewModel by viewModels() + enableEdgeToEdge() + setContent { + TransmissionTheme { + SampleScreen(viewModel) + } + } + } } @Composable fun SampleScreen(viewModel: SampleViewModel) { - val uiState by viewModel.uiState.collectAsStateWithLifecycle() - val transmissionList by viewModel.transmissionList.collectAsStateWithLifecycle() - Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> - Column( - modifier = Modifier.padding(innerPadding), - verticalArrangement = Arrangement.spacedBy(8.dp), - horizontalAlignment = Alignment.CenterHorizontally - ) { - InputComponent( - backgroundColor = uiState.inputState.backgroundColor, - onSignal = viewModel::processSignal - ) - OutputComponent( - writtenText = uiState.outputState.outputText, - backgroundColor = uiState.outputState.backgroundColor - ) - ColorPickerComponent( - backgroundColor = uiState.colorPickerState.backgroundColor, - onSignal = viewModel::processSignal, - selectedColorIndex = uiState.colorPickerState.selectedColorIndex - ) - MultiOutputComponent( - writtenText = uiState.multiOutputState.writtenUppercaseText, - selectedColor = uiState.multiOutputState.selectedColor, - backgroundColor = uiState.multiOutputState.backgroundColor - ) - TransmissionComponent(transmissionList = transmissionList) - } - } + val inputUiState by viewModel.inputUiState.collectAsStateWithLifecycle() + val outputUiState by viewModel.outputUiState.collectAsStateWithLifecycle() + val colorPickerUiState by viewModel.colorPickerUiState.collectAsStateWithLifecycle() + val multiOutputUiState by viewModel.multiOutputUiState.collectAsStateWithLifecycle() + val transmissionList by viewModel.transmissionList.collectAsStateWithLifecycle() + Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> + Column( + modifier = Modifier.padding(innerPadding), + verticalArrangement = Arrangement.spacedBy(8.dp), + horizontalAlignment = Alignment.CenterHorizontally + ) { + InputComponent( + backgroundColor = inputUiState.backgroundColor, + onSignal = viewModel::processSignal + ) + OutputComponent( + writtenText = outputUiState.outputText, + backgroundColor = outputUiState.backgroundColor + ) + ColorPickerComponent( + backgroundColor = colorPickerUiState.backgroundColor, + onSignal = viewModel::processSignal, + selectedColorIndex = colorPickerUiState.selectedColorIndex + ) + MultiOutputComponent( + writtenText = multiOutputUiState.writtenUppercaseText, + selectedColor = multiOutputUiState.selectedColor, + backgroundColor = multiOutputUiState.backgroundColor + ) + TransmissionComponent(transmissionList = transmissionList) + } + } } @Composable fun TransmissionComponent( - modifier: Modifier = Modifier, - transmissionList: List + modifier: Modifier = Modifier, + transmissionList: List ) { - val scrollState = rememberLazyListState() - LaunchedEffect(transmissionList.size) { - if (transmissionList.lastIndex > 0) { - scrollState.scrollToItem(transmissionList.lastIndex) - } - } - LazyColumn(modifier = modifier.padding(8.dp), state = scrollState) { - items(transmissionList) { - Text(it, fontSize = 10.sp) - } + val scrollState = rememberLazyListState() + LaunchedEffect(transmissionList.size) { + if (transmissionList.lastIndex > 0) { + scrollState.scrollToItem(transmissionList.lastIndex) + } + } + LazyColumn(modifier = modifier.padding(8.dp), state = scrollState) { + items(transmissionList) { + Text(it, fontSize = 10.sp) + } - } + } } @Composable fun InputComponent( - modifier: Modifier = Modifier, - backgroundColor: Color, - onSignal: (Transmission.Signal) -> Unit + modifier: Modifier = Modifier, + backgroundColor: Color, + onSignal: (Transmission.Signal) -> Unit ) { - var text by remember { mutableStateOf("") } - val keyboardController = LocalSoftwareKeyboardController.current - Column( - modifier = modifier - .fillMaxWidth() - .padding(12.dp) - .background(color = backgroundColor) - .border(1.dp, color = Color.Gray) - .padding(12.dp) - ) { - Text(text = "Input Component") - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceEvenly, - verticalAlignment = Alignment.CenterVertically - ) { - TextField( - modifier = Modifier.weight(2.5f), - shape = RoundedCornerShape(4.dp), - colors = TextFieldDefaults.colors( - focusedIndicatorColor = Color.Transparent, - unfocusedIndicatorColor = Color.Transparent - ), - value = text, - onValueChange = { text = it }, - keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Text), - trailingIcon = { - if (text.isNotEmpty()) { - Icon( - imageVector = Icons.Default.Clear, - contentDescription = "Clear text", - modifier = Modifier.clickable { text = "" } - ) - } - } - ) - Button( - onClick = { - keyboardController?.hide() - onSignal.invoke(InputSignal.InputUpdate(text)) - }, - modifier = Modifier - .padding(horizontal = 4.dp) - .weight(1f) - ) { - Text("Update") - } - } - } + var text by remember { mutableStateOf("") } + val keyboardController = LocalSoftwareKeyboardController.current + Column( + modifier = modifier + .fillMaxWidth() + .padding(12.dp) + .background(color = backgroundColor) + .border(1.dp, color = Color.Gray) + .padding(12.dp) + ) { + Text(text = "Input Component") + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly, + verticalAlignment = Alignment.CenterVertically + ) { + TextField( + modifier = Modifier.weight(2.5f), + shape = RoundedCornerShape(4.dp), + colors = TextFieldDefaults.colors( + focusedIndicatorColor = Color.Transparent, + unfocusedIndicatorColor = Color.Transparent + ), + value = text, + onValueChange = { text = it }, + keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Text), + trailingIcon = { + if (text.isNotEmpty()) { + Icon( + imageVector = Icons.Default.Clear, + contentDescription = "Clear text", + modifier = Modifier.clickable { text = "" } + ) + } + } + ) + Button( + onClick = { + keyboardController?.hide() + onSignal.invoke(InputSignal.InputUpdate(text)) + }, + modifier = Modifier + .padding(horizontal = 4.dp) + .weight(1f) + ) { + Text("Update") + } + } + } } @Composable fun OutputComponent( - modifier: Modifier = Modifier, - writtenText: String = "", - backgroundColor: Color + modifier: Modifier = Modifier, + writtenText: String = "", + backgroundColor: Color ) { - Column( - modifier = modifier - .fillMaxWidth() - .padding(12.dp) - .background(color = backgroundColor) - .border(1.dp, color = Color.Gray) - .padding(12.dp) - ) { - Text("Output Component") - val annotatedString = AnnotatedString.Builder().apply { - withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { - append("Written text is: ") - } - withStyle(SpanStyle(textDecoration = TextDecoration.None, fontSize = 16.sp)) { - append(writtenText) - } - }.toAnnotatedString() - Text(annotatedString) - } + Column( + modifier = modifier + .fillMaxWidth() + .padding(12.dp) + .background(color = backgroundColor) + .border(1.dp, color = Color.Gray) + .padding(12.dp) + ) { + Text("Output Component") + val annotatedString = AnnotatedString.Builder().apply { + withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { + append("Written text is: ") + } + withStyle(SpanStyle(textDecoration = TextDecoration.None, fontSize = 16.sp)) { + append(writtenText) + } + }.toAnnotatedString() + Text(annotatedString) + } } @Composable fun ColorPickerComponent( - modifier: Modifier = Modifier, - backgroundColor: Color, - selectedColorIndex: Int, - onSignal: (Transmission.Signal) -> Unit + modifier: Modifier = Modifier, + backgroundColor: Color, + selectedColorIndex: Int, + onSignal: (Transmission.Signal) -> Unit ) { - val items = - listOf( - Color.White, - Color.Green, - Color.DarkGray, - Color.Yellow, - Color.Magenta, - Color.Black, - Color.Blue - ) - Column( - modifier = modifier - .fillMaxWidth() - .padding(12.dp) - .background(color = backgroundColor) - .border(1.dp, color = Color.Gray) - .padding(12.dp) - ) { - Text("Color Picker Component") - LazyRow( - horizontalArrangement = Arrangement.SpaceAround, - modifier = Modifier.fillMaxWidth() - ) { - itemsIndexed(items) { index, item -> - ColorItem(item, selectedColorIndex == index) { - onSignal(ColorPickerSignal.SelectColor(index, item)) - } - } - } - } + val items = + listOf( + Color.White, + Color.Green, + Color.DarkGray, + Color.Yellow, + Color.Magenta, + Color.Black, + Color.Blue + ) + Column( + modifier = modifier + .fillMaxWidth() + .padding(12.dp) + .background(color = backgroundColor) + .border(1.dp, color = Color.Gray) + .padding(12.dp) + ) { + Text("Color Picker Component") + LazyRow( + horizontalArrangement = Arrangement.SpaceAround, + modifier = Modifier.fillMaxWidth() + ) { + itemsIndexed(items) { index, item -> + ColorItem(item, selectedColorIndex == index) { + onSignal(ColorPickerSignal.SelectColor(index, item)) + } + } + } + } } @Composable fun ColorItem(color: Color, isSelected: Boolean, onClickColor: (Color) -> Unit) { - Column( - horizontalAlignment = Alignment.CenterHorizontally, - modifier = Modifier.padding(bottom = 8.dp) - ) { - Box( - modifier = Modifier - .size(40.dp) - .background(color, CircleShape) - .border( - width = 2.dp, - color = if (isSelected) Color.Black else Color.Gray, - shape = CircleShape - ) - .clickable { onClickColor(color) } - ) - Spacer(modifier = Modifier.height(8.dp)) - } + Column( + horizontalAlignment = Alignment.CenterHorizontally, + modifier = Modifier.padding(bottom = 8.dp) + ) { + Box( + modifier = Modifier + .size(40.dp) + .background(color, CircleShape) + .border( + width = 2.dp, + color = if (isSelected) Color.Black else Color.Gray, + shape = CircleShape + ) + .clickable { onClickColor(color) } + ) + Spacer(modifier = Modifier.height(8.dp)) + } } @Composable fun MultiOutputComponent( - modifier: Modifier = Modifier, - writtenText: String, - selectedColor: Color, - backgroundColor: Color + modifier: Modifier = Modifier, + writtenText: String, + selectedColor: Color, + backgroundColor: Color ) { - Column( - modifier = modifier - .fillMaxWidth() - .padding(12.dp) - .background(color = backgroundColor) - .border(1.dp, color = Color.Gray) - .padding(12.dp) - ) { - Text("Multi Output Component") - val annotatedString = AnnotatedString.Builder().apply { - withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { - append("Written Uppercase text is: ") - } - withStyle(SpanStyle(textDecoration = TextDecoration.None, fontSize = 16.sp)) { - append(writtenText) - } - }.toAnnotatedString() - Text(annotatedString) - Row( - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.Center - ) { - Text("Selected background is") - Spacer(modifier = Modifier.width(8.dp)) - ColorItem(color = selectedColor, false, {}) - } - } + Column( + modifier = modifier + .fillMaxWidth() + .padding(12.dp) + .background(color = backgroundColor) + .border(1.dp, color = Color.Gray) + .padding(12.dp) + ) { + Text("Multi Output Component") + val annotatedString = AnnotatedString.Builder().apply { + withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { + append("Written Uppercase text is: ") + } + withStyle(SpanStyle(textDecoration = TextDecoration.None, fontSize = 16.sp)) { + append(writtenText) + } + }.toAnnotatedString() + Text(annotatedString) + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.Center + ) { + Text("Selected background is") + Spacer(modifier = Modifier.width(8.dp)) + ColorItem(color = selectedColor, false, {}) + } + } } diff --git a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt index 58acec1..956954f 100644 --- a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt +++ b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt @@ -5,11 +5,11 @@ import androidx.lifecycle.viewModelScope import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer import com.trendyol.transmission.features.input.InputTransformer +import com.trendyol.transmission.router.toState import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.InputUiState import com.trendyol.transmission.ui.MultiOutputUiState import com.trendyol.transmission.ui.OutputUiState -import com.trendyol.transmission.ui.SampleScreenUiState import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow @@ -24,9 +24,12 @@ class SampleViewModel @Inject constructor( private val transmissionRouter: TransmissionRouter ) : ViewModel() { - private val _uiState = MutableStateFlow(SampleScreenUiState()) - val uiState = _uiState.asStateFlow() - + val inputUiState = transmissionRouter.dataStream.toState(viewModelScope, InputUiState()) + val outputUiState = transmissionRouter.dataStream.toState(viewModelScope, OutputUiState()) + val colorPickerUiState = + transmissionRouter.dataStream.toState(viewModelScope, ColorPickerUiState()) + val multiOutputUiState = + transmissionRouter.dataStream.toState(viewModelScope, MultiOutputUiState()) private val _transmissionList = MutableStateFlow>(emptyList()) val transmissionList = _transmissionList.asStateFlow() @@ -63,18 +66,10 @@ class SampleViewModel @Inject constructor( _transmissionList.update { it.plus("Current ColorPickerData: $colorPicker") } } - private fun onData(data: Transmission.Data) { - when (data) { - is InputUiState -> _uiState.update { it.copy(inputState = data) } - is OutputUiState -> _uiState.update { it.copy(outputState = data) } - is MultiOutputUiState -> _uiState.update { it.copy(multiOutputState = data) } - is ColorPickerUiState -> _uiState.update { it.copy(colorPickerState = data) } - } _transmissionList.update { it.plus("Data: $data") } } - override fun onCleared() { transmissionRouter.clear() } diff --git a/sample/src/main/java/com/trendyol/transmission/ui/SampleScreenUiState.kt b/sample/src/main/java/com/trendyol/transmission/ui/SampleScreenUiState.kt index ff71e53..4bdc1eb 100644 --- a/sample/src/main/java/com/trendyol/transmission/ui/SampleScreenUiState.kt +++ b/sample/src/main/java/com/trendyol/transmission/ui/SampleScreenUiState.kt @@ -3,14 +3,6 @@ package com.trendyol.transmission.ui import androidx.compose.ui.graphics.Color import com.trendyol.transmission.Transmission - -data class SampleScreenUiState( - val inputState: InputUiState = InputUiState(), - val outputState: OutputUiState = OutputUiState(), - val colorPickerState: ColorPickerUiState = ColorPickerUiState(), - val multiOutputState: MultiOutputUiState = MultiOutputUiState() -) - data class InputUiState( val writtenText: String = "", val backgroundColor: Color = Color.White diff --git a/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt b/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt new file mode 100644 index 0000000..ea2e7c7 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt @@ -0,0 +1,26 @@ +package com.trendyol.transmission.router + +import com.trendyol.transmission.Transmission +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.flow.stateIn + +inline fun SharedFlow.toState( + scope: CoroutineScope, + initialValue: T, + sharingStarted: SharingStarted = SharingStarted.WhileSubscribed(), +): StateFlow { + return this.filterIsInstance().stateIn(scope, sharingStarted, initialValue) +} + +inline fun Flow.toState( + scope: CoroutineScope, + initialValue: T, + sharingStarted: SharingStarted = SharingStarted.WhileSubscribed(), +): StateFlow { + return this.filterIsInstance().stateIn(scope, sharingStarted, initialValue) +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt index 91668a4..81f65d1 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt @@ -4,14 +4,14 @@ import com.trendyol.transmission.Transmission import com.trendyol.transmission.effect.EffectWrapper import com.trendyol.transmission.transformer.handler.CommunicationScope import com.trendyol.transmission.transformer.request.Contract -import com.trendyol.transmission.transformer.request.TransformerQueryDelegate +import com.trendyol.transmission.transformer.request.TransformerRequestDelegate import kotlinx.coroutines.channels.Channel import kotlin.reflect.KClass internal class CommunicationScopeBuilder( private val effectChannel: Channel, private val dataChannel: Channel, - private val queryDelegate: TransformerQueryDelegate + private val requestDelegate: TransformerRequestDelegate ) : CommunicationScope { override fun send(data: D?) { @@ -27,14 +27,14 @@ internal class CommunicationScopeBuilder( } override suspend fun , D : Transmission.Data> getData(contract: C): D? { - return queryDelegate.interactor.getData(contract) + return requestDelegate.interactor.getData(contract) } override suspend fun , D : Any> compute( contract: C, invalidate: Boolean ): D? { - return queryDelegate.interactor.compute(contract, invalidate) + return requestDelegate.interactor.compute(contract, invalidate) } override suspend fun , A : Any, D : Any> compute( @@ -42,7 +42,7 @@ internal class CommunicationScopeBuilder( args: A, invalidate: Boolean ): D? { - return queryDelegate.interactor.compute(contract, args, invalidate) + return requestDelegate.interactor.compute(contract, args, invalidate) } override suspend fun execute(contract: C, invalidate: Boolean) { 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 94ca9b3..7030358 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -9,7 +9,7 @@ import com.trendyol.transmission.transformer.handler.EffectHandler import com.trendyol.transmission.transformer.handler.SignalHandler import com.trendyol.transmission.transformer.request.Query import com.trendyol.transmission.transformer.request.QueryResult -import com.trendyol.transmission.transformer.request.TransformerQueryDelegate +import com.trendyol.transmission.transformer.request.TransformerRequestDelegate import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -31,7 +31,7 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) { private val identifier: String = this.identifier() private val effectChannel: Channel = Channel(capacity = Channel.BUFFERED) - private val queryDelegate = TransformerQueryDelegate(transformerScope, identifier) + private val requestDelegate = TransformerRequestDelegate(transformerScope, identifier) internal val dataChannel: Channel = Channel(capacity = Channel.BUFFERED) internal val storage = TransformerStorage() @@ -49,7 +49,7 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) { val communicationScope: CommunicationScope = CommunicationScopeBuilder( effectChannel = effectChannel, dataChannel = dataChannel, - queryDelegate = queryDelegate + requestDelegate = requestDelegate ) fun startSignalCollection(incoming: SharedFlow) { @@ -101,11 +101,11 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) { incomingQuery .filter { it.owner == identifier } .collect { - this@Transformer.queryDelegate.resultBroadcast.producer.trySend(it) + this@Transformer.requestDelegate.resultBroadcast.producer.trySend(it) } } launch { - this@Transformer.queryDelegate.outGoingQuery.receiveAsFlow().collect { + this@Transformer.requestDelegate.outGoingQuery.receiveAsFlow().collect { outGoingQuery.trySend(it) } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerQueryDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt similarity index 96% rename from transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerQueryDelegate.kt rename to transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt index eefa60a..6d56841 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerQueryDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.first -internal class TransformerQueryDelegate(scope: CoroutineScope, identifier: String) { +internal class TransformerRequestDelegate(scope: CoroutineScope, identifier: String) { val outGoingQuery: Channel = Channel(capacity = Channel.BUFFERED) val resultBroadcast = scope.createBroadcast() From d8e80d0fdd6ce7e26fcef43c0ff9090c0505eaed Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Wed, 26 Jun 2024 14:21:50 +0300 Subject: [PATCH 13/21] Add onEach extension function --- .../main/java/com/trendyol/transmission/SampleViewModel.kt | 5 ++++- .../java/com/trendyol/transmission/router/StreamExt.kt | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt index 956954f..04791a7 100644 --- a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt +++ b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer import com.trendyol.transmission.features.input.InputTransformer +import com.trendyol.transmission.router.onEach import com.trendyol.transmission.router.toState import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.InputUiState @@ -24,7 +25,9 @@ class SampleViewModel @Inject constructor( private val transmissionRouter: TransmissionRouter ) : ViewModel() { - val inputUiState = transmissionRouter.dataStream.toState(viewModelScope, InputUiState()) + val inputUiState = transmissionRouter.dataStream + .onEach { _transmissionList.value = listOf() } + .toState(viewModelScope, InputUiState()) val outputUiState = transmissionRouter.dataStream.toState(viewModelScope, OutputUiState()) val colorPickerUiState = transmissionRouter.dataStream.toState(viewModelScope, ColorPickerUiState()) diff --git a/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt b/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt index ea2e7c7..b4eb101 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.stateIn inline fun SharedFlow.toState( @@ -24,3 +25,9 @@ inline fun Flow.toState( ): StateFlow { return this.filterIsInstance().stateIn(scope, sharingStarted, initialValue) } + +inline fun Flow.onEach( + noinline action: suspend (T) -> Unit +): Flow { + return this.filterIsInstance().onEach(action) +} From 1f0a80b948bb3df1d941fc03865d5c8cb3a97fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Jun 2024 19:07:40 +0300 Subject: [PATCH 14/21] Fix nullable types for extension functions --- .../main/java/com/trendyol/transmission/router/StreamExt.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt b/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt index b4eb101..d831e35 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/StreamExt.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.stateIn -inline fun SharedFlow.toState( +inline fun SharedFlow.toState( scope: CoroutineScope, initialValue: T, sharingStarted: SharingStarted = SharingStarted.WhileSubscribed(), @@ -18,7 +18,7 @@ inline fun SharedFlow.toState return this.filterIsInstance().stateIn(scope, sharingStarted, initialValue) } -inline fun Flow.toState( +inline fun Flow.toState( scope: CoroutineScope, initialValue: T, sharingStarted: SharingStarted = SharingStarted.WhileSubscribed(), @@ -26,7 +26,7 @@ inline fun Flow.toState( return this.filterIsInstance().stateIn(scope, sharingStarted, initialValue) } -inline fun Flow.onEach( +inline fun Flow.onEach( noinline action: suspend (T) -> Unit ): Flow { return this.filterIsInstance().onEach(action) From 6a3662e713a445691d70aeec4fb6cfeaa669adcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Jun 2024 19:11:18 +0300 Subject: [PATCH 15/21] Fix computation extension generic input --- .../transformer/request/computation/ComputationExt.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 df49b28..521df9e 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 @@ -14,7 +14,7 @@ import com.trendyol.transmission.transformer.request.RequestHandler * @param useCache Stores the result after first computation * @param computation Computation to get the result [Transmission.Data] */ -fun , T : Transmission.Data> Transformer.registerComputation( +fun , T : Any> Transformer.registerComputation( contract: C, computation: suspend RequestHandler.() -> T?, ) { From dff9b72d46f97f976371cc7b9b1bc2f4bb988cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Jun 2024 19:19:42 +0300 Subject: [PATCH 16/21] Fix test code --- .../com/yigitozgumus/transmissiontesting/TestRouter.kt | 4 +++- .../com/yigitozgumus/transmissiontesting/TestWithExt.kt | 4 ++++ .../com/trendyol/transmission/transformer/Transformer.kt | 9 ++------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt index 80b0ca2..b695d21 100644 --- a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt +++ b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt @@ -37,7 +37,7 @@ internal class TestRouter( private val outGoingQueryChannel: Channel = Channel(capacity = Channel.BUFFERED) - private val queryResultChannel: Channel> = + private val queryResultChannel: Channel = Channel(capacity = Channel.BUFFERED) private val incomingQueryResponse = queryResultChannel.receiveAsFlow() @@ -83,6 +83,8 @@ internal class TestRouter( is Query.Computation -> processComputationQuery(query) is Query.Data -> processDataQuery(query) is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) + is Query.Execution -> TODO() + is Query.ExecutionWithArgs<*> -> TODO() } } diff --git a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestWithExt.kt b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestWithExt.kt index d8be8f8..a431f41 100644 --- a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestWithExt.kt +++ b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestWithExt.kt @@ -75,3 +75,7 @@ fun Transformer.testWithSignal( } } +suspend fun Transformer.waitProcessingToFinish() { + currentSignalProcessing?.join() + currentEffectProcessing?.join() +} 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 7030358..13c07b2 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -38,13 +38,8 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) { open val signalHandler: SignalHandler? = null open val effectHandler: EffectHandler? = null - private var currentEffectProcessing: Job? = null - private var currentSignalProcessing: Job? = null - - suspend fun waitProcessingToFinish() { - currentSignalProcessing?.join() - currentEffectProcessing?.join() - } + var currentEffectProcessing: Job? = null + var currentSignalProcessing: Job? = null val communicationScope: CommunicationScope = CommunicationScopeBuilder( effectChannel = effectChannel, From a3902c842718ddcca49d13d2107d0b4cbb0f0dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=96zg=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Jun 2024 19:33:33 +0300 Subject: [PATCH 17/21] Update sample code --- .../trendyol/transmission/SampleViewModel.kt | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt index 04791a7..c45eb0e 100644 --- a/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt +++ b/sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt @@ -22,33 +22,33 @@ import kotlin.time.Duration.Companion.seconds @HiltViewModel class SampleViewModel @Inject constructor( - private val transmissionRouter: TransmissionRouter + private val router: TransmissionRouter ) : ViewModel() { - val inputUiState = transmissionRouter.dataStream + val inputUiState = router.dataStream .onEach { _transmissionList.value = listOf() } .toState(viewModelScope, InputUiState()) - val outputUiState = transmissionRouter.dataStream.toState(viewModelScope, OutputUiState()) - val colorPickerUiState = - transmissionRouter.dataStream.toState(viewModelScope, ColorPickerUiState()) - val multiOutputUiState = - transmissionRouter.dataStream.toState(viewModelScope, MultiOutputUiState()) + + val outputUiState = router.dataStream.toState(viewModelScope, OutputUiState()) + val colorPickerUiState = router.dataStream.toState(viewModelScope, ColorPickerUiState()) + val multiOutputUiState = router.dataStream.toState(viewModelScope, MultiOutputUiState()) + private val _transmissionList = MutableStateFlow>(emptyList()) val transmissionList = _transmissionList.asStateFlow() init { viewModelScope.launch { launch { - transmissionRouter.dataStream.collect(::onData) + router.dataStream.collect(::onData) } launch { - transmissionRouter.effectStream.collect(::onEffect) + router.effectStream.collect(::onEffect) } } } fun processSignal(signal: Transmission.Signal) { - transmissionRouter.processSignal(signal) + router.processSignal(signal) _transmissionList.update { it.plus("Signal: $signal") } } @@ -61,10 +61,10 @@ class SampleViewModel @Inject constructor( } } } - val inputData = transmissionRouter.requestHelper.getData(InputTransformer.holderContract) + val inputData = router.requestHelper.getData(InputTransformer.holderContract) delay(1.seconds) val colorPicker = - transmissionRouter.requestHelper.getData(ColorPickerTransformer.holderContract) + router.requestHelper.getData(ColorPickerTransformer.holderContract) _transmissionList.update { it.plus("Current InputData: $inputData") } _transmissionList.update { it.plus("Current ColorPickerData: $colorPicker") } } @@ -74,7 +74,7 @@ class SampleViewModel @Inject constructor( } override fun onCleared() { - transmissionRouter.clear() + router.clear() } } From 122db34f92217e4d98fd129226d34b80c0444def Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Fri, 28 Jun 2024 09:28:52 +0300 Subject: [PATCH 18/21] Simplify execute functions --- .../transformer/request/RequestHandler.kt | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt index 9708210..51a28b8 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/RequestHandler.kt @@ -17,14 +17,7 @@ interface RequestHandler { invalidate: Boolean = false, ): D? - suspend fun execute( - contract: C, - invalidate: Boolean = false, - ) + suspend fun execute(contract: C) - suspend fun , A : Any> execute( - contract: C, - args: A, - invalidate: Boolean = false, - ) + suspend fun , A : Any> execute(contract: C, args: A) } From fc118baa646ad9413c750ee7c702a66c635d4d87 Mon Sep 17 00:00:00 2001 From: ertugrul Date: Fri, 28 Jun 2024 10:56:22 +0300 Subject: [PATCH 19/21] Remove example unit test --- .../trendyol/transmission/ExampleUnitTest.kt | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 sample/src/test/java/com/trendyol/transmission/ExampleUnitTest.kt diff --git a/sample/src/test/java/com/trendyol/transmission/ExampleUnitTest.kt b/sample/src/test/java/com/trendyol/transmission/ExampleUnitTest.kt deleted file mode 100644 index 1f2983f..0000000 --- a/sample/src/test/java/com/trendyol/transmission/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.trendyol.transmission - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} From f8df1c846b7fe3902c98afb0caf33cc78a5a3c7a Mon Sep 17 00:00:00 2001 From: ertugrul Date: Fri, 28 Jun 2024 10:59:57 +0300 Subject: [PATCH 20/21] Typo fix --- .../java/com/yigitozgumus/transmissiontesting/RegistryScope.kt | 1 - .../com/trendyol/transmission/transformer/TransformerStorage.kt | 2 -- 2 files changed, 3 deletions(-) diff --git a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/RegistryScope.kt b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/RegistryScope.kt index 1c89380..68a6c39 100644 --- a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/RegistryScope.kt +++ b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/RegistryScope.kt @@ -26,5 +26,4 @@ class RegistryScopeImpl : RegistryScope { override fun addComputation(data: D, key: String) { computationMap[key] = data } - } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt index e066b4b..3fd8f14 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt @@ -76,6 +76,4 @@ internal class TransformerStorage { fun clear() { internalComputationMap.clear() } - - } From ea7e295fd26c2fe0344af2c47384290ee9cfe0df Mon Sep 17 00:00:00 2001 From: ertugrul Date: Fri, 28 Jun 2024 11:03:25 +0300 Subject: [PATCH 21/21] Implement execution delegation --- .../features/output/OutputTransformer.kt | 10 +++++ .../colorpicker/ColorPickerTransformerTest.kt | 1 - .../transmissiontesting/TestRouter.kt | 6 +-- .../transmission/router/RequestDelegate.kt | 42 +++++++++++++++---- .../transformer/CommunicationScopeBuilder.kt | 9 ++-- .../transformer/TransformerStorage.kt | 26 +++++++++++- .../request/TransformerRequestDelegate.kt | 20 ++++++--- .../request/execution/ExecutionBuilder.kt | 29 +++++++++++++ .../execution/ExecutionDelegateImpl.kt | 21 ++++++++++ .../request/execution/ExecutionExt.kt | 37 ++++++++++++++++ .../request/execution/ExecutionOwner.kt | 14 +++++++ 11 files changed, 191 insertions(+), 24 deletions(-) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionBuilder.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionDelegateImpl.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionExt.kt create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionOwner.kt 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 cb10994..d0440d0 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 @@ -10,9 +10,12 @@ import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.dataholder.buildDataHolder import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler import com.trendyol.transmission.transformer.request.buildComputationContract +import com.trendyol.transmission.transformer.request.buildExecutionContract import com.trendyol.transmission.transformer.request.computation.registerComputation +import com.trendyol.transmission.transformer.request.execution.registerExecution import com.trendyol.transmission.ui.ColorPickerUiState import com.trendyol.transmission.ui.OutputUiState +import com.trendyol.transmission.ui.theme.Pink80 import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.delay import javax.inject.Inject @@ -35,6 +38,10 @@ class OutputTransformer @Inject constructor( val result = Random.nextInt(5, 15) * Random.nextInt(5, 15) OutputCalculationResult("result is $result with ($writtenOutput) and $data") } + registerExecution(outputExecutionContract) { + delay(4.seconds) + communicationScope.publish(ColorPickerEffect.BackgroundColorUpdate(Pink80)) + } } override val effectHandler = buildGenericEffectHandler { effect -> @@ -52,6 +59,7 @@ class OutputTransformer @Inject constructor( effect = ColorPickerEffect.BackgroundColorUpdate(holder2.getValue().backgroundColor), to = ColorPickerTransformer::class ) + execute(outputExecutionContract) publish(effect = RouterEffect(holder.getValue())) } @@ -64,5 +72,7 @@ class OutputTransformer @Inject constructor( companion object { val outputCalculationContract = buildComputationContract("OutputCalculationResult") + val outputExecutionContract = + buildExecutionContract("outputExecutionContract") } } 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 6f0aa9d..d25568f 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 @@ -67,5 +67,4 @@ class ColorPickerTransformerTest { assertTrue { effectStream.last().effect is ColorPickerEffect.SelectedColorUpdate } } } - } diff --git a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt index b695d21..6451ff6 100644 --- a/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt +++ b/transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestRouter.kt @@ -19,7 +19,7 @@ import kotlinx.coroutines.launch internal class TestRouter( private val registry: RegistryScopeImpl, private val transformer: Transformer, - private val dispatcher: CoroutineDispatcher + dispatcher: CoroutineDispatcher ) { private val testScope = CoroutineScope(dispatcher) @@ -83,8 +83,8 @@ internal class TestRouter( is Query.Computation -> processComputationQuery(query) is Query.Data -> processDataQuery(query) is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) - is Query.Execution -> TODO() - is Query.ExecutionWithArgs<*> -> TODO() + is Query.Execution -> {} + is Query.ExecutionWithArgs<*> -> {} } } 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 170e741..6089589 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -6,7 +6,6 @@ import com.trendyol.transmission.transformer.request.Contract import com.trendyol.transmission.transformer.request.Query import com.trendyol.transmission.transformer.request.QueryResult import com.trendyol.transmission.transformer.request.RequestHandler -import com.trendyol.transmission.transformer.request.computation.ComputationOwner import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel @@ -45,8 +44,8 @@ internal class RequestDelegate( is Query.Computation -> processComputationQuery(query) is Query.Data -> processDataQuery(query) is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query) - is Query.Execution -> TODO() - is Query.ExecutionWithArgs<*> -> TODO() + is Query.Execution -> processExecution(query) + is Query.ExecutionWithArgs<*> -> processExecutionWithArgs(query) } } @@ -116,6 +115,23 @@ internal class RequestDelegate( } } + private fun processExecution( + query: Query.Execution + ) = queryScope.launch { + val executionHolder = routerRef.transformerSet + .find { it.storage.hasExecution(query.key) } + executionHolder?.storage?.getExecutionByKey(query.key) + ?.execute(executionHolder.communicationScope) + } + + private fun processExecutionWithArgs(query: Query.ExecutionWithArgs) = + queryScope.launch { + val executionHolder = routerRef.transformerSet + .find { it.storage.hasExecution(query.key) } + executionHolder?.storage?.getExecutionByKey(query.key) + ?.execute(executionHolder.communicationScope, query.args) + } + override suspend fun , D : Transmission.Data> getData(contract: C): D? { outGoingQuery.trySend( Query.Data(sender = routerRef.routerName, key = contract.key) @@ -162,15 +178,25 @@ internal class RequestDelegate( .first().data } - override suspend fun execute(contract: C, invalidate: Boolean) { - TODO("Not yet implemented") + override suspend fun execute(contract: C) { + outGoingQuery.trySend( + Query.Execution( + sender = routerRef.routerName, + key = contract.key, + ) + ) } override suspend fun , A : Any> execute( contract: C, - args: A, - invalidate: Boolean + args: A ) { - TODO("Not yet implemented") + outGoingQuery.trySend( + Query.ExecutionWithArgs( + sender = routerRef.routerName, + key = contract.key, + args = args, + ) + ) } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt index 81f65d1..48e0d3b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/CommunicationScopeBuilder.kt @@ -45,15 +45,14 @@ internal class CommunicationScopeBuilder( return requestDelegate.interactor.compute(contract, args, invalidate) } - override suspend fun execute(contract: C, invalidate: Boolean) { - TODO("Not yet implemented") + override suspend fun execute(contract: C) { + requestDelegate.interactor.execute(contract) } override suspend fun , A : Any> execute( contract: C, - args: A, - invalidate: Boolean + args: A ) { - TODO("Not yet implemented") + requestDelegate.interactor.execute(contract, args) } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt index 3fd8f14..9e01d97 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/TransformerStorage.kt @@ -4,6 +4,7 @@ import com.trendyol.transmission.Transmission import com.trendyol.transmission.identifier import com.trendyol.transmission.transformer.dataholder.HolderState import com.trendyol.transmission.transformer.request.computation.ComputationOwner +import com.trendyol.transmission.transformer.request.execution.ExecutionOwner import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.update @@ -17,6 +18,9 @@ internal class TransformerStorage { private val internalComputationMap: MutableMap = mutableMapOf() + private val internalExecutionMap: MutableMap = + mutableMapOf() + fun isHolderStateInitialized(): Boolean { return internalTransmissionHolderSet is HolderState.Initialized } @@ -34,7 +38,6 @@ internal class TransformerStorage { } fun updateHolderDataReferenceToTrack(dataHolderToTrack: String) { - internalTransmissionHolderSet = when (internalTransmissionHolderSet) { is HolderState.Initialized -> { val currentSet = (internalTransmissionHolderSet as HolderState.Initialized).valueSet @@ -57,18 +60,37 @@ internal class TransformerStorage { internalComputationMap[key] = delegate } + fun registerExecution(key: String, delegate: ExecutionOwner) { + require(!internalExecutionMap.containsKey(key)) { + "Multiple executions with the same key is not allowed: $key" + } + internalExecutionMap[key] = delegate + } + fun hasComputation(type: String): Boolean { return internalComputationMap.containsKey(type) } + fun hasExecution(type: String): Boolean { + return internalExecutionMap.containsKey(type) + } + fun getComputationByKey(type: String): ComputationOwner.Default? { return internalComputationMap[type] as? ComputationOwner.Default } - fun getComputationByKey(type: String): ComputationOwner.WithArgs? { + fun getExecutionByKey(type: String): ExecutionOwner.Default? { + return internalExecutionMap[type] as? ExecutionOwner.Default + } + + fun getComputationByKey(type: String): ComputationOwner.WithArgs? { return internalComputationMap[type] as? ComputationOwner.WithArgs } + fun getExecutionByKey(type: String): ExecutionOwner.WithArgs? { + return internalExecutionMap[type] as? ExecutionOwner.WithArgs + } + fun getHolderDataByKey(key: String): Transmission.Data? { return holderDataReference.value[key] } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt index 6d56841..55329b0 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/TransformerRequestDelegate.kt @@ -59,16 +59,26 @@ internal class TransformerRequestDelegate(scope: CoroutineScope, identifier: Str .first().data } - override suspend fun execute(contract: C, invalidate: Boolean) { - TODO("Not yet implemented") + override suspend fun execute(contract: C) { + outGoingQuery.trySend( + Query.Execution( + sender = identifier, + key = contract.key, + ) + ) } override suspend fun , A : Any> execute( contract: C, - args: A, - invalidate: Boolean + args: A ) { - TODO("Not yet implemented") + outGoingQuery.trySend( + Query.ExecutionWithArgs( + sender = identifier, + key = contract.key, + args = args, + ) + ) } } } diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionBuilder.kt new file mode 100644 index 0000000..01c581e --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionBuilder.kt @@ -0,0 +1,29 @@ +package com.trendyol.transmission.transformer.request.execution + +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.request.RequestHandler + +class ExecutionBuilder { + + fun buildWith( + key: String, + transformer: Transformer, + execution: suspend RequestHandler.() -> Unit + ) { + transformer.storage.registerExecution( + key = key, + delegate = ExecutionDelegate(execution) + ) + } + + fun buildWith( + key: String, + transformer: Transformer, + execution: suspend RequestHandler.(args: A) -> Unit + ) { + transformer.storage.registerExecution( + key = key, + delegate = ExecutionDelegateWithArgs(execution) + ) + } +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionDelegateImpl.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionDelegateImpl.kt new file mode 100644 index 0000000..5399219 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionDelegateImpl.kt @@ -0,0 +1,21 @@ +package com.trendyol.transmission.transformer.request.execution + +import com.trendyol.transmission.transformer.request.RequestHandler + +internal class ExecutionDelegate( + private val execution: suspend RequestHandler.() -> Unit, +) : ExecutionOwner.Default { + + override suspend fun execute(scope: RequestHandler) { + execution.invoke(scope) + } +} + +internal class ExecutionDelegateWithArgs( + private val execution: suspend RequestHandler.(args: A) -> Unit, +) : ExecutionOwner.WithArgs { + + override suspend fun execute(scope: RequestHandler, args: A) { + execution(scope, args) + } +} 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 new file mode 100644 index 0000000..03b215a --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionExt.kt @@ -0,0 +1,37 @@ +package com.trendyol.transmission.transformer.request.execution + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.RequestHandler + +/** + * Throws [IllegalArgumentException] when multiple executions with the same key + * are defined inside the [Transformer]. + * + * Adds a execution to [Transformer] to be queried that returns Unit. + * Can be queried using [RequestHandler.execute] + * @param execution execution to get the result [Transmission.Data] + */ +fun Transformer.registerExecution( + contract: C, + execution: suspend RequestHandler.() -> Unit, +) { + ExecutionBuilder().buildWith(contract.key, this, execution) +} + +/** + * Throws [IllegalArgumentException] when multiple executions with the same key + * are defined inside the [Transformer]. + * + * Adds a execution to [Transformer] to be queried. This execution accepts any class as Argument + * that returns Unit. + * Can be queried using [RequestHandler.execute] + * @param execution execution to get the result [Transmission.Data] + */ +fun , A : Any> Transformer.registerExecution( + contract: C, + execution: suspend RequestHandler.(args: A) -> Unit, +) { + ExecutionBuilder().buildWith(contract.key, this, execution) +} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionOwner.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionOwner.kt new file mode 100644 index 0000000..179f916 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionOwner.kt @@ -0,0 +1,14 @@ +package com.trendyol.transmission.transformer.request.execution + +import com.trendyol.transmission.transformer.request.RequestHandler + +internal sealed interface ExecutionOwner { + + interface WithArgs : ExecutionOwner { + suspend fun execute(scope: RequestHandler, args: A) + } + + interface Default : ExecutionOwner { + suspend fun execute(scope: RequestHandler) + } +}