commonMain.de.halfbit.componental.router.Router.kt Maven / Gradle / Ivy
/** Copyright 2024 Halfbit GmbH, Sergej Shafarenka */
package de.halfbit.componental.router
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
public abstract class Router(
public val name: String
) {
private val channel = Channel(capacity = 64)
public val transformers: Flow =
flow {
while (true)
emit(channel.receive())
}.onCompletion {
channel.close()
}
public fun route(transform: Transform) {
val result = channel.trySend(transform)
if (result.isFailure && !result.isClosed) {
throw IllegalStateException(
"Failed to schedule transform to a not closed channel"
)
}
}
}