This commit is contained in:
2018-07-22 15:43:40 +02:00
commit 53e0f4374d
3 changed files with 169 additions and 0 deletions

41
notifier.go Normal file
View File

@@ -0,0 +1,41 @@
package notifier
import (
"gopkg.in/olahol/melody.v1"
)
type Notifier struct {
channel chan *Notification
Melody *melody.Melody
}
func NewNotifier(m *melody.Melody) *Notifier {
n := new(Notifier)
n.channel = make(chan *Notification)
n.Melody = m
return n
}
func (n *Notifier) Listen(then func(notification *Notification)) {
for {
select {
case noti := <-n.channel:
then(noti)
}
}
}
func (n *Notifier) Notify(collection, type_ string, data []byte) {
n.channel <- &Notification{
Collection: collection,
Type: type_,
Data:data,
}
}
type Notification struct {
Collection string `json:"collection"`
Type string `json:"type"`
Data []byte `json:"data,omitempty"`
}