All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.trendyol.kediatr.MediatorImpl.kt Maven / Gradle / Ivy

package com.trendyol.kediatr

class MediatorImpl(
  private val registry: Registry,
  private val defaultPublishStrategy: PublishStrategy = StopOnExceptionPublishStrategy()
) : Mediator {
  override suspend fun , TResponse> send(query: TQuery): TResponse =
    processPipeline(
      registry.getPipelineBehaviors(),
      query
    ) {
      registry.resolveQueryHandler(query.javaClass).handle(query)
    }

  override suspend fun  send(command: TCommand) =
    processPipeline(
      registry.getPipelineBehaviors(),
      command
    ) {
      registry.resolveCommandHandler(command.javaClass).handle(command)
    }

  override suspend fun , TResult> send(command: TCommand): TResult =
    processPipeline(
      registry.getPipelineBehaviors(),
      command
    ) {
      registry.resolveCommandWithResultHandler(command.javaClass).handle(command)
    }

  override suspend fun  publish(notification: T) = publish(notification, defaultPublishStrategy)

  override suspend fun  publish(
    notification: T,
    publishStrategy: PublishStrategy
  ) = processPipeline(
    registry.getPipelineBehaviors(),
    notification
  ) {
    publishStrategy.publish(notification, registry.resolveNotificationHandlers(notification.javaClass))
  }

  private suspend fun  processPipeline(
    pipelineBehaviors: Collection,
    request: TRequest,
    handler: RequestHandlerDelegate
  ): TResponse =
    pipelineBehaviors
      .sortedByDescending { it.order }
      .fold(handler) { next, pipeline ->
        { pipeline.handle(request) { next(it) } }
      }(request)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy