-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext.go
More file actions
54 lines (45 loc) · 922 Bytes
/
context.go
File metadata and controls
54 lines (45 loc) · 922 Bytes
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
package nsxbot
import (
"context"
"log/slog"
"math"
"github.com/nsxdevx/nsxbot/driver"
"github.com/nsxdevx/nsxbot/event"
"github.com/nsxdevx/nsxbot/nlog"
)
const abortIndex int8 = math.MaxInt8 >> 1
type Context[T any] struct {
context.Context
driver.Emitter
event.Replyer
Time int64
SelfId int64
Msg T
Log *slog.Logger
index int8
handlers HandlersChain[T]
}
func NewContext[T any](ctx context.Context, emitter driver.Emitter, selfId int64, time int64, data T, Replyer event.Replyer) Context[T] {
return Context[T]{
Context: ctx,
Emitter: emitter,
SelfId: selfId,
Time: time,
Msg: data,
Log: nlog.Logger(),
Replyer: Replyer,
index: -1,
}
}
func (c *Context[T]) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
if c.handlers[c.index] != nil {
c.handlers[c.index](c)
}
c.index++
}
}
func (c *Context[T]) Abort() {
c.index = abortIndex
}