-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefining.go
52 lines (41 loc) · 1.19 KB
/
defining.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package decs
import (
"fmt"
"strings"
)
const UnlimitedUndoables uint = 0
type CommandDefinition struct {
Name string
DataType interface{}
RelatedInFlow func(lhs Command, rhs Command) bool
UndoneEvents []string
AutoFocusable bool
Category string
UndoableLimit uint
}
func (cd *CommandDefinition) Flowing() bool {
return cd.RelatedInFlow != nil
}
func (cd *CommandDefinition) validate() {
if cd == nil || len(strings.TrimSpace(cd.Name)) == 0 {
panic(fmt.Errorf("invalid command definition: %+v", cd))
}
}
func (cbus *CommandBus) DefineCommands(commandDefinitions ...*CommandDefinition) {
for _, h := range commandDefinitions {
cbus.DefineCommand(h)
}
}
func (cbus *CommandBus) DefineCommand(commandDefinition *CommandDefinition) {
if cbus.mqProvider == nil {
panic("Please configure MQ provider first through CommandBus.SetMqProvider")
}
commandDefinition.validate()
if _, ok := cbus.defs[commandDefinition.Name]; !ok {
cbus.createConsumer(commandDefinition.Name)
}
cbus.defs[commandDefinition.Name] = commandDefinition
}
func (cbus *CommandBus) CanUndo(commandDefinition *CommandDefinition) bool {
return cbus.undoHandlers[commandDefinition.Name] != nil
}