Skip to content

Commit

Permalink
fix: allows middlewares to also be processed with closure commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Brotas committed Oct 18, 2023
1 parent 8da0b5b commit 7838953
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (bus *Bus) Handle(cmd Command) (any, error) {
if err != nil {
return nil, err
}
return bus.handle(hdl, cmd)
return bus.handle(hdl, cmd, nil)
}

// HandleAsync processes the command asynchronously using workers through their respective handler.
Expand Down Expand Up @@ -174,14 +174,19 @@ func (bus *Bus) worker(asyncCommandsQueue <-chan *Async, closed chan<- bool) {
closed <- true
}

func (bus *Bus) handle(hdl Handler, cmd Command) (data any, err error) {
func (bus *Bus) handle(hdl Handler, cmd Command, cls ClosureCommand) (data any, err error) {
for _, inMdl := range bus.inwardMiddlewares {
if err = inMdl.HandleInward(cmd); err != nil {
bus.error(cmd, err)
return
}
}
if data, err = hdl.Handle(cmd); err != nil {
if cls != nil {
data, err = cls()
} else {
data, err = hdl.Handle(cmd)
}
if err != nil {
data = nil
bus.error(cmd, err)
}
Expand All @@ -198,13 +203,7 @@ func (bus *Bus) handle(hdl Handler, cmd Command) (data any, err error) {
}

func (bus *Bus) handleAsync(async *Async) {
var data any
var err error
if async.isCls {
data, err = async.cls()
} else {
data, err = bus.handle(async.hdl, async.cmd)
}
data, err := bus.handle(async.hdl, async.cmd, async.cls)
if err != nil {
async.fail(err)
return
Expand Down

0 comments on commit 7838953

Please sign in to comment.