Skip to content

Commit

Permalink
Merge pull request #16 from Trendyol/handler-api-v2
Browse files Browse the repository at this point in the history
Handler api v2
  • Loading branch information
yigitozgumus authored Jul 29, 2024
2 parents 6b478ce + 9375114 commit db7f308
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ColorPickerSignal.SelectColor> { 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<ColorPickerEffect.BackgroundColorUpdate> { effect ->
holder.update {
it.copy(backgroundColor = effect.color)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,20 +34,13 @@ class InputTransformer @Inject constructor(
}
}

override val signalHandler = buildTypedSignalHandler<InputSignal> { 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<InputSignal.InputUpdate> { 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<ColorPickerEffect.BackgroundColorUpdate> { effect ->
holder.update { it.copy(backgroundColor = effect.color) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<InputEffect.InputUpdate> { effect ->
holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) }
val result = compute(OutputTransformer.outputCalculationContract)
holder.update {
it.copy(writtenUppercaseText = it.writtenUppercaseText + " ${result?.result}")
}
}
registerEffect<ColorPickerEffect.BackgroundColorUpdate> { effect ->
holder.update { it.copy(backgroundColor = effect.color) }
}
registerEffect<ColorPickerEffect.SelectedColorUpdate> { effect ->
holder.update { it.copy(selectedColor = effect.color) }
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<InputEffect.InputUpdate> { 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<ColorPickerEffect.BackgroundColorUpdate> { effect ->
holder.update { it.copy(backgroundColor = effect.color) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,8 +43,7 @@ open class Transformer(
internal val dataChannel: Channel<Transmission.Data> = 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) }
Expand All @@ -60,10 +60,8 @@ open class Transformer(
fun startSignalCollection(incoming: SharedFlow<Transmission.Signal>) {
transformerScope.launch {
incoming.collect {
signalHandler?.apply {
currentSignalProcessing = launch {
communicationScope.onSignal(it)
}
currentSignalProcessing = launch {
handlerRegistry?.signalHandlerRegistry?.get(it::class)?.invoke(communicationScope, it)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 <reified T : Transmission.Effect> HandlerScope.registerEffect(noinline lambda: suspend CommunicationScope.(effect: T) -> Unit) {
handlerRegistry.registerEffect<T>(lambda)
}

inline fun <reified T : Transmission.Signal> HandlerScope.registerSignal(noinline lambda: suspend CommunicationScope.(signal: T) -> Unit) {
handlerRegistry.registerSignal<T>(lambda)
}

class HandlerRegistry internal constructor() {

@PublishedApi
internal val signalHandlerRegistry =
mutableMapOf<KClass<out Transmission.Signal>, suspend CommunicationScope.(effect: Transmission.Signal) -> Unit>()

@PublishedApi
internal val effectHandlerRegistry =
mutableMapOf<KClass<out Transmission.Effect>, suspend CommunicationScope.(effect: Transmission.Effect) -> Unit>()

@PublishedApi
internal inline fun <reified T : Transmission.Signal> registerSignal(noinline lambda: suspend CommunicationScope.(signal: T) -> Unit) {
signalHandlerRegistry[T::class] =
lambda as suspend CommunicationScope.(Transmission.Signal) -> Unit
}

@PublishedApi
internal inline fun <reified T : Transmission.Effect> registerEffect(noinline lambda: suspend CommunicationScope.(effect: T) -> Unit) {
effectHandlerRegistry[T::class] =
lambda as suspend CommunicationScope.(Transmission.Effect) -> Unit
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Transmission.Signal>()
val effectList = mutableListOf<Transmission.Effect>()
val signalList = mutableListOf<Transmission.Signal>()
val effectList = mutableListOf<Transmission.Effect>()

private val holder = buildDataHolder<TestData?>(null)
private val holder = buildDataHolder<TestData?>(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<TestSignal> { signal ->
signalList.add(signal)
publish(TestEffect)
publish(RouterEffect(""))
holder.update { TestData("update with ${this@FakeTransformer.javaClass.simpleName}") }
}
registerEffect<TestEffect> { effect ->
effectList.add(effect)
}
}
}

0 comments on commit db7f308

Please sign in to comment.