Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to invoke publish method with custom publishStrategy #92

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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