Skip to content

Commit

Permalink
Add possibility to invoke publish method with custom publishStrategy (#…
Browse files Browse the repository at this point in the history
…92)

Co-authored-by: Adam Waniak <adam.waniak@brightinventions.pl>
  • Loading branch information
awaniak and adam-waniak-bright authored Apr 12, 2023
1 parent b103192 commit 21ef304
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ interface Mediator {
* @param T any [Notification] subclass to publish
*/
suspend fun <T : Notification> publish(notification: T)

suspend fun <T : Notification> publish(notification: T, publishStrategy: PublishStrategy)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.trendyol.kediatr
class MediatorBuilder(
private val dependencyProvider: DependencyProvider,
) {
internal var publishStrategy: PublishStrategy = StopOnExceptionPublishStrategy()
internal var defaultPublishStrategy: PublishStrategy = StopOnExceptionPublishStrategy()
private set

/**
Expand All @@ -18,11 +18,11 @@ class MediatorBuilder(
* @see [ParallelWhenAllPublishStrategy]
*/
fun withPublishStrategy(publishStrategy: PublishStrategy): MediatorBuilder {
this.publishStrategy = publishStrategy
this.defaultPublishStrategy = publishStrategy
return this
}

fun build(registry: Registry = RegistryImpl(dependencyProvider)): Mediator {
return MediatorImpl(registry, publishStrategy)
return MediatorImpl(registry, defaultPublishStrategy)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.trendyol.kediatr

class MediatorImpl(
private val registry: Registry,
private val publishStrategy: PublishStrategy = StopOnExceptionPublishStrategy(),
private val defaultPublishStrategy: PublishStrategy = StopOnExceptionPublishStrategy(),
) : Mediator {

override suspend fun <TQuery : Query<TResponse>, TResponse> send(query: TQuery): TResponse = processPipeline(
Expand All @@ -26,7 +26,12 @@ class MediatorImpl(
registry.resolveCommandWithResultHandler(command.javaClass).handle(command)
}

override suspend fun <T : Notification> publish(notification: T) = processPipeline(
override suspend fun <T : Notification> publish(notification: T) = publish(notification, defaultPublishStrategy)

override suspend fun <T : Notification> publish(
notification: T,
publishStrategy: PublishStrategy,
) = processPipeline(
registry.getPipelineBehaviors(),
notification
) {
Expand All @@ -38,8 +43,8 @@ class MediatorImpl(
request: TRequest,
handler: RequestHandlerDelegate<TRequest, TResponse>,
): TResponse = pipelineBehaviors
.reversed()
.fold(handler) { next, pipeline ->
{ pipeline.handle(request) { next(it) } }
}(request)
.reversed()
.fold(handler) { next, pipeline ->
{ pipeline.handle(request) { next(it) } }
}(request)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class MediatorBuilderTest {
.withPublishStrategy(expectedStrategy)

// Assert
assertEquals(expectedStrategy, builder.publishStrategy)
assertEquals(expectedStrategy, builder.defaultPublishStrategy)
}

companion object {
Expand Down

0 comments on commit 21ef304

Please sign in to comment.