Compare commits
2 Commits
a2e74105c5
...
b13cd05b45
Author | SHA1 | Date | |
---|---|---|---|
b13cd05b45 | |||
2cd46585a8 |
@ -82,3 +82,7 @@ func (s State) LogError(err error) {
|
|||||||
func (s State) Move(mc fsm.MachineControls[Wizard], stateID fsm.StateID, pl *Wizard) {
|
func (s State) Move(mc fsm.MachineControls[Wizard], stateID fsm.StateID, pl *Wizard) {
|
||||||
s.LogError(mc.Move(stateID, pl))
|
s.LogError(mc.Move(stateID, pl))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s State) MoveForHandle(mc fsm.MachineControls[Wizard], stateID fsm.StateID, pl *Wizard) {
|
||||||
|
s.LogError(mc.MoveForHandle(stateID, pl))
|
||||||
|
}
|
||||||
|
@ -24,12 +24,12 @@ func (s *WaitingForMemberWebhookState) Enter(pl *Wizard, mc fsm.MachineControls[
|
|||||||
}
|
}
|
||||||
if !cm.OldChatMember.MemberIsMember() && cm.OldChatMember.MemberUser().ID == s.App.TGProfile().ID &&
|
if !cm.OldChatMember.MemberIsMember() && cm.OldChatMember.MemberUser().ID == s.App.TGProfile().ID &&
|
||||||
cm.NewChatMember.MemberIsMember() && cm.NewChatMember.MemberUser().ID == s.App.TGProfile().ID {
|
cm.NewChatMember.MemberIsMember() && cm.NewChatMember.MemberUser().ID == s.App.TGProfile().ID {
|
||||||
s.Move(mc, AddChatMemberStateID, pl)
|
s.MoveForHandle(mc, AddChatMemberStateID, pl)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if cm.OldChatMember.MemberIsMember() && cm.OldChatMember.MemberUser().ID == s.App.TGProfile().ID &&
|
if cm.OldChatMember.MemberIsMember() && cm.OldChatMember.MemberUser().ID == s.App.TGProfile().ID &&
|
||||||
!cm.NewChatMember.MemberIsMember() && cm.NewChatMember.MemberUser().ID == s.App.TGProfile().ID {
|
!cm.NewChatMember.MemberIsMember() && cm.NewChatMember.MemberUser().ID == s.App.TGProfile().ID {
|
||||||
s.Move(mc, RemoveChatMemberStateID, pl)
|
s.MoveForHandle(mc, RemoveChatMemberStateID, pl)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ type Machine[T any] struct {
|
|||||||
initialState StateID
|
initialState StateID
|
||||||
states *types.Map[StateID, IState[T]]
|
states *types.Map[StateID, IState[T]]
|
||||||
errHandler ErrorState[T]
|
errHandler ErrorState[T]
|
||||||
|
handleNow bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New machine.
|
// New machine.
|
||||||
@ -89,6 +90,11 @@ func (m *Machine[T]) Move(id StateID, payload *T) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Machine[T]) MoveForHandle(id StateID, payload *T) error {
|
||||||
|
m.handleNow = true
|
||||||
|
return m.Move(id, payload)
|
||||||
|
}
|
||||||
|
|
||||||
// Handle the input.
|
// Handle the input.
|
||||||
func (m *Machine[T]) Handle(provider MachineStateProvider[T]) error {
|
func (m *Machine[T]) Handle(provider MachineStateProvider[T]) error {
|
||||||
if provider != nil {
|
if provider != nil {
|
||||||
@ -101,8 +107,14 @@ func (m *Machine[T]) Handle(provider MachineStateProvider[T]) error {
|
|||||||
if st == nil || err != nil {
|
if st == nil || err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
st.Handle(m.payload, m)
|
for {
|
||||||
return nil
|
st.Handle(m.payload, m)
|
||||||
|
if m.handleNow { // MoveForHandle was called, trying to handle again.
|
||||||
|
m.handleNow = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// State of the Machine.
|
// State of the Machine.
|
||||||
|
@ -10,6 +10,8 @@ const NilStateID = StateID("")
|
|||||||
// It may fail with an error which should be handled by the IState implementation.
|
// It may fail with an error which should be handled by the IState implementation.
|
||||||
type MachineControls[T any] interface {
|
type MachineControls[T any] interface {
|
||||||
Move(StateID, *T) error
|
Move(StateID, *T) error
|
||||||
|
// MoveForHandle is the same as Move but it also triggers Handle immediately for the next state.
|
||||||
|
MoveForHandle(StateID, *T) error
|
||||||
Reset()
|
Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user