notifier.go 769 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package notifier
  2. import (
  3. "gopkg.in/olahol/melody.v1"
  4. )
  5. type Notifier struct {
  6. channel chan *Notification
  7. Melody *melody.Melody
  8. }
  9. func NewNotifier(m *melody.Melody) *Notifier {
  10. n := new(Notifier)
  11. n.channel = make(chan *Notification)
  12. n.Melody = m
  13. return n
  14. }
  15. func (n *Notifier) Listen(then func(notification *Notification)) {
  16. for {
  17. select {
  18. case noti := <-n.channel:
  19. then(noti)
  20. }
  21. }
  22. }
  23. func (n *Notifier) Notify(collection, type_ string, data []byte) {
  24. n.channel <- &Notification{
  25. Collection: collection,
  26. Type: type_,
  27. Data: data,
  28. }
  29. }
  30. func (n *Notifier) Close() {
  31. close(n.channel)
  32. }
  33. type Notification struct {
  34. Collection string `json:"collection"`
  35. Type string `json:"type"`
  36. Data []byte `json:"data,omitempty"`
  37. }