2019-07-14 14:44:00 -06:00
|
|
|
package streamdeck
|
|
|
|
|
2019-07-23 04:14:48 -06:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-08-08 15:34:37 -06:00
|
|
|
sdcontext "code.encyclopediaofdaniel.com/dlprows/streamdeck-sdk/context"
|
2019-07-23 04:14:48 -06:00
|
|
|
)
|
|
|
|
|
2019-07-14 14:44:00 -06:00
|
|
|
type Action struct {
|
|
|
|
uuid string
|
|
|
|
handlers map[string][]EventHandler
|
2019-07-23 04:14:48 -06:00
|
|
|
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
|
2019-07-14 14:44:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (action *Action) RegisterHandler(eventName string, handler EventHandler) {
|
|
|
|
action.handlers[eventName] = append(action.handlers[eventName], handler)
|
|
|
|
}
|
2019-07-23 04:14:48 -06:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|