forked from eternal-flame-AD/gotify-broadcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.go
39 lines (33 loc) · 783 Bytes
/
exchange.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
package main
import (
"sync"
"github.com/eternal-flame-AD/gotify-broadcast/model"
)
var msgExchanger = newMessageExchange()
type messageExchange struct {
MsgChan chan<- model.Message
callbacks []func(model.Message)
mutex sync.RWMutex
}
func (c *messageExchange) OnMessage(cb func(model.Message)) {
c.callbacks = append(c.callbacks, cb)
}
func newMessageExchange() *messageExchange {
messageExchanger := new(messageExchange)
msgChan := make(chan model.Message)
messageExchanger.MsgChan = msgChan
go func() {
for {
msg := <-msgChan
msg.IsSend = false
func() {
messageExchanger.mutex.RLock()
defer messageExchanger.mutex.RUnlock()
for _, cb := range messageExchanger.callbacks {
cb(msg)
}
}()
}
}()
return messageExchanger
}