-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegating.go
49 lines (41 loc) · 1.28 KB
/
delegating.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
package decs
type Delegate interface {
To(handlers ...string)
}
type delegate struct {
ebus *eventBus
delegations map[string][]func(cmd Command, delegate Delegate, notifier ResultNotifier)
undoDelegations map[string][]func(cmd Command, delegate Delegate)
undo bool
currCmd *command
}
func newDelegate(eventBus *eventBus) *delegate {
return &delegate{
ebus: eventBus,
delegations: make(map[string][]func(cmd Command, delegate Delegate, notifier ResultNotifier)),
undoDelegations: make(map[string][]func(cmd Command, delegate Delegate)),
}
}
func (d *delegate) To(handlers ...string) {
if d.undo {
var undoDelegations []func(cmd Command, delegate Delegate)
for _, handler := range handlers {
for _, undoDelegation := range d.undoDelegations[handler] {
undoDelegations = append(undoDelegations, undoDelegation)
}
}
for _, undoDelegation := range undoDelegations {
undoDelegation(d.currCmd, d)
}
return
}
var delegations []func(cmd Command, delegate Delegate, notifier ResultNotifier)
for _, handler := range handlers {
for _, delegation := range d.delegations[handler] {
delegations = append(delegations, delegation)
}
}
for _, delegation := range delegations {
delegation(d.currCmd, d, d.ebus)
}
}