Skip to content

Commit

Permalink
refactor(cmdbus): check if bus handles command by name (closes #95)
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Mar 28, 2023
1 parent 67438ec commit 914806d
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions command/cmdbus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,15 @@ func (b *Bus[ErrorCode]) commandDispatched(evt event.Of[CommandDispatchedData])
return
}

// if the bus does not handle the dispatched command, return
if !b.handles(data.Name) {
return
}

cmd := command.New(data.Name, load, command.ID(data.ID), command.Aggregate(data.AggregateName, data.AggregateID))

// if the bus does not handle the dispatched command, return
if !b.handles(cmd) {
// apply user-defined filters
if !b.filterAllows(cmd) {
return
}

Expand All @@ -451,24 +456,25 @@ func (b *Bus[ErrorCode]) commandDispatched(evt event.Of[CommandDispatchedData])
b.requested[data.ID] = cmd
}

func (b *Bus[ErrorCode]) handles(cmd command.Command) bool {
b.debugLog("checking if %q command is handled by this bus ...", cmd.Name())
func (b *Bus[ErrorCode]) handles(name string) bool {
b.debugLog("checking if %q command is handled by this bus ...", name)
b.subMux.RLock()
defer b.subMux.RUnlock()
if _, ok := b.subscriptions[cmd.Name()]; !ok {
b.debugLog("this bus does not handle %q commands", cmd.Name())
if _, ok := b.subscriptions[name]; !ok {
b.debugLog("this bus does not handle %q commands", name)
return false
}
b.debugLog("this bus handles %q commands", name)
return true
}

b.debugLog("this bus handles %q commands", cmd.Name())

func (b *Bus[ErrorCode]) filterAllows(cmd command.Command) bool {
for _, fn := range b.filters {
if !fn(cmd) {
b.debugLog("filtered out %q command", cmd.Name())
return false
}
}

return true
}

Expand Down

0 comments on commit 914806d

Please sign in to comment.