notifier/notifier.go

45 lines
769 B
Go
Raw Normal View History

2018-07-22 15:43:40 +02:00
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,
2018-07-22 19:51:30 +02:00
Type: type_,
Data: data,
2018-07-22 15:43:40 +02:00
}
}
2018-07-22 19:51:30 +02:00
func (n *Notifier) Close() {
close(n.channel)
}
2018-07-22 15:43:40 +02:00
type Notification struct {
Collection string `json:"collection"`
Type string `json:"type"`
Data []byte `json:"data,omitempty"`
}