fix: state clear
This commit is contained in:
1 parent
ec12f29fdb
commit
cb1ed91e05
2 files changed
+10
-8
No files matched your search
@@ -176,17 +176,15 @@ func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
st = c.State()
|
st = c.State()
|
||||||
if st.State() == state.ClearState {
|
if st.IsEmpty() {
|
||||||
if err := d.fsmStorage.Delete(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID}); err != nil {
|
if err := d.fsmStorage.Delete(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID}); err != nil {
|
||||||
log.Error().Err(err).Send()
|
log.Error().Err(err).Send()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if st.State() != "" {
|
|
||||||
if err := d.fsmStorage.Put(ctx, st.Snapshot()); err != nil {
|
if err := d.fsmStorage.Put(ctx, st.Snapshot()); err != nil {
|
||||||
log.Error().Err(err).Send()
|
log.Error().Err(err).Send()
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -11,8 +11,6 @@ var (
|
|||||||
ErrKeyNotFound = fmt.Errorf("state: key not found")
|
ErrKeyNotFound = fmt.Errorf("state: key not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
const ClearState State = "clear"
|
|
||||||
|
|
||||||
type State string
|
type State string
|
||||||
|
|
||||||
type Codec interface {
|
type Codec interface {
|
||||||
@@ -41,6 +39,7 @@ type Context struct {
|
|||||||
state State
|
state State
|
||||||
data map[string][]byte
|
data map[string][]byte
|
||||||
codec Codec
|
codec Codec
|
||||||
|
clear bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Context)
|
type Option func(*Context)
|
||||||
@@ -83,10 +82,15 @@ func (sc *Context) Set(state State) {
|
|||||||
func (sc *Context) State() State { return sc.state }
|
func (sc *Context) State() State { return sc.state }
|
||||||
|
|
||||||
func (sc *Context) Clear() {
|
func (sc *Context) Clear() {
|
||||||
sc.state = ClearState
|
sc.state = ""
|
||||||
|
sc.clear = true
|
||||||
sc.data = make(map[string][]byte)
|
sc.data = make(map[string][]byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sc *Context) IsEmpty() bool {
|
||||||
|
return sc.state == "" && sc.clear
|
||||||
|
}
|
||||||
|
|
||||||
func (sc *Context) Store(key string, val any) error {
|
func (sc *Context) Store(key string, val any) error {
|
||||||
b, err := sc.codec.Marshal(val)
|
b, err := sc.codec.Marshal(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user