-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync_fsm.go
132 lines (118 loc) · 2.75 KB
/
async_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 mermaidlive
import (
"context"
"log"
"time"
"github.com/Arceliar/phony"
"github.com/cskr/pubsub/v2"
)
type AsyncFSM struct {
phony.Inbox
ctx context.Context
cancel context.CancelFunc
events *pubsub.PubSub[string, Event]
delay time.Duration
currentCount uint8
currentState string
}
func NewAsyncFSM(events *pubsub.PubSub[string, Event]) *AsyncFSM {
return NewCustomAsyncFSM(events, 800*time.Millisecond)
}
func NewCustomAsyncFSM(events *pubsub.PubSub[string, Event], delay time.Duration) *AsyncFSM {
return &AsyncFSM{
ctx: context.Background(),
cancel: noOp(), /*no-op*/
events: events,
delay: delay,
currentState: "waiting",
}
}
func (fsm *AsyncFSM) StartWork() {
fsm.Act(fsm, func() {
if fsm.currentCount != 0 {
fsm.events.Pub(NewEventWithReason("RequestIgnored", "cannot start: machine busy"), Topic)
return
}
fsm.ctx, fsm.cancel = context.WithCancel(context.Background())
fsm.currentState = "working"
fsm.events.Pub(NewSimpleEvent("WorkStarted"), Topic)
fsm.currentCount = 10
go fsm.tick()
})
log.Println("StartWork finished")
}
func (fsm *AsyncFSM) AbortWork() {
fsm.Act(fsm, func() {
if fsm.currentCount == 0 {
fsm.events.Pub(NewEventWithReason("RequestIgnored", "cannot abort: machine not busy"), Topic)
return
}
fsm.cancel()
fsm.currentState = "aborting"
fsm.events.Pub(NewSimpleEvent("WorkAbortRequested"), Topic)
})
log.Println("AbortWork finished")
}
func (fsm *AsyncFSM) tick() {
fsm.Act(fsm, func() {
// check if aborted
select {
case <-fsm.ctx.Done():
go func() {
time.Sleep(fsm.delay)
fsm.abort()
}()
return
default:
// not canceled yet
}
fsm.events.Pub(NewEventWithParam("Tick", fsm.currentCount), Topic)
fsm.currentCount--
if fsm.currentCount == 0 {
go func() {
time.Sleep(fsm.delay)
fsm.done()
}()
return
}
go func() {
time.Sleep(fsm.delay)
fsm.tick()
}()
})
}
func (fsm *AsyncFSM) abort() {
fsm.Act(fsm, func() {
fsm.currentState = "waiting"
fsm.currentCount = 0
fsm.events.Pub(NewSimpleEvent("WorkAborted"), Topic)
})
}
func (fsm *AsyncFSM) done() {
fsm.Act(fsm, func() {
fsm.currentState = "waiting"
fsm.currentCount = 0
fsm.events.Pub(NewSimpleEvent("WorkDone"), Topic)
})
}
// sync queries - not to be used from within actor behaviors (methods)
func (fsm *AsyncFSM) IsWaiting() bool {
return fsm.getCurrentCount() == 0
}
func (fsm *AsyncFSM) CurrentState() string {
var res string
phony.Block(fsm, func() {
res = fsm.currentState
})
return res
}
func (fsm *AsyncFSM) getCurrentCount() uint8 {
var currentCount uint8
phony.Block(fsm, func() {
currentCount = fsm.currentCount
})
return currentCount
}
func noOp() context.CancelFunc {
return func() {}
}