42 lines
707 B
Go
42 lines
707 B
Go
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"`
|
|
}
|
|
|