Skip to content

Commit

Permalink
remove interface and linq dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ssengalanto committed Jul 20, 2023
1 parent 38d0624 commit 83305a4
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions midt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,8 @@ import (
"context"
"fmt"
"reflect"

"github.com/ahmetb/go-linq/v3"
)

type Mediator interface {
RegisterRequestHandler(handler RequestHandler) error
RegisterNotificationHandler(handler NotificationHandler) error
RegisterPipelineBehaviour(behaviour PipelineBehaviour) error
Send(ctx context.Context, request any) (any, error)
Publish(ctx context.Context, request any) error
}

type RequestHandler interface {
Name() string
Handle(ctx context.Context, request any) (any, error)
Expand Down Expand Up @@ -86,7 +76,7 @@ func (m *Midt) RegisterPipelineBehaviour(behaviour PipelineBehaviour) error {
return fmt.Errorf("%w: %s", ErrPipelineBehaviourAlreadyExists, bt)
}

m.pipelineBehaviourRegistry = prepend(m.pipelineBehaviourRegistry, behaviour)
m.pipelineBehaviourRegistry = append(m.pipelineBehaviourRegistry, behaviour)
return nil
}

Expand All @@ -104,21 +94,17 @@ func (m *Midt) Send(ctx context.Context, request any) (any, error) {
return handler.Handle(ctx, request)
}

aggregateResult := linq.From(m.pipelineBehaviourRegistry).AggregateWithSeedT(
lastHandler,
func(next RequestHandlerFunc, pipe PipelineBehaviour) RequestHandlerFunc {
pipeValue := pipe
nexValue := next

var handlerFunc RequestHandlerFunc = func() (any, error) {
return pipeValue.Handle(ctx, request, nexValue)
}
var aggregateResult = lastHandler
for _, pipe := range m.pipelineBehaviourRegistry {
pipeValue := pipe
nextValue := aggregateResult

return handlerFunc
})
aggregateResult = func() (any, error) {
return pipeValue.Handle(ctx, request, nextValue)
}
}

v := aggregateResult.(RequestHandlerFunc)
response, err := v()
response, err := aggregateResult()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,9 +146,3 @@ func (m *Midt) existsPipeType(p reflect.Type) bool {
}
return false
}

// prepend an element in the slice.
func prepend[T any](x []T, y T) []T {
x = append([]T{y}, x...)
return x
}

0 comments on commit 83305a4

Please sign in to comment.