-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.go
52 lines (44 loc) · 841 Bytes
/
event.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
package evo
import (
"os"
"os/signal"
"syscall"
)
type event struct {
events map[string][]func()
}
var Events = event{}
func (e *event) On(event string, callback func()) {
if _, ok := e.events[event]; ok {
e.events[event] = append(e.events[event], callback)
} else {
e.events[event] = []func(){callback}
}
}
func (e *event) Trigger(event string) {
if callbacks, ok := e.events[event]; ok {
for _, callback := range callbacks {
callback()
}
}
}
func (e *event) List() []string {
var keys []string
for key, _ := range e.events {
keys = append(keys, key)
}
return keys
}
func (e *event) Register() {
if len(e.events) > 0 {
return
}
e.events = map[string][]func(){}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
Events.Trigger("exit")
os.Exit(1)
}()
}