-
Notifications
You must be signed in to change notification settings - Fork 7
/
action.go
61 lines (48 loc) · 1.4 KB
/
action.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
package streamdeck
import (
"context"
sdcontext "github.com/samwho/streamdeck/context"
)
type Action struct {
uuid string
handlers map[string][]EventHandler
contexts map[string]context.Context
}
func newAction(uuid string) *Action {
action := &Action{
uuid: uuid,
handlers: make(map[string][]EventHandler),
contexts: make(map[string]context.Context),
}
action.RegisterHandler(WillAppear, func(ctx context.Context, client *Client, event Event) error {
action.addContext(ctx)
return nil
})
action.RegisterHandler(WillDisappear, func(ctx context.Context, client *Client, event Event) error {
action.removeContext(ctx)
return nil
})
return action
}
func (action *Action) RegisterHandler(eventName string, handler EventHandler) {
action.handlers[eventName] = append(action.handlers[eventName], handler)
}
func (action *Action) Contexts() []context.Context {
cs := make([]context.Context, len(action.contexts))
for _, c := range action.contexts {
cs = append(cs, c)
}
return cs
}
func (action *Action) addContext(ctx context.Context) {
if sdcontext.Context(ctx) == "" {
panic("passed non-streamdeck context to addContext")
}
action.contexts[sdcontext.Context(ctx)] = ctx
}
func (action *Action) removeContext(ctx context.Context) {
if sdcontext.Context(ctx) == "" {
panic("passed non-streamdeck context to addContext")
}
delete(action.contexts, sdcontext.Context(ctx))
}