Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitozgumus committed Jul 29, 2024
2 parents 5fba3f4 + 9b2fc24 commit 99608db
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 176 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 @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class RequestDelegate(

init {
queryScope.launch {
launch { outGoingQuery.consumeAsFlow().collect { processQuery(it) } }
outGoingQuery.consumeAsFlow().collect { processQuery(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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.SignalHandler
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.request.Contract
import com.trendyol.transmission.transformer.request.Query
import com.trendyol.transmission.transformer.request.QueryResult
Expand All @@ -17,6 +16,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
Expand All @@ -26,13 +26,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())
Expand All @@ -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,9 @@ open class Transformer(
fun startSignalCollection(incoming: SharedFlow<Transmission.Signal>) {
transformerScope.launch {
incoming.collect {
signalHandler?.apply {
currentSignalProcessing = launch {
communicationScope.onSignal(it)
}
currentSignalProcessing = transformerScope.launch {
handlerRegistry?.signalHandlerRegistry?.get(it::class)
?.invoke(communicationScope, it)
}
}
}
Expand All @@ -78,21 +77,22 @@ open class Transformer(
incoming: SharedFlow<EffectWrapper>
) {
transformerScope.launch {
launch {
incoming
.filterNot { it.effect is RouterEffect }
.filter { it.identity == null || it.identity == internalIdentity }
.map { it.effect }
.collect {
effectHandler?.apply {
supervisorScope {
launch {
incoming
.filterNot { it.effect is RouterEffect }
.filter { it.identity == null || it.identity == internalIdentity }
.map { it.effect }
.collect {
currentEffectProcessing = launch {
communicationScope.onEffect(it)
handlerRegistry?.effectHandlerRegistry?.get(it::class)
?.invoke(communicationScope, it)
}
}
}
}
launch {
effectChannel.receiveAsFlow().collect { producer.send(it) }
}
launch {
effectChannel.receiveAsFlow().collect { producer.send(it) }
}
}
}
}
Expand All @@ -102,16 +102,18 @@ open class Transformer(
outGoingQuery: SendChannel<Query>
) {
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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun <T : Transmission.Data?> Transformer.buildDataHolder(
contract: Contract.Data<T>? = null,
publishUpdates: Boolean = true
): TransmissionDataHolder<T> {
return TransmissionDataHolderBuilder<T>().buildWith(
return TransmissionDataHolderBuilder.buildWith(
initialValue = initialValue,
publishUpdates = publishUpdates,
transformer = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ internal class TransmissionDataHolderImpl<T : Transmission.Data?>(
override fun update(updater: (T) -> @UnsafeVariance T) {
holder.update(updater)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package com.trendyol.transmission.transformer.dataholder
import com.trendyol.transmission.Transmission
import com.trendyol.transmission.transformer.Transformer

internal class TransmissionDataHolderBuilder<T : Transmission.Data?> {
fun buildWith(
internal object TransmissionDataHolderBuilder {

fun <T : Transmission.Data?> buildWith(
initialValue: T,
publishUpdates: Boolean = true,
transformer: Transformer,
Expand Down

This file was deleted.

Loading

0 comments on commit 99608db

Please sign in to comment.