-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
72 lines (61 loc) · 1.45 KB
/
context.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"NothinBot/EasyBot"
"context"
"time"
log "github.com/sirupsen/logrus"
)
type botContext struct {
timer *time.Timer
callbackStart func()
callbackReach func(msg *EasyBot.CQMessage) (isDone bool)
callbackEnd func()
}
var (
msgChans map[int64]chan *EasyBot.CQMessage
)
func newContext(timeout time.Duration) *botContext {
timer := time.NewTimer(timeout)
return &botContext{
timer: timer,
}
}
// 创建上下文监听, reach 时返回 true 则结束, 或者 WithCancel 后自行调用 CancelFunc
func newMsgContext(ctx context.Context, callbackStart func(), callbackReach func(msg *EasyBot.CQMessage) (isDone bool), callbackEnd func()) {
now := time.Now().Unix() // 时间戳, 作为ID
msgChan := make(chan *EasyBot.CQMessage) // 接收消息用
msgChans[now] = msgChan
go func() {
defer func() {
close(msgChan)
msgChans[now] = nil
}()
log.Debug("[context] 创建了一条新的上下文: ", now)
if callbackStart != nil {
callbackStart()
}
for {
select {
case msg := <-msgChan:
if callbackReach != nil {
if isDone := callbackReach(msg); isDone {
return
}
}
case <-ctx.Done():
log.Debug("[context] 上下文结束返回")
if callbackEnd != nil {
callbackEnd()
}
return
}
}
}()
<-ctx.Done()
}
// 将消息放入管道待取
func checkContextPutIn(ctx *EasyBot.CQMessage) {
for _, msgChan := range msgChans {
msgChan <- ctx
}
}