-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatcher.go
59 lines (51 loc) · 1.26 KB
/
watcher.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
package main
type Watcher struct {
messages map[string]Message
status Status
}
func NewWatcher() Watcher {
messages := make(map[string]Message)
return Watcher{
messages: messages,
status: UNKNOWN,
}
}
func (watcher *Watcher) Status() Status {
return watcher.status
}
func (watcher *Watcher) Messages() []Message {
var messages []Message
for _, message := range watcher.messages {
messages = append(messages, message)
}
return messages
}
func (watcher *Watcher) MessagesFor(displays StringSet) []Message {
var messages []Message
for display := range displays {
message, contains := watcher.messages[display]
if contains {
messages = append(messages, message)
} else {
messages = append(messages, Message{PID: 0, Display: display})
}
}
return messages
}
func (watcher *Watcher) Update(messages []Message, status Status) {
for _, message := range messages {
watcher.messages[message.Display] = message
}
watcher.status = status
}
func (watcher *Watcher) CleanUp(activeDisplays StringSet) {
var currentDisplays []string
for display := range watcher.messages {
currentDisplays = append(currentDisplays, display)
}
for _, display := range currentDisplays {
if !activeDisplays.Contains(display) {
delete(watcher.messages, display)
}
}
}