-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatmsgmap.go
55 lines (44 loc) · 1 KB
/
chatmsgmap.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package chatbot
import (
"sync"
chatbotpb "github.com/zhs007/chatbot/chatbotpb"
)
// FuncOnChatMsgs - function
type FuncOnChatMsgs func(lst []*chatbotpb.ChatMsg)
// chatMsgList - ChatMsg List
type chatMsgList struct {
lst []*chatbotpb.ChatMsg
lock sync.Mutex
}
// chatMsgMap - ChatMsg Map
type chatMsgMap struct {
mapMsgs map[string]*chatMsgList
}
func newChatMsgMap() *chatMsgMap {
return &chatMsgMap{
mapMsgs: make(map[string]*chatMsgList),
}
}
// addChatMsgs - add chat msgs
func (msgmap *chatMsgMap) addChatMsgs(token string, lst []*chatbotpb.ChatMsg) {
msgs, isok := msgmap.mapMsgs[token]
if !isok {
msgs = &chatMsgList{}
msgmap.mapMsgs[token] = msgs
}
msgs.lock.Lock()
msgs.lst = append(msgs.lst, lst...)
msgs.lock.Unlock()
}
// addChatMap - push list
func (msgmap *chatMsgMap) getChatMsgs(token string, onChatMsgs FuncOnChatMsgs) {
msgs, isok := msgmap.mapMsgs[token]
if !isok {
onChatMsgs(nil)
return
}
msgs.lock.Lock()
onChatMsgs(msgs.lst)
msgs.lst = nil
msgs.lock.Unlock()
}