-
Notifications
You must be signed in to change notification settings - Fork 2
/
async_adapter.go
276 lines (238 loc) · 8.15 KB
/
async_adapter.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2023 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package yarn
import (
"errors"
"fmt"
"sync/atomic"
)
// ErrAlreadyStopped is returned when the AsyncAdapter cannot
// stop the virtual machine, because it is already stopped.
const ErrAlreadyStopped = virtualMachineError("VM already stopped or stopping")
var _ DialogueHandler = &AsyncAdapter{}
// VMState enumerates the different states that AsyncAdapter can be in.
type VMState int32
const (
// No event has been delivered (since the last call to Go / GoWithOption);
// the VM is executing.
VMStateRunning = iota
// An event other than Options was delivered, and VM execution is blocked.
VMStatePaused
// Options event was delivered, and VM execution is blocked.
VMStatePausedOptions
// Execution has not begun, or has ended (e.g. by calling Abort, or any
// other error).
VMStateStopped
)
func (s VMState) String() string {
switch s {
case VMStateRunning:
return "Running"
case VMStatePaused:
return "Paused"
case VMStatePausedOptions:
return "PausedOptions"
case VMStateStopped:
return "Stopped"
}
return fmt.Sprintf("(invalid VMState %d)", s)
}
// VMStateMismatchErr is returned when AsyncAdapter is told to do something
// (either by the user calling Go, GoWithChoice, or Abort, or the VM calling a
// DialogueHandler method) but this requires AsyncAdapter to be in a different
// state than the state it is in.
type VMStateMismatchErr struct {
// The VM was in state Got, but we wanted it to be in state Want in order
// to change it to state Next.
Got, Want, Next VMState
}
func (e VMStateMismatchErr) Error() string {
return fmt.Sprintf("VM is %v, so cannot transition from %v to %v", e.Got, e.Want, e.Next)
}
// AsyncAdapter is a DialogueHandler that exposes an interface that is similar
// to the mainline YarnSpinner VM dialogue handler. Instead of manually blocking
// inside the DialogueHandler callbacks, AsyncAdapter does this for you, until
// you call Go, GoWithChoice, or Abort (as appropriate).
type AsyncAdapter struct {
state atomic.Int32
handler AsyncDialogueHandler
msgCh chan asyncMsg
}
// NewAsyncAdapter returns a new AsyncAdapter.
func NewAsyncAdapter(h AsyncDialogueHandler) *AsyncAdapter {
return &AsyncAdapter{
handler: h,
// The user might call Go from within their handler's Line method
// (or however many other ways to try to continue the VM immediately).
// If msgCh was unbuffered, calling Go would wait forever trying to send
// on the channel, because AsyncAdapter only receives on msgCh after
// their method returns.
msgCh: make(chan asyncMsg, 1),
}
}
// State returns the current state.
func (a *AsyncAdapter) State() VMState {
return VMState(a.state.Load())
}
func (a *AsyncAdapter) stateTransition(old, new int32) error {
if !a.state.CompareAndSwap(old, new) {
// This races (between CAS and a.State, something else could switch the
// state around). While I try to make the error maximally useful for
// debugging ... YOLO?
return VMStateMismatchErr{
Got: a.State(),
Want: VMState(old),
Next: VMState(new),
}
}
return nil
}
// Go will continue the VM after it has delivered any event (other than
// Options). If the VM is not paused following any event other than Options, an
// error will be returned.
func (a *AsyncAdapter) Go() error {
if err := a.stateTransition(VMStatePaused, VMStateRunning); err != nil {
return err
}
a.msgCh <- goMsg{}
return nil
}
// GoWithChoice will continue the VM after it has delivered an Options event.
// Pass the ID of the chosen option. If the VM is not paused following an
// Options event, an error will be returned.
func (a *AsyncAdapter) GoWithChoice(id int) error {
if err := a.stateTransition(VMStatePausedOptions, VMStateRunning); err != nil {
return err
}
a.msgCh <- choiceMsg{id}
return nil
}
// Abort stops the VM with the given error as soon as possible (either within
// the current event, or on the next event). If a nil error is passed, Abort
// will replace it with Stop (so that NodeComplete and DialogueComplete still
// fire). If the VM is already stopped (either through Abort, or after the
// DialogueComplete event) an error will be returned.
func (a *AsyncAdapter) Abort(err error) error {
if old := a.state.Swap(VMStateStopped); old == VMStateStopped {
return ErrAlreadyStopped
}
if err == nil {
err = Stop
}
a.msgCh <- abortMsg{err}
return nil
}
// waitForGo waits for Go or Abort to be called.
func (a *AsyncAdapter) waitForGo() error {
switch msg := (<-a.msgCh).(type) {
case goMsg:
return nil
case choiceMsg:
// This is incredibly unlikely, but I check it anyway.
return errors.New("AsyncAdapter.GoWithChoice called, but last event was not Options")
case abortMsg:
return msg.err
default:
return fmt.Errorf("invalid message type %T received", msg)
}
}
// waitForChoice waits for GoWithChoice or Abort to be called.
func (a *AsyncAdapter) waitForChoice() (int, error) {
switch msg := (<-a.msgCh).(type) {
case goMsg:
// This is incredibly unlikely, but I check it anyway.
return -1, errors.New("AsyncAdapter.Go called, but last event was Options")
case choiceMsg:
return msg.choice, nil
case abortMsg:
return -1, msg.err
default:
return -1, fmt.Errorf("invalid message type %T received", msg)
}
}
// --- DialogueHandler implementation --- \\
// NodeStart is called by the VM and blocks until Go or Abort is called.
func (a *AsyncAdapter) NodeStart(nodeName string) error {
if err := a.stateTransition(VMStateRunning, VMStatePaused); err != nil {
return err
}
a.handler.NodeStart(nodeName)
return a.waitForGo()
}
// PrepareForLines is called by the VM and blocks until Go or Abort is called.
func (a *AsyncAdapter) PrepareForLines(lineIDs []string) error {
if err := a.stateTransition(VMStateRunning, VMStatePaused); err != nil {
return err
}
a.handler.PrepareForLines(lineIDs)
return a.waitForGo()
}
// Line is called by the VM and blocks until Go or Abort is called.
func (a *AsyncAdapter) Line(line Line) error {
if err := a.stateTransition(VMStateRunning, VMStatePaused); err != nil {
return err
}
a.handler.Line(line)
return a.waitForGo()
}
// Options is called by the VM and blocks until GoWithChoice or Abort is called.
func (a *AsyncAdapter) Options(options []Option) (int, error) {
if err := a.stateTransition(VMStateRunning, VMStatePausedOptions); err != nil {
return -1, err
}
a.handler.Options(options)
return a.waitForChoice()
}
// Command is called by the VM and blocks until Go or Abort is called.
func (a *AsyncAdapter) Command(command string) error {
if err := a.stateTransition(VMStateRunning, VMStatePaused); err != nil {
return err
}
a.handler.Command(command)
return a.waitForGo()
}
// NodeComplete is called by the VM and blocks until Go or Abort is called.
func (a *AsyncAdapter) NodeComplete(nodeName string) error {
if err := a.stateTransition(VMStateRunning, VMStatePaused); err != nil {
return err
}
a.handler.NodeComplete(nodeName)
return a.waitForGo()
}
// DialogueComplete is called by the VM and blocks until Go or Abort is called.
func (a *AsyncAdapter) DialogueComplete() error {
if err := a.stateTransition(VMStateRunning, VMStatePaused); err != nil {
return err
}
a.handler.DialogueComplete()
return a.waitForGo()
}
// --- AsyncAdapter messages --- \\
// AsyncAdapter works by waiting on a channel. The three message types are below.
type asyncMsg interface {
asyncMsgTag()
}
// Sent on the channel when Go is called.
type goMsg struct{}
func (goMsg) asyncMsgTag() {}
// Sent on the channel when GoWithChoice is called.
type choiceMsg struct {
choice int
}
func (choiceMsg) asyncMsgTag() {}
// Sent on the channel when Abort is called.
type abortMsg struct {
err error
}
func (abortMsg) asyncMsgTag() {}