forked from qmuntal/stateless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
121 lines (102 loc) · 3.19 KB
/
example_test.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
package stateless_test
import (
"context"
"fmt"
"reflect"
"github.com/kkettinger/stateless"
)
const (
triggerCallDialed = "CallDialed"
triggerCallConnected = "CallConnected"
triggerLeftMessage = "LeftMessage"
triggerPlacedOnHold = "PlacedOnHold"
triggerTakenOffHold = "TakenOffHold"
triggerPhoneHurledAgainstWall = "PhoneHurledAgainstWall"
triggerMuteMicrophone = "MuteMicrophone"
triggerUnmuteMicrophone = "UnmuteMicrophone"
triggerSetVolume = "SetVolume"
)
const (
stateOffHook = "OffHook"
stateRinging = "Ringing"
stateConnected = "Connected"
stateOnHold = "OnHold"
statePhoneDestroyed = "PhoneDestroyed"
)
func Example() {
phoneCall := stateless.NewStateMachine(stateOffHook)
phoneCall.SetTriggerParameters(triggerSetVolume, reflect.TypeOf(0))
phoneCall.SetTriggerParameters(triggerCallDialed, reflect.TypeOf(""))
phoneCall.Configure(stateOffHook).
Permit(triggerCallDialed, stateRinging)
phoneCall.Configure(stateRinging).
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
onDialed(args[0].(string))
return nil
}).
Permit(triggerCallConnected, stateConnected)
phoneCall.Configure(stateConnected).
OnEntry(startCallTimer).
OnExit(func(_ context.Context, _ ...interface{}) error {
stopCallTimer()
return nil
}).
InternalTransition(triggerMuteMicrophone, func(_ context.Context, _ ...interface{}) error {
onMute()
return nil
}).
InternalTransition(triggerUnmuteMicrophone, func(_ context.Context, _ ...interface{}) error {
onUnmute()
return nil
}).
InternalTransition(triggerSetVolume, func(_ context.Context, args ...interface{}) error {
onSetVolume(args[0].(int))
return nil
}).
Permit(triggerLeftMessage, stateOffHook).
Permit(triggerPlacedOnHold, stateOnHold)
phoneCall.Configure(stateOnHold).
SubstateOf(stateConnected).
Permit(triggerTakenOffHold, stateConnected).
Permit(triggerPhoneHurledAgainstWall, statePhoneDestroyed)
phoneCall.ToGraph()
phoneCall.Fire(triggerCallDialed, "qmuntal")
phoneCall.Fire(triggerCallConnected)
phoneCall.Fire(triggerSetVolume, 2)
phoneCall.Fire(triggerPlacedOnHold)
phoneCall.Fire(triggerMuteMicrophone)
phoneCall.Fire(triggerUnmuteMicrophone)
phoneCall.Fire(triggerTakenOffHold)
phoneCall.Fire(triggerSetVolume, 11)
phoneCall.Fire(triggerPlacedOnHold)
phoneCall.Fire(triggerPhoneHurledAgainstWall)
fmt.Printf("State is %v\n", phoneCall.MustState())
// Output:
// [Phone Call] placed for : [qmuntal]
// [Timer:] Call started at 11:00am
// Volume set to 2!
// Microphone muted!
// Microphone unmuted!
// Volume set to 11!
// [Timer:] Call ended at 11:30am
// State is PhoneDestroyed
}
func onSetVolume(volume int) {
fmt.Printf("Volume set to %d!\n", volume)
}
func onUnmute() {
fmt.Println("Microphone unmuted!")
}
func onMute() {
fmt.Println("Microphone muted!")
}
func onDialed(callee string) {
fmt.Printf("[Phone Call] placed for : [%s]\n", callee)
}
func startCallTimer(_ context.Context, _ ...interface{}) error {
fmt.Println("[Timer:] Call started at 11:00am")
return nil
}
func stopCallTimer() {
fmt.Println("[Timer:] Call ended at 11:30am")
}