From b09f9308c4b27974cb43155b315882d4e7d34420 Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 13:33:30 +0300 Subject: [PATCH 01/10] Typo fix --- .../transformer/dataholder/TransmissionDataHolder.kt | 1 - .../transformer/request/computation/ComputationOwner.kt | 1 - 2 files changed, 2 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolder.kt index c631ccf..36539c3 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolder.kt @@ -47,5 +47,4 @@ internal class TransmissionDataHolderImpl( override fun update(updater: (T) -> @UnsafeVariance T) { holder.update(updater) } - } 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 index febfe75..ccb8d99 100644 --- 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 @@ -14,5 +14,4 @@ internal sealed interface ComputationOwner { interface Default: ComputationOwner { suspend fun getResult(scope: RequestHandler, invalidate: Boolean = false): Any? } - } From 0b5b4481d2e45b39c7eef02f992afa00e1e12523 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 29 Jul 2024 13:38:41 +0300 Subject: [PATCH 02/10] Remove signal and effect handler --- .../transformer/handler/EffectHandler.kt | 25 ------------------- .../transformer/handler/SignalHandler.kt | 21 ---------------- 2 files changed, 46 deletions(-) delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/handler/EffectHandler.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/handler/SignalHandler.kt diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/EffectHandler.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/EffectHandler.kt deleted file mode 100644 index 80bc133..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/EffectHandler.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.trendyol.transmission.transformer.handler - -import com.trendyol.transmission.Transmission - -fun interface EffectHandler { - suspend fun CommunicationScope.onEffect(effect: Transmission.Effect) -} - -fun buildGenericEffectHandler( - onEffect: suspend CommunicationScope.(effect: Transmission.Effect) -> Unit -): EffectHandler { - return EffectHandler { effect -> - onEffect(effect) - } -} - -inline fun buildTypedEffectHandler( - crossinline onEffect: suspend CommunicationScope.(effect: E) -> Unit -): EffectHandler { - return EffectHandler { incomingEffect -> - incomingEffect.takeIf { it is E }?.let { effect -> - onEffect(effect as E) - } - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/SignalHandler.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/SignalHandler.kt deleted file mode 100644 index 93d957b..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/SignalHandler.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.trendyol.transmission.transformer.handler - -import com.trendyol.transmission.Transmission - -fun interface SignalHandler { - suspend fun CommunicationScope.onSignal(signal: Transmission.Signal) -} - -fun buildGenericSignalHandler( - onSignal: suspend CommunicationScope.(signal: Transmission.Signal) -> Unit -): SignalHandler { - return SignalHandler { signal -> onSignal(signal) } -} - -inline fun buildTypedSignalHandler( - crossinline onSignal: suspend CommunicationScope.(signal: S) -> Unit -): SignalHandler { - return SignalHandler { incomingSignal -> - incomingSignal.takeIf { it is S }?.let { signal -> onSignal(signal as S) } - } -} From 4094bbd7e287621cbfc65d8c7c4aa47284641cd6 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 29 Jul 2024 13:38:43 +0300 Subject: [PATCH 03/10] Introduce Handler Registry --- .../transformer/handler/HandlerRegistry.kt | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt new file mode 100644 index 0000000..fed2ee9 --- /dev/null +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/handler/HandlerRegistry.kt @@ -0,0 +1,47 @@ +@file:Suppress("UNCHECKED_CAST") + +package com.trendyol.transmission.transformer.handler + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.Transformer +import kotlin.reflect.KClass + +class HandlerScope(val handlerRegistry: HandlerRegistry) + +fun Transformer.handlerRegistry(scope: HandlerScope.() -> Unit): HandlerRegistry { + val handlerRegistry = HandlerRegistry() + HandlerScope(handlerRegistry).apply(scope) + return handlerRegistry +} + +inline fun HandlerScope.registerEffect(noinline lambda: suspend CommunicationScope.(effect: T) -> Unit) { + handlerRegistry.registerEffect(lambda) +} + +inline fun HandlerScope.registerSignal(noinline lambda: suspend CommunicationScope.(signal: T) -> Unit) { + handlerRegistry.registerSignal(lambda) +} + +class HandlerRegistry internal constructor() { + + @PublishedApi + internal val signalHandlerRegistry = + mutableMapOf, suspend CommunicationScope.(effect: Transmission.Signal) -> Unit>() + + @PublishedApi + internal val effectHandlerRegistry = + mutableMapOf, suspend CommunicationScope.(effect: Transmission.Effect) -> Unit>() + + @PublishedApi + internal inline fun registerSignal(noinline lambda: suspend CommunicationScope.(signal: T) -> Unit) { + signalHandlerRegistry[T::class] = + lambda as suspend CommunicationScope.(Transmission.Signal) -> Unit + } + + @PublishedApi + internal inline fun registerEffect(noinline lambda: suspend CommunicationScope.(effect: T) -> Unit) { + effectHandlerRegistry[T::class] = + lambda as suspend CommunicationScope.(Transmission.Effect) -> Unit + } + +} From 9375114aa8bfd259be6cd7ea97f2296527838be9 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Mon, 29 Jul 2024 13:39:08 +0300 Subject: [PATCH 04/10] Update sample usage and router tests --- .../colorpicker/ColorPickerTransformer.kt | 39 ++++++++--------- .../features/input/InputTransformer.kt | 25 +++++------ .../multioutput/MultiOutputTransformer.kt | 33 +++++++------- .../features/output/OutputTransformer.kt | 43 +++++++++---------- .../transmission/transformer/Transformer.kt | 17 +++----- .../transformer/FakeTransformer.kt | 34 ++++++++------- 6 files changed, 90 insertions(+), 101 deletions(-) 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 89e2573..0209361 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 @@ -4,8 +4,10 @@ import com.trendyol.transmission.DefaultDispatcher import com.trendyol.transmission.features.multioutput.multiOutputTransformerIdentity 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.handler.HandlerRegistry +import com.trendyol.transmission.transformer.handler.handlerRegistry +import com.trendyol.transmission.transformer.handler.registerEffect +import com.trendyol.transmission.transformer.handler.registerSignal import com.trendyol.transmission.transformer.request.buildDataContract import com.trendyol.transmission.transformer.request.createIdentity import com.trendyol.transmission.ui.ColorPickerUiState @@ -20,27 +22,20 @@ class ColorPickerTransformer @Inject constructor( private val holder = buildDataHolder(ColorPickerUiState(), holderContract) - override val signalHandler = buildGenericSignalHandler { signal -> - when (signal) { - is ColorPickerSignal.SelectColor -> { - holder.update { it.copy(selectedColorIndex = signal.index) } - publish( - ColorPickerEffect.BackgroundColorUpdate(signal.selectedColor.copy(alpha = 0.1f)) - ) - send( - effect = ColorPickerEffect.SelectedColorUpdate(signal.selectedColor), - identity = multiOutputTransformerIdentity - ) - } + override val handlerRegistry: HandlerRegistry = handlerRegistry { + registerSignal { signal -> + holder.update { it.copy(selectedColorIndex = signal.index) } + publish( + ColorPickerEffect.BackgroundColorUpdate(signal.selectedColor.copy(alpha = 0.1f)) + ) + send( + effect = ColorPickerEffect.SelectedColorUpdate(signal.selectedColor), + identity = multiOutputTransformerIdentity + ) } - } - - override val effectHandler = buildGenericEffectHandler { effect -> - when (effect) { - is ColorPickerEffect.BackgroundColorUpdate -> { - holder.update { - it.copy(backgroundColor = effect.color) - } + registerEffect { effect -> + holder.update { + it.copy(backgroundColor = effect.color) } } } 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 4841422..074b208 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 @@ -4,8 +4,10 @@ import com.trendyol.transmission.DefaultDispatcher import com.trendyol.transmission.features.colorpicker.ColorPickerEffect 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.handler.HandlerRegistry +import com.trendyol.transmission.transformer.handler.handlerRegistry +import com.trendyol.transmission.transformer.handler.registerEffect +import com.trendyol.transmission.transformer.handler.registerSignal import com.trendyol.transmission.transformer.request.buildComputationContract import com.trendyol.transmission.transformer.request.buildComputationContractWithArgs import com.trendyol.transmission.transformer.request.buildDataContract @@ -32,20 +34,13 @@ class InputTransformer @Inject constructor( } } - override val signalHandler = buildTypedSignalHandler { signal -> - when (signal) { - is InputSignal.InputUpdate -> { - holder.update { it.copy(writtenText = signal.value) } - publish(effect = InputEffect.InputUpdate(signal.value)) - } + override val handlerRegistry: HandlerRegistry = handlerRegistry { + registerSignal { signal -> + holder.update { it.copy(writtenText = signal.value) } + publish(effect = InputEffect.InputUpdate(signal.value)) } - } - - override val effectHandler = buildGenericEffectHandler { effect -> - when (effect) { - is ColorPickerEffect.BackgroundColorUpdate -> { - holder.update { it.copy(backgroundColor = effect.color) } - } + registerEffect { effect -> + holder.update { it.copy(backgroundColor = effect.color) } } } 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 a3a0ee7..729f233 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 @@ -6,7 +6,9 @@ import com.trendyol.transmission.features.input.InputEffect 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 +import com.trendyol.transmission.transformer.handler.HandlerRegistry +import com.trendyol.transmission.transformer.handler.handlerRegistry +import com.trendyol.transmission.transformer.handler.registerEffect import com.trendyol.transmission.transformer.request.createIdentity import com.trendyol.transmission.ui.MultiOutputUiState import kotlinx.coroutines.CoroutineDispatcher @@ -20,23 +22,20 @@ class MultiOutputTransformer @Inject constructor( private val holder = buildDataHolder(MultiOutputUiState()) - override val effectHandler = buildGenericEffectHandler { effect -> - when (effect) { - is InputEffect.InputUpdate -> { - holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) } - val result = compute(OutputTransformer.outputCalculationContract) - holder.update { - it.copy(writtenUppercaseText = it.writtenUppercaseText + " ${result?.result}") - } - } - - is ColorPickerEffect.BackgroundColorUpdate -> { - holder.update { it.copy(backgroundColor = effect.color) } - } - - is ColorPickerEffect.SelectedColorUpdate -> { - holder.update { it.copy(selectedColor = effect.color) } + override val handlerRegistry: HandlerRegistry = handlerRegistry { + registerEffect { effect -> + holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) } + val result = compute(OutputTransformer.outputCalculationContract) + holder.update { + it.copy(writtenUppercaseText = it.writtenUppercaseText + " ${result?.result}") } } + registerEffect { effect -> + holder.update { it.copy(backgroundColor = effect.color) } + } + registerEffect { effect -> + holder.update { it.copy(selectedColor = effect.color) } + } } + } 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 39d6bff..4e13738 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,9 @@ import com.trendyol.transmission.features.input.InputEffect 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.handler.HandlerRegistry +import com.trendyol.transmission.transformer.handler.handlerRegistry +import com.trendyol.transmission.transformer.handler.registerEffect import com.trendyol.transmission.transformer.request.buildComputationContract import com.trendyol.transmission.transformer.request.buildExecutionContract import com.trendyol.transmission.transformer.request.computation.registerComputation @@ -47,28 +49,25 @@ class OutputTransformer @Inject constructor( } } - override val effectHandler = buildGenericEffectHandler { effect -> - when (effect) { - is InputEffect.InputUpdate -> { - holder.update { it.copy(outputText = effect.value) } - delay(3.seconds) - val selectedColor = getData(ColorPickerTransformer.holderContract) - selectedColor ?: return@buildGenericEffectHandler - holder.update { - it.copy(outputText = it.outputText + " and Selected color index is ${selectedColor.selectedColorIndex}") - } - delay(1.seconds) - send( - effect = ColorPickerEffect.BackgroundColorUpdate(holder2.getValue().backgroundColor), - identity = colorPickerIdentity - ) - execute(outputExecutionContract) - publish(effect = RouterEffect(holder.getValue())) - } - - is ColorPickerEffect.BackgroundColorUpdate -> { - holder.update { it.copy(backgroundColor = effect.color) } + override val handlerRegistry: HandlerRegistry = handlerRegistry { + registerEffect { effect -> + holder.update { it.copy(outputText = effect.value) } + delay(3.seconds) + val selectedColor = getData(ColorPickerTransformer.holderContract) + selectedColor ?: return@registerEffect + holder.update { + it.copy(outputText = it.outputText + " and Selected color index is ${selectedColor.selectedColorIndex}") } + delay(1.seconds) + send( + effect = ColorPickerEffect.BackgroundColorUpdate(holder2.getValue().backgroundColor), + identity = colorPickerIdentity + ) + execute(outputExecutionContract) + publish(effect = RouterEffect(holder.getValue())) + } + registerEffect { effect -> + holder.update { it.copy(backgroundColor = effect.color) } } } 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 acb91e3..a4205e3 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -5,6 +5,7 @@ import com.trendyol.transmission.effect.EffectWrapper import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.transformer.handler.CommunicationScope import com.trendyol.transmission.transformer.handler.EffectHandler +import com.trendyol.transmission.transformer.handler.HandlerRegistry import com.trendyol.transmission.transformer.handler.SignalHandler import com.trendyol.transmission.transformer.request.Contract import com.trendyol.transmission.transformer.request.Query @@ -42,8 +43,7 @@ open class Transformer( internal val dataChannel: Channel = Channel(capacity = Channel.BUFFERED) internal val storage = TransformerStorage() - open val signalHandler: SignalHandler? = null - open val effectHandler: EffectHandler? = null + protected open val handlerRegistry : HandlerRegistry? = null protected val executionRegistry: ExecutionRegistry by lazy { ExecutionRegistry(this) } protected val computationRegistry: ComputationRegistry by lazy { ComputationRegistry(this) } @@ -60,10 +60,8 @@ open class Transformer( fun startSignalCollection(incoming: SharedFlow) { transformerScope.launch { incoming.collect { - signalHandler?.apply { - currentSignalProcessing = launch { - communicationScope.onSignal(it) - } + currentSignalProcessing = launch { + handlerRegistry?.signalHandlerRegistry?.get(it::class)?.invoke(communicationScope, it) } } } @@ -84,10 +82,9 @@ open class Transformer( .filter { it.identity == null || it.identity == internalIdentity } .map { it.effect } .collect { - effectHandler?.apply { - currentEffectProcessing = launch { - communicationScope.onEffect(it) - } + currentEffectProcessing = launch { + handlerRegistry?.effectHandlerRegistry?.get(it::class) + ?.invoke(communicationScope, it) } } } diff --git a/transmission/src/test/kotlin/com/trendyol/transmission/transformer/FakeTransformer.kt b/transmission/src/test/kotlin/com/trendyol/transmission/transformer/FakeTransformer.kt index 4707f49..19d8d68 100644 --- a/transmission/src/test/kotlin/com/trendyol/transmission/transformer/FakeTransformer.kt +++ b/transmission/src/test/kotlin/com/trendyol/transmission/transformer/FakeTransformer.kt @@ -4,25 +4,29 @@ import com.trendyol.transmission.Transmission import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.transformer.data.TestData import com.trendyol.transmission.transformer.data.TestEffect +import com.trendyol.transmission.transformer.data.TestSignal 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.handler.HandlerRegistry +import com.trendyol.transmission.transformer.handler.handlerRegistry +import com.trendyol.transmission.transformer.handler.registerEffect +import com.trendyol.transmission.transformer.handler.registerSignal import kotlinx.coroutines.CoroutineDispatcher open class FakeTransformer(dispatcher: CoroutineDispatcher) : Transformer(dispatcher) { - val signalList = mutableListOf() - val effectList = mutableListOf() + val signalList = mutableListOf() + val effectList = mutableListOf() - private val holder = buildDataHolder(null) + private val holder = buildDataHolder(null) - override val signalHandler = buildGenericSignalHandler { signal -> - signalList.add(signal) - publish(TestEffect) - publish(RouterEffect("")) - holder.update { TestData("update with ${this@FakeTransformer.javaClass.simpleName}") } - } - - override val effectHandler = buildGenericEffectHandler { effect -> - effectList.add(effect) - } + override val handlerRegistry: HandlerRegistry = handlerRegistry { + registerSignal { signal -> + signalList.add(signal) + publish(TestEffect) + publish(RouterEffect("")) + holder.update { TestData("update with ${this@FakeTransformer.javaClass.simpleName}") } + } + registerEffect { effect -> + effectList.add(effect) + } + } } From e5ebc84a38c81afbe583206749d97e1c4e8458c2 Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 13:45:18 +0300 Subject: [PATCH 05/10] Convert TransmissionDataHolderBuilder into an object --- .../transmission/transformer/dataholder/DataHolderExt.kt | 2 +- .../transformer/dataholder/TransmissionDataHolderBuilder.kt | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) 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 e71f740..60a3272 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 @@ -20,7 +20,7 @@ fun Transformer.buildDataHolder( contract: Contract.Data? = null, publishUpdates: Boolean = true ): TransmissionDataHolder { - return TransmissionDataHolderBuilder().buildWith( + return TransmissionDataHolderBuilder.buildWith( initialValue = initialValue, publishUpdates = publishUpdates, transformer = this, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolderBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolderBuilder.kt index e79dbd7..bcf2ead 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolderBuilder.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/dataholder/TransmissionDataHolderBuilder.kt @@ -3,8 +3,9 @@ package com.trendyol.transmission.transformer.dataholder import com.trendyol.transmission.Transmission import com.trendyol.transmission.transformer.Transformer -internal class TransmissionDataHolderBuilder { - fun buildWith( +internal object TransmissionDataHolderBuilder { + + fun buildWith( initialValue: T, publishUpdates: Boolean = true, transformer: Transformer, From e8eed6d7340508fa9a9d6848df731bfa8d23eca2 Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 13:45:43 +0300 Subject: [PATCH 06/10] Remove mapping on transformet set --- .../main/java/com/trendyol/transmission/TransmissionRouter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt index 91a7c1d..331843b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt +++ b/transmission/src/main/java/com/trendyol/transmission/TransmissionRouter.kt @@ -52,7 +52,7 @@ class TransmissionRouter( "transformerSet should not be empty" } routerScope.launch { - transformerSet.map { transformer -> + transformerSet.forEach { transformer -> transformer.run { startSignalCollection(incoming = signalBroadcast.output) startDataPublishing(data = dataBroadcast.producer) From b1f6217d99240619ac783a5e79a0c3a702a2701a Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 13:46:43 +0300 Subject: [PATCH 07/10] Add supervisor scope to Transformer jobs --- .../transmission/transformer/Transformer.kt | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) 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 acb91e3..ff7e2fd 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.SendChannel @@ -26,13 +27,14 @@ import kotlinx.coroutines.flow.filterNot import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.launch +import kotlinx.coroutines.supervisorScope open class Transformer( dispatcher: CoroutineDispatcher = Dispatchers.Default, identity: Contract.Identity? = null ) { - val transformerScope = CoroutineScope(dispatcher) + val transformerScope = CoroutineScope(dispatcher + SupervisorJob()) private val internalIdentity: Contract.Identity = identity ?: createIdentity(this::class.simpleName.orEmpty()) @@ -61,7 +63,7 @@ open class Transformer( transformerScope.launch { incoming.collect { signalHandler?.apply { - currentSignalProcessing = launch { + currentSignalProcessing = transformerScope.launch { communicationScope.onSignal(it) } } @@ -78,21 +80,23 @@ open class Transformer( incoming: SharedFlow ) { transformerScope.launch { - launch { - incoming - .filterNot { it.effect is RouterEffect } - .filter { it.identity == null || it.identity == internalIdentity } - .map { it.effect } - .collect { - effectHandler?.apply { - currentEffectProcessing = launch { - communicationScope.onEffect(it) + supervisorScope { + launch { + incoming + .filterNot { it.effect is RouterEffect } + .filter { it.identity == null || it.identity == internalIdentity } + .map { it.effect } + .collect { + effectHandler?.apply { + currentEffectProcessing = launch { + communicationScope.onEffect(it) + } } } - } - } - launch { - effectChannel.receiveAsFlow().collect { producer.send(it) } + } + launch { + effectChannel.receiveAsFlow().collect { producer.send(it) } + } } } } @@ -102,16 +106,18 @@ open class Transformer( outGoingQuery: SendChannel ) { transformerScope.launch { - launch { - incomingQuery - .filter { it.owner == internalIdentity.key } - .collect { - this@Transformer.requestDelegate.resultBroadcast.producer.trySend(it) + supervisorScope { + launch { + incomingQuery + .filter { it.owner == internalIdentity.key } + .collect { + this@Transformer.requestDelegate.resultBroadcast.producer.trySend(it) + } + } + launch { + this@Transformer.requestDelegate.outGoingQuery.receiveAsFlow().collect { + outGoingQuery.trySend(it) } - } - launch { - this@Transformer.requestDelegate.outGoingQuery.receiveAsFlow().collect { - outGoingQuery.trySend(it) } } } From d304549d4f31c0b8519547a867fa1a92af989de7 Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 13:46:59 +0300 Subject: [PATCH 08/10] Remove unnecessary launch --- .../java/com/trendyol/transmission/router/RequestDelegate.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6089589..22b3a4b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -35,7 +35,7 @@ internal class RequestDelegate( init { queryScope.launch { - launch { outGoingQuery.consumeAsFlow().collect { processQuery(it) } } + outGoingQuery.consumeAsFlow().collect { processQuery(it) } } } From a5d6060a473881c93d9dd740e60dc4956b9f3fc6 Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 13:49:02 +0300 Subject: [PATCH 09/10] Make compotation&execution registries internal --- .../transformer/request/computation/ComputationRegistry.kt | 2 +- .../transformer/request/execution/ExecutionRegistry.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationRegistry.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationRegistry.kt index e14ae12..f29026c 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationRegistry.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationRegistry.kt @@ -3,7 +3,7 @@ package com.trendyol.transmission.transformer.request.computation import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.request.RequestHandler -class ComputationRegistry(private val transformer: Transformer) { +class ComputationRegistry internal constructor(private val transformer: Transformer) { internal fun buildWith( key: String, diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionRegistry.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionRegistry.kt index 98c0231..a1af1fc 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionRegistry.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/execution/ExecutionRegistry.kt @@ -3,7 +3,7 @@ package com.trendyol.transmission.transformer.request.execution import com.trendyol.transmission.transformer.Transformer import com.trendyol.transmission.transformer.request.RequestHandler -class ExecutionRegistry(private val transformer: Transformer) { +class ExecutionRegistry internal constructor(private val transformer: Transformer) { internal fun buildWith( key: String, From c085831b6957ecdf7cba02bfa6230bce54a7847e Mon Sep 17 00:00:00 2001 From: Ertugrul Karagoz Date: Mon, 29 Jul 2024 14:14:18 +0300 Subject: [PATCH 10/10] Remove unused imports --- .../com/trendyol/transmission/transformer/Transformer.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 25c4824..167f5dd 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/Transformer.kt @@ -4,9 +4,7 @@ import com.trendyol.transmission.Transmission import com.trendyol.transmission.effect.EffectWrapper import com.trendyol.transmission.effect.RouterEffect import com.trendyol.transmission.transformer.handler.CommunicationScope -import com.trendyol.transmission.transformer.handler.EffectHandler import com.trendyol.transmission.transformer.handler.HandlerRegistry -import com.trendyol.transmission.transformer.handler.SignalHandler import com.trendyol.transmission.transformer.request.Contract import com.trendyol.transmission.transformer.request.Query import com.trendyol.transmission.transformer.request.QueryResult @@ -45,7 +43,7 @@ open class Transformer( internal val dataChannel: Channel = Channel(capacity = Channel.BUFFERED) internal val storage = TransformerStorage() - protected open val handlerRegistry : HandlerRegistry? = null + protected open val handlerRegistry: HandlerRegistry? = null protected val executionRegistry: ExecutionRegistry by lazy { ExecutionRegistry(this) } protected val computationRegistry: ComputationRegistry by lazy { ComputationRegistry(this) } @@ -63,7 +61,8 @@ open class Transformer( transformerScope.launch { incoming.collect { currentSignalProcessing = transformerScope.launch { - handlerRegistry?.signalHandlerRegistry?.get(it::class)?.invoke(communicationScope, it) + handlerRegistry?.signalHandlerRegistry?.get(it::class) + ?.invoke(communicationScope, it) } } }