entities.EntityResolver.kt Maven / Gradle / Ivy
package com.github.fluidsonic.baku
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.flatMap
import kotlin.reflect.KClass
internal class EntityResolver(
private val resolvers: Map, suspend Transaction.(ids: Set) -> ReceiveChannel>
) {
suspend fun resolve(ids: Set, transaction: Transaction) =
ids
.groupBy { it.factory }
.map { (factory, ids) ->
resolvers[factory.idClass]
?.let { resolve -> transaction.resolve(ids.toSet()) }
?: GlobalScope.emptyReceiveChannel()
}
.toChannel()
.flatMap { it }
}