-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelegates.go
More file actions
60 lines (50 loc) · 1.78 KB
/
Copy pathdelegates.go
File metadata and controls
60 lines (50 loc) · 1.78 KB
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
53
54
55
56
57
58
59
60
package fsm
// ActionMuxDelegate allows to register a set of delegates per action.
type ActionMuxDelegate struct {
delegates map[string]Delegate
}
// NewActionMuxDelegate returns a new ActionMuxDelegate.
func NewActionMuxDelegate(delegates map[string]Delegate) *ActionMuxDelegate {
return &ActionMuxDelegate{delegates}
}
// Handle calls the underlying delegate for an action if any.
func (d *ActionMuxDelegate) Handle(action string, fromState string, toState string, args []interface{}) error {
if delegate, ok := d.delegates[action]; ok {
return delegate.Handle(action, fromState, toState, args)
}
return nil
}
func (d *ActionMuxDelegate) SetStateMachine(sm *StateMachine) {
for _, delegate := range d.delegates {
if smaDelegate, ok := delegate.(StateMachineAwareDelegate); ok {
smaDelegate.SetStateMachine(sm)
}
}
}
// CompositeDelegate allows to multiplex the single delegate in the state machine.
type CompositeDelegate struct {
delegates []Delegate
}
// NewCompositeDelegate returns a new CompositeDelegate.
func NewCompositeDelegate(delegates []Delegate) *CompositeDelegate {
return &CompositeDelegate{delegates}
}
// Handle calls the underlying delegates.
func (d *CompositeDelegate) Handle(action string, fromState string, toState string, args []interface{}) error {
for _, delegate := range d.delegates {
// TODO: consider collecting and returning errors
err := delegate.Handle(action, fromState, toState, args)
if err == StopPropagation {
// Error must be returned so that embedded composite delegates pass up the signal
return err
}
}
return nil
}
func (d *CompositeDelegate) SetStateMachine(sm *StateMachine) {
for _, delegate := range d.delegates {
if smaDelegate, ok := delegate.(StateMachineAwareDelegate); ok {
smaDelegate.SetStateMachine(sm)
}
}
}