-
Notifications
You must be signed in to change notification settings - Fork 6
/
group.go
62 lines (54 loc) · 1.82 KB
/
group.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
53
54
55
56
57
58
59
60
61
62
package action
import "encoding/gob"
// actionGroup groups multiple actions as a single action. To use it, call Group() and pass a list of actions.
type actionGroup struct {
Actions []ActionI
}
type GroupI interface {
GetCallbackAction() CallbackActionI
}
// RenderScript renders the group of actions as a single action.
func (g actionGroup) RenderScript(params RenderParams) (s string) {
for _, a := range g.Actions {
s += a.RenderScript(params)
}
return
}
// Group joins multiple actions into a single action.
// Any number of javascript actions can be included, but only one callback action can be included in the group.
func Group(actions ...ActionI) ActionI {
var foundCallback bool
for _, a := range actions {
switch a.(type) {
case actionGroup:
panic("You cannot put an actionGroup into another actionGroup")
case CallbackActionI:
if foundCallback {
panic("You can only associate one callback action with an event, and it must be the last action.")
}
foundCallback = true
default:
if foundCallback {
panic("You can only associate one callback action with an event, and it must be the last action.")
}
}
}
// Note, the above could be more robust and allow multiple callback actions, but it would get quite tricky if different
// kinds of actions were interleaved. We will wait until someone presents a compelling need for something like that.
return actionGroup{actions}
}
// GetCallbackAction returns the embedded callback action in the group, if one exists. Note that
// you can only have at most one callback action in a group
func (g actionGroup) GetCallbackAction() CallbackActionI {
if g.Actions == nil || len(g.Actions) == 0 {
return nil
}
a := g.Actions[len(g.Actions)-1]
if a2, ok := a.(CallbackActionI); ok {
return a2
}
return nil
}
func init() {
gob.Register(actionGroup{})
}