-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
40 lines (34 loc) · 809 Bytes
/
notifier.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
package watcher
import (
"reflect"
"time"
)
type Notifier <-chan interface{}
// NewNotifier takes any readable channel type (chan or <-chan but not chan<-) and
// exposes it as a Notifier
func NewNotifier(ch interface{}) Notifier {
return Notifier(wrapChannel(ch))
}
func NewTickNotifier(interval time.Duration) Notifier {
t := time.NewTicker(interval)
return NewNotifier(t.C)
}
func wrapChannel(ch interface{}) <-chan interface{} {
t := reflect.TypeOf(ch)
if t.Kind() != reflect.Chan || t.ChanDir()&reflect.RecvDir == 0 {
panic("channels: input to Wrap must be readable channel")
}
realChan := make(chan interface{})
go func() {
v := reflect.ValueOf(ch)
for {
x, ok := v.Recv()
if !ok {
close(realChan)
return
}
realChan <- x.Interface()
}
}()
return realChan
}