Skip to content

Commit 94090d9

Browse files
authored
Cache pipeline behaviours on mediatorImpl (#370)
1 parent 7aa3525 commit 94090d9

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

projects/kediatr-core/src/main/kotlin/com/trendyol/kediatr/MediatorImpl.kt

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,30 @@ class MediatorImpl(
44
private val registry: Registry,
55
private val defaultPublishStrategy: PublishStrategy = StopOnExceptionPublishStrategy()
66
) : Mediator {
7-
override suspend fun <TQuery : Query<TResponse>, TResponse> send(query: TQuery): TResponse =
8-
processPipeline(
9-
registry.getPipelineBehaviors(),
10-
query
11-
) {
12-
registry.resolveQueryHandler(query.javaClass).handle(query)
13-
}
7+
private val sortedPipelineBehaviors by lazy { registry.getPipelineBehaviors().sortedByDescending { it.order } }
148

15-
override suspend fun <TCommand : Command> send(command: TCommand) =
16-
processPipeline(
17-
registry.getPipelineBehaviors(),
18-
command
19-
) {
20-
registry.resolveCommandHandler(command.javaClass).handle(command)
21-
}
9+
override suspend fun <TQuery : Query<TResponse>, TResponse> send(
10+
query: TQuery
11+
): TResponse = handle(query) { registry.resolveQueryHandler(query.javaClass).handle(query) }
2212

23-
override suspend fun <TCommand : CommandWithResult<TResult>, TResult> send(command: TCommand): TResult =
24-
processPipeline(
25-
registry.getPipelineBehaviors(),
26-
command
27-
) {
28-
registry.resolveCommandWithResultHandler(command.javaClass).handle(command)
29-
}
13+
override suspend fun <TCommand : Command> send(
14+
command: TCommand
15+
) = handle(command) { registry.resolveCommandHandler(command.javaClass).handle(command) }
16+
17+
override suspend fun <TCommand : CommandWithResult<TResult>, TResult> send(
18+
command: TCommand
19+
): TResult = handle(command) { registry.resolveCommandWithResultHandler(command.javaClass).handle(command) }
3020

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

3323
override suspend fun <T : Notification> publish(
3424
notification: T,
3525
publishStrategy: PublishStrategy
36-
) = processPipeline(
37-
registry.getPipelineBehaviors(),
38-
notification
39-
) {
40-
publishStrategy.publish(notification, registry.resolveNotificationHandlers(notification.javaClass))
41-
}
26+
) = handle(notification) { publishStrategy.publish(notification, registry.resolveNotificationHandlers(notification.javaClass)) }
4227

43-
private suspend fun <TRequest, TResponse> processPipeline(
44-
pipelineBehaviors: Collection<PipelineBehavior>,
28+
private suspend fun <TRequest, TResponse> handle(
4529
request: TRequest,
4630
handler: RequestHandlerDelegate<TRequest, TResponse>
47-
): TResponse =
48-
pipelineBehaviors
49-
.sortedByDescending { it.order }
50-
.fold(handler) { next, pipeline ->
51-
{ pipeline.handle(request) { next(it) } }
52-
}(request)
31+
): TResponse = sortedPipelineBehaviors
32+
.fold(handler) { next, pipeline -> { pipeline.handle(request) { next(it) } } }(request)
5333
}

0 commit comments

Comments
 (0)