-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathexample_fsm.go
132 lines (111 loc) · 2.75 KB
/
example_fsm.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package fsm
import (
"context"
"fmt"
)
// ExampleService is an example service that we want to wait for in the FSM.
type ExampleService interface {
WaitForStuffHappening() (<-chan bool, error)
}
// ExampleStore is an example store that we want to use in our exitFunc.
type ExampleStore interface {
StoreStuff() error
}
// ExampleFSM implements the FSM and uses the ExampleService and ExampleStore
// to implement the actions.
type ExampleFSM struct {
*StateMachine
service ExampleService
store ExampleStore
}
// NewExampleFSMContext creates a new example FSM context.
func NewExampleFSMContext(service ExampleService,
store ExampleStore) *ExampleFSM {
exampleFSM := &ExampleFSM{
service: service,
store: store,
}
exampleFSM.StateMachine = NewStateMachine(exampleFSM.GetStates(), 10)
return exampleFSM
}
// States.
const (
InitFSM = StateType("InitFSM")
StuffSentOut = StateType("StuffSentOut")
WaitingForStuff = StateType("WaitingForStuff")
StuffFailed = StateType("StuffFailed")
StuffSuccess = StateType("StuffSuccess")
)
// Events.
var (
OnRequestStuff = EventType("OnRequestStuff")
OnStuffSentOut = EventType("OnStuffSentOut")
OnStuffSuccess = EventType("OnStuffSuccess")
)
// GetStates returns the states for the example FSM.
func (e *ExampleFSM) GetStates() States {
return States{
EmptyState: State{
Transitions: Transitions{
OnRequestStuff: InitFSM,
},
},
InitFSM: State{
Action: e.initFSM,
Transitions: Transitions{
OnStuffSentOut: StuffSentOut,
OnError: StuffFailed,
},
},
StuffSentOut: State{
Action: e.waitForStuff,
Transitions: Transitions{
OnStuffSuccess: StuffSuccess,
OnError: StuffFailed,
},
},
StuffFailed: State{
Action: NoOpAction,
},
StuffSuccess: State{
Action: NoOpAction,
},
}
}
// InitStuffRequest is the event context for the InitFSM state.
type InitStuffRequest struct {
Stuff string
respondChan chan<- string
}
// initFSM is the action for the InitFSM state.
func (e *ExampleFSM) initFSM(_ context.Context, eventCtx EventContext,
) EventType {
req, ok := eventCtx.(*InitStuffRequest)
if !ok {
return e.HandleError(
fmt.Errorf("invalid event context type: %T", eventCtx),
)
}
err := e.store.StoreStuff()
if err != nil {
return e.HandleError(err)
}
req.respondChan <- req.Stuff
return OnStuffSentOut
}
// waitForStuff is an action that waits for stuff to happen.
func (e *ExampleFSM) waitForStuff(ctx context.Context, eventCtx EventContext,
) EventType {
waitChan, err := e.service.WaitForStuffHappening()
if err != nil {
return e.HandleError(err)
}
go func() {
<-waitChan
err := e.SendEvent(ctx, OnStuffSuccess, nil)
if err != nil {
log.Errorf("unable to send event: %v", err)
}
}()
return NoOp
}