Skip to content

Commit

Permalink
Add a way to extend handlers, computations and executions for transfo…
Browse files Browse the repository at this point in the history
…rmers
  • Loading branch information
yigitozgumus committed Oct 11, 2024
1 parent 1f4c2d7 commit 14543bc
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import com.trendyol.transmission.DefaultDispatcher
import com.trendyol.transmission.components.features.multioutput.multiOutputTransformerIdentity
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.dataHolder
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.handler.handlers
import com.trendyol.transmission.transformer.handler.createHandlers
import com.trendyol.transmission.transformer.handler.onEffect
import com.trendyol.transmission.transformer.handler.onSignal
import com.trendyol.transmission.transformer.request.Contracts
import com.trendyol.transmission.transformer.request.dataHolder
import com.trendyol.transmission.transformer.request.identity
import com.trendyol.transmission.components.features.ColorPickerUiState
import com.trendyol.transmission.transformer.handler.Handlers
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Inject

Expand All @@ -23,7 +23,7 @@ class ColorPickerTransformer @Inject constructor(

private val holder = dataHolder(ColorPickerUiState(), holderContract)

override val handlers: HandlerRegistry = handlers {
override val handlers: Handlers = createHandlers {
onSignal<ColorPickerSignal.SelectColor> { signal ->
holder.update { it.copy(selectedColorIndex = signal.index) }
publish(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import com.trendyol.transmission.DefaultDispatcher
import com.trendyol.transmission.components.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.dataHolder
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.handler.handlers
import com.trendyol.transmission.transformer.handler.Handlers
import com.trendyol.transmission.transformer.handler.createHandlers
import com.trendyol.transmission.transformer.handler.onEffect
import com.trendyol.transmission.transformer.handler.onSignal
import com.trendyol.transmission.transformer.request.Contracts
import com.trendyol.transmission.transformer.request.computation
import com.trendyol.transmission.transformer.request.computation.ComputationRegistry
import com.trendyol.transmission.transformer.request.computation.computations
import com.trendyol.transmission.transformer.request.computation.Computations
import com.trendyol.transmission.transformer.request.computation.createComputations
import com.trendyol.transmission.transformer.request.computation.register
import com.trendyol.transmission.transformer.request.computationWithArgs
import com.trendyol.transmission.transformer.request.dataHolder
import com.trendyol.transmission.transformer.request.identity
import com.trendyol.transmission.components.features.InputUiState
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
Expand All @@ -28,7 +27,7 @@ class InputTransformer @Inject constructor(

private val holder = dataHolder(InputUiState(), holderContract)

override val computations: ComputationRegistry = computations {
override val computations: Computations = createComputations {
register(writtenInputContract) {
delay(1.seconds)
WrittenInput(holder.getValue().writtenText)
Expand All @@ -38,7 +37,7 @@ class InputTransformer @Inject constructor(
}
}

override val handlers: HandlerRegistry = handlers {
override val handlers: Handlers = createHandlers {
onSignal<InputSignal.InputUpdate> { signal ->
holder.update { it.copy(writtenText = signal.value) }
publish(effect = InputEffect.InputUpdate(signal.value))
Expand All @@ -54,3 +53,4 @@ class InputTransformer @Inject constructor(
val holderContract = Contracts.dataHolder<InputUiState>()
}
}

Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.trendyol.transmission.components.features.multioutput

import com.trendyol.transmission.DefaultDispatcher
import com.trendyol.transmission.components.features.MultiOutputUiState
import com.trendyol.transmission.components.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.components.features.input.InputEffect
import com.trendyol.transmission.components.features.output.OutputTransformer
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.dataHolder
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.handler.handlers
import com.trendyol.transmission.transformer.handler.Handlers
import com.trendyol.transmission.transformer.handler.createHandlers
import com.trendyol.transmission.transformer.handler.onEffect
import com.trendyol.transmission.transformer.request.Contracts
import com.trendyol.transmission.transformer.request.identity
import com.trendyol.transmission.components.features.MultiOutputUiState
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Inject

Expand All @@ -23,7 +23,7 @@ class MultiOutputTransformer @Inject constructor(

private val holder = dataHolder(MultiOutputUiState())

override val handlers: HandlerRegistry = handlers {
override val handlers: Handlers = createHandlers {
onEffect<InputEffect.InputUpdate> { effect ->
holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) }
val result = compute(OutputTransformer.outputCalculationContract)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@ package com.trendyol.transmission.components.features.output

import android.util.Log
import com.trendyol.transmission.DefaultDispatcher
import com.trendyol.transmission.effect.RouterEffect
import com.trendyol.transmission.components.features.ColorPickerUiState
import com.trendyol.transmission.components.features.OutputUiState
import com.trendyol.transmission.components.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.components.features.colorpicker.ColorPickerTransformer
import com.trendyol.transmission.components.features.colorpicker.colorPickerIdentity
import com.trendyol.transmission.components.features.input.InputEffect
import com.trendyol.transmission.components.features.input.InputTransformer
import com.trendyol.transmission.effect.RouterEffect
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.dataHolder
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.handler.handlers
import com.trendyol.transmission.transformer.handler.Handlers
import com.trendyol.transmission.transformer.handler.createHandlers
import com.trendyol.transmission.transformer.handler.onEffect
import com.trendyol.transmission.transformer.request.Contracts
import com.trendyol.transmission.transformer.request.computation
import com.trendyol.transmission.transformer.request.computation.ComputationRegistry
import com.trendyol.transmission.transformer.request.computation.computations
import com.trendyol.transmission.transformer.request.computation.Computations
import com.trendyol.transmission.transformer.request.computation.createComputations
import com.trendyol.transmission.transformer.request.computation.register
import com.trendyol.transmission.transformer.request.execution
import com.trendyol.transmission.transformer.request.execution.ExecutionRegistry
import com.trendyol.transmission.transformer.request.execution.executions
import com.trendyol.transmission.transformer.request.execution.Executions
import com.trendyol.transmission.transformer.request.execution.createExecutions
import com.trendyol.transmission.transformer.request.execution.register
import com.trendyol.transmission.transformer.request.identity
import com.trendyol.transmission.components.features.ColorPickerUiState
import com.trendyol.transmission.components.features.OutputUiState
import com.trendyol.transmission.ui.theme.Pink80
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
Expand All @@ -40,7 +39,7 @@ class OutputTransformer @Inject constructor(

private val holder2 = dataHolder(ColorPickerUiState(), publishUpdates = false)

override val computations: ComputationRegistry = computations {
override val computations: Computations = createComputations {
register(outputCalculationContract) {
delay(2.seconds)
val data = getData(ColorPickerTransformer.holderContract)?.selectedColorIndex
Expand All @@ -50,7 +49,7 @@ class OutputTransformer @Inject constructor(
}
}

override val executions: ExecutionRegistry = executions {
override val executions: Executions = createExecutions {
register(outputExecutionContract) {
delay(4.seconds)
communicationScope.publish(ColorPickerEffect.BackgroundColorUpdate(Pink80))
Expand All @@ -61,7 +60,7 @@ class OutputTransformer @Inject constructor(
}
}

override val handlers: HandlerRegistry = handlers {
override val handlers: Handlers = createHandlers {
onEffect<InputEffect.InputUpdate> { effect ->
holder.update { it.copy(outputText = effect.value) }
delay(3.seconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.dataHolder
import com.trendyol.transmission.transformer.request.Contracts
import com.trendyol.transmission.transformer.request.computation.ComputationRegistry
import com.trendyol.transmission.transformer.request.computation.computations
import com.trendyol.transmission.transformer.request.computation.createComputations
import com.trendyol.transmission.transformer.request.computation.register
import com.trendyol.transmission.transformer.request.computationWithArgs

Expand All @@ -17,7 +17,7 @@ class Holder : Transformer() {

val counterData = dataHolder(TestCounter(0))

override val computations: ComputationRegistry = computations {
override val computationRegistry: ComputationRegistry = createComputations {
register(lookUpAndReturn) { id ->
counterData.updateAndGet { it.copy(value = it.value.plus(1)) }.value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package com.trendyol.transmission.counter

import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.handler.handlers
import com.trendyol.transmission.transformer.handler.createHandlers
import com.trendyol.transmission.transformer.handler.onSignal

class Worker(val id: String) : Transformer() {

override val handlers: HandlerRegistry = handlers {
override val handlers: HandlerRegistry = createHandlers {
onSignal<CounterSignal.Lookup> {
send(CounterData("Transformer $id updated data to ${compute(lookUpAndReturn, id)}"))
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.shareIn

Expand All @@ -14,12 +13,15 @@ interface Broadcast<T> {
val output: SharedFlow<T>
}

fun <T> CoroutineScope.createBroadcast(): Broadcast<T> = object : Broadcast<T> {
internal fun <T> CoroutineScope.createBroadcast(
sharingStarted: SharingStarted = SharingStarted.WhileSubscribed()
): Broadcast<T> = object : Broadcast<T> {

private val _source = Channel<T>(capacity = Channel.BUFFERED)
override val producer: SendChannel<T> = _source

override val output by lazy {
_source.receiveAsFlow()
.shareIn(this@createBroadcast, SharingStarted.Lazily)
.shareIn(this@createBroadcast, sharingStarted)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TransmissionRouter internal constructor(

val dataStream = dataBroadcast.output
val effectStream: SharedFlow<Transmission.Effect> = effectBroadcast.output.map { it.effect }
.shareIn(routerScope, SharingStarted.Lazily)
.shareIn(routerScope, SharingStarted.WhileSubscribed())

private val _requestDelegate = RequestDelegate(
queryScope = routerScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ 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.ExtendedHandlers
import com.trendyol.transmission.transformer.handler.Handlers
import com.trendyol.transmission.transformer.handler.HandlerRegistry
import com.trendyol.transmission.transformer.request.Contract
import com.trendyol.transmission.transformer.request.Contracts
import com.trendyol.transmission.transformer.request.Query
import com.trendyol.transmission.transformer.request.QueryResult
import com.trendyol.transmission.transformer.request.TransformerRequestDelegate
import com.trendyol.transmission.transformer.request.computation.ComputationRegistry
import com.trendyol.transmission.transformer.request.computation.Computations
import com.trendyol.transmission.transformer.request.computation.ExtendedComputations
import com.trendyol.transmission.transformer.request.execution.ExecutionRegistry
import com.trendyol.transmission.transformer.request.execution.Executions
import com.trendyol.transmission.transformer.request.execution.ExtendedExecutions
import com.trendyol.transmission.transformer.request.identity
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineExceptionHandler
Expand Down Expand Up @@ -49,10 +55,20 @@ open class Transformer(
internal val dataChannel: Channel<Transmission.Data> = Channel(capacity = Channel.BUFFERED)
internal val storage = TransformerStorage()

protected open val handlers: HandlerRegistry? = null
internal val handlerRegistry by lazy { HandlerRegistry() }
internal val executionRegistry: ExecutionRegistry by lazy { ExecutionRegistry(this) }
internal val computationRegistry: ComputationRegistry by lazy {
ComputationRegistry(this)
}

protected open val handlers: Handlers by lazy { Handlers() }
protected open val extendedHandlers: ExtendedHandlers by lazy { ExtendedHandlers() }

protected open val computations: Computations by lazy { Computations() }
protected open val extendedComputations: ExtendedComputations by lazy { ExtendedComputations() }

protected open val executions: ExecutionRegistry? = null
protected open val computations: ComputationRegistry? = null
protected open val executions: Executions by lazy { Executions() }
protected open val extendedExecutions: ExtendedExecutions by lazy { ExtendedExecutions() }

var currentEffectProcessing: Job? = null
var currentSignalProcessing: Job? = null
Expand All @@ -67,8 +83,10 @@ open class Transformer(
transformerScope.launch {
incoming.collect {
currentSignalProcessing = transformerScope.launch {
handlers?.signalHandlerRegistry?.get(it::class)
?.invoke(communicationScope, it)
handlerRegistry.signalHandlerRegistry[it::class]?.invoke(
communicationScope,
it
)
}
}
}
Expand All @@ -91,7 +109,7 @@ open class Transformer(
.map { it.effect }
.collect {
currentEffectProcessing = launch {
handlers?.effectHandlerRegistry?.get(it::class)
handlerRegistry.effectHandlerRegistry[it::class]
?.invoke(communicationScope, it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ internal class TransformerStorage {
private val internalExecutionMap: MutableMap<String, ExecutionOwner> =
mutableMapOf()

fun clearComputations() {
internalComputationMap.clear()
}

fun clearExecutions() {
internalExecutionMap.clear()
}

fun isHolderStateInitialized(): Boolean {
return internalTransmissionHolderSet is HolderState.Initialized
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import kotlin.reflect.KClass

class HandlerRegistry internal constructor() {

internal fun clear() {
signalHandlerRegistry.clear()
effectHandlerRegistry.clear()
}

@PublishedApi
internal val signalHandlerRegistry =
mutableMapOf<KClass<out Transmission.Signal>, suspend CommunicationScope.(effect: Transmission.Signal) -> Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import com.trendyol.transmission.transformer.Transformer

class HandlerScope internal constructor(val handlerRegistry: HandlerRegistry)

fun Transformer.handlers(scope: HandlerScope.() -> Unit): HandlerRegistry {
val handlerRegistry = HandlerRegistry()
class Handlers internal constructor()

class ExtendedHandlers internal constructor()

fun Transformer.extendedHandlers(scope: HandlerScope.() -> Unit): ExtendedHandlers {
HandlerScope(handlerRegistry).apply(scope)
return ExtendedHandlers()
}

fun Transformer.createHandlers(scope: HandlerScope.() -> Unit): Handlers {
this.handlerRegistry.clear()
HandlerScope(handlerRegistry).apply(scope)
return handlerRegistry
return Handlers()
}

inline fun <reified T : Transmission.Effect> HandlerScope.onEffect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package com.trendyol.transmission.transformer.request.computation
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.request.RequestHandler

class ComputationRegistry internal constructor(private val transformer: Transformer) {
internal class ComputationRegistry internal constructor(private val transformer: Transformer) {

internal fun clear() {
transformer.storage.clearComputations()
}

internal fun <T : Any> buildWith(
key: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import com.trendyol.transmission.transformer.Transformer

class ComputationScope internal constructor(internal val computationRegistry: ComputationRegistry)

fun Transformer.computations(scope: ComputationScope.() -> Unit): ComputationRegistry {
val computationRegistry = ComputationRegistry(this)
class Computations internal constructor()

class ExtendedComputations internal constructor()

fun Transformer.createComputations(scope: ComputationScope.() -> Unit): Computations {
this.computationRegistry.clear()
ComputationScope(computationRegistry).apply(scope)
return Computations()
}

fun Transformer.extendComputations(scope: ComputationScope.() -> Unit): ExtendedComputations {
ComputationScope(computationRegistry).apply(scope)
return computationRegistry
return ExtendedComputations()
}
Loading

0 comments on commit 14543bc

Please sign in to comment.