-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbleep.go
117 lines (101 loc) · 2.56 KB
/
bleep.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
110
111
112
113
114
115
116
117
package bleep
import (
"os"
"os/signal"
"sync"
"github.com/google/uuid"
)
var defaultBleep = New()
// Action is what will be performed on an os signal
type Action func(os.Signal)
// Bleep is the type for the handler
type Bleep struct {
actions map[string]Action
mu *sync.RWMutex
}
// New is used to create a new os signal handler
func New() *Bleep {
return &Bleep{
actions: make(map[string]Action),
mu: &sync.RWMutex{},
}
}
// Add is used to add an action to the default bleep instance to be performed on an os signal
func Add(action Action) string {
return defaultBleep.Add(action)
}
// Remove is used to remove an action from the default bleep instance
func Remove(key string) Action {
return defaultBleep.Remove(key)
}
// Reset is used to reset the default handler instance
func Reset() map[string]Action {
return defaultBleep.Reset()
}
// Actions is used to get the set of actions part of the default bleep instance
func Actions() map[string]Action {
return defaultBleep.Actions()
}
// Listen is used to listen for the provided OS signals
// If none are provided, then it will listen for any OS signal
func Listen(signals ...os.Signal) {
defaultBleep.Listen(signals...)
}
// Add is used to add an action to be performed on an os signal
func (b *Bleep) Add(action Action) string {
b.mu.Lock()
defer b.mu.Unlock()
key := uuid.New().String()
b.actions[key] = action
return key
}
// Remove is used to remove an action added previously
func (b *Bleep) Remove(key string) Action {
b.mu.Lock()
defer b.mu.Unlock()
if action, ok := b.actions[key]; ok {
delete(b.actions, key)
return action
}
return nil
}
// Reset is used to reset the current handler instance
func (b *Bleep) Reset() map[string]Action {
b.mu.Lock()
defer b.mu.Unlock()
actions := make(map[string]Action)
for k, a := range b.actions {
delete(b.actions, k)
actions[k] = a
}
return actions
}
// Actions is used to get the set of actions part of the current bleep instance
func (b *Bleep) Actions() map[string]Action {
b.mu.RLock()
defer b.mu.RUnlock()
actions := make(map[string]Action)
for k, a := range b.actions {
actions[k] = a
}
return actions
}
// Listen is used to listen for the provided OS signals
// If none are provided, then it will listen for any OS signal
func (b *Bleep) Listen(signals ...os.Signal) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, signals...)
s := <-ch
b.mu.RLock()
defer b.mu.RUnlock()
wg := sync.WaitGroup{}
for _, a := range b.actions {
wg.Add(1)
action := a
go func(s os.Signal) {
defer wg.Done()
action(s)
}(s)
}
wg.Wait()
}