-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
events.go
109 lines (89 loc) · 2.76 KB
/
events.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package incus
import (
"context"
"fmt"
"sync"
"github.com/lxc/incus/v6/shared/api"
)
// The EventListener struct is used to interact with an Incus event stream.
type EventListener struct {
r *ProtocolIncus
ctx context.Context
ctxCancel context.CancelFunc
err error
// projectName stores which project this event listener is associated with (empty for all projects).
projectName string
targets []*EventTarget
targetsLock sync.Mutex
}
// The EventTarget struct is returned to the caller of AddHandler and used in RemoveHandler.
type EventTarget struct {
function func(api.Event)
types []string
}
// AddHandler adds a function to be called whenever an event is received.
func (e *EventListener) AddHandler(types []string, function func(api.Event)) (*EventTarget, error) {
if function == nil {
return nil, fmt.Errorf("A valid function must be provided")
}
// Handle locking
e.targetsLock.Lock()
defer e.targetsLock.Unlock()
// Create a new target
target := EventTarget{
function: function,
types: types,
}
// And add it to the targets
e.targets = append(e.targets, &target)
return &target, nil
}
// RemoveHandler removes a function to be called whenever an event is received.
func (e *EventListener) RemoveHandler(target *EventTarget) error {
if target == nil {
return fmt.Errorf("A valid event target must be provided")
}
// Handle locking
e.targetsLock.Lock()
defer e.targetsLock.Unlock()
// Locate and remove the function from the list
for i, entry := range e.targets {
if entry == target {
copy(e.targets[i:], e.targets[i+1:])
e.targets[len(e.targets)-1] = nil
e.targets = e.targets[:len(e.targets)-1]
return nil
}
}
return fmt.Errorf("Couldn't find this function and event types combination")
}
// Disconnect must be used once done listening for events.
func (e *EventListener) Disconnect() {
// Handle locking
e.r.eventListenersLock.Lock()
defer e.r.eventListenersLock.Unlock()
if e.ctx.Err() != nil {
return
}
// Locate and remove it from the global list
for i, listener := range e.r.eventListeners[e.projectName] {
if listener == e {
copy(e.r.eventListeners[e.projectName][i:], e.r.eventListeners[e.projectName][i+1:])
e.r.eventListeners[e.projectName][len(e.r.eventListeners[e.projectName])-1] = nil
e.r.eventListeners[e.projectName] = e.r.eventListeners[e.projectName][:len(e.r.eventListeners[e.projectName])-1]
break
}
}
// Turn off the handler
e.err = nil
e.ctxCancel()
}
// Wait blocks until the server disconnects the connection or Disconnect() is called.
func (e *EventListener) Wait() error {
<-e.ctx.Done()
return e.err
}
// IsActive returns true if this listener is still connected, false otherwise.
func (e *EventListener) IsActive() bool {
return e.ctx.Err() == nil
}