-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
48 lines (41 loc) · 1.13 KB
/
handler.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
package fsm
import (
tf "github.com/vitaliy-ukiru/telebot-filter/telefilter"
tele "gopkg.in/telebot.v3"
)
func (m *Manager) runHandler(c tele.Context, handler Handler) error {
fsmCtx, ok := m.mustGetContext(c)
// don't run handler if can't get context
if !ok || fsmCtx == nil {
return nil
}
return handler(c, fsmCtx)
}
// WrapContext is middleware for wrapping fsm context. It helps to create
// context only one time for update and make small allocation optimization.
// FSM will unwrap this context in internal mechanic.
func (m *Manager) WrapContext(next tele.HandlerFunc) tele.HandlerFunc {
return func(c tele.Context) error {
fsmCtx, ok := m.NewContext(c)
if ok {
c = newWrapperContext(c, fsmCtx)
}
return next(c)
}
}
type fsmHandler struct {
onState StateMatcher
filter tf.Filter
handler Handler
manager *Manager
}
func (fh fsmHandler) Check(c tele.Context) bool {
// skip state filter on nil
if fh.onState != nil && !fh.manager.runFilter(c, fh.onState) {
return false
}
return fh.filter == nil || fh.filter(c)
}
func (fh fsmHandler) Execute(c tele.Context) error {
return fh.manager.runHandler(c, fh.handler)
}