-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify.go
105 lines (83 loc) · 2.48 KB
/
notify.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
package gofig
import "context"
// A Notifier is a Parser that notifies via a channel if changes to configuration have occurred.
// Remember to check the error on the channel.
type Notifier interface {
Notify() <-chan error
Close() error
}
// A FileNotifier is a Notifier that also returns the path to the file being watched.
type FileNotifier interface {
Notifier
Path() string
}
// A NotifyParser is a Notifier that also Parses configuration.
type NotifyParser interface {
Parser
Notifier
}
// Notify notifies when a change to configuration has occurred.
func (l *Loader) Notify(c chan<- error, notifiers ...NotifyParser) {
l.NotifyWithContext(context.Background(), c, notifiers...)
}
// NotifyWithContext notifies when a change to configuration has occurred.
func (l *Loader) NotifyWithContext(ctx context.Context, c chan<- error, notifiers ...NotifyParser) {
l.notifiers = append(l.notifiers, notifiers...)
l.wg.Add(len(notifiers))
for _, n := range notifiers {
go func(n NotifyParser) {
defer l.wg.Done()
ch := n.Notify()
for {
select {
case <-ctx.Done():
return
case err, ok := <-ch:
if !ok {
return // Channel is closed
}
if err == nil {
err = l.Parse(n)
}
c <- err
}
}
}(n)
}
}
// Close stops listening for notification events. This only needs to be called if Notify or
// NotifyWithContext are being used.
func (l *Loader) Close() error {
var err CloseError
for _, n := range l.notifiers {
if e := n.Close(); e != nil {
err.Add(e)
}
}
l.wg.Wait()
return err.NilOrError()
}
// FileNotifyParser parses and watches for notifications from a notifier.
type FileNotifyParser struct {
*FileParser
notifier FileNotifier
}
// NewFileNotifyParser constructs a new FileNotifyParser.
func NewFileNotifyParser(parser ParseReadCloser, notifier FileNotifier) *FileNotifyParser {
return &FileNotifyParser{
FileParser: NewFileParser(parser, notifier.Path()),
notifier: notifier,
}
}
// Notify calls the wrapped function returning the values from the returned Notifier Notify method.
func (p *FileNotifyParser) Notify() <-chan error {
return p.notifier.Notify()
}
// Close calls the wrapped function returning the values from the returned Notifier Close method.
func (p *FileNotifyParser) Close() error {
return p.notifier.Close()
}
// FromFileAndNotify reads a file anf notifies of changes.
func FromFileAndNotify(parser ParseReadCloser, notifier FileNotifier) NotifyParser {
return NewFileNotifyParser(parser, notifier)
}