forked from ggcrunchy/solar2d-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_ops.lua
250 lines (213 loc) · 8.1 KB
/
flow_ops.lua
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
--- This module defines some control-flow operations for use inside coroutines.
--
-- **TODO**: Signals terminology needs revision, also too heavyweight
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
-- Standard library imports --
local assert = assert
-- Modules --
local array_ops = require("array_ops")
local flow_bodies = require("flow_bodies")
-- Imports --
local Body = flow_bodies.Body
local Body_Timed = flow_bodies.Body_Timed
-- Exports --
local M = {}
do
-- Wait config --
local Config = { use_time = true }
-- Wait helper
local function AuxWait (t, duration)
return t.time + t.lapse >= duration, duration - t.time
end
--- Waits for some time to pass.
--
-- Built on top of @{flow_bodies.Body_Timed}.
-- @number duration Time to wait.
-- @callable update Optional update logic, called as
-- update(time_state, duration, arg),
-- with _time\_state_ as per @{flow_bodies.Body_Timed}.
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn boolean The wait completed?
function M.Wait (duration, update, arg, yvalue)
Config.yvalue = yvalue
return Body_Timed(update, AuxWait, Config, duration, arg)
end
end
do
-- WaitForMultipleSignals* config --
local Config = {}
-- Signal predicates --
local Predicates = {
-- All signals set --
all = array_ops.All,
-- Any signals set --
any = array_ops.Any,
-- Any signals not set --
any_not = array_ops.All,
-- No signal set --
none = array_ops.Any
}
-- Config setup helper
local function Setup (config, pred)
config.negate_done = pred == "any_not" or pred == "none"
return assert(Predicates[pred], "Invalid predicate")
end
--- Waits for a group of signals to reach a certain state.
--
-- Built on top of @{flow_bodies.Body}.
-- @param signals Callable or read-indexable signal object. For _i_ = 1 to _count_,
-- the corresponding test is performed: `signals(i)` or `signals[i]`.
--
-- A test passes if the return or lookup result is true.
-- @uint count Signal count.
-- @string pred Predicate name, which may be any of the following:
--
-- * **"all"**: All tests must pass.
-- * **"any"**: At least one test must pass.
-- * **"any_not**: At least one test must not pass.
-- * **"none"**: No test may pass.
-- @callable update Optional update logic, called as
-- update(signals, count, arg).
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn The signals satisfied the predicate?
-- @see array_ops.All, array_ops.Any
function M.WaitForMultipleSignals (signals, count, pred, update, arg, yvalue)
local pred_op = Setup(Config, pred)
return Body(update, pred_op, Config, signals, count, arg)
end
--- Timed variant of @{WaitForMultipleSignals}, built on top of @{flow_bodies.Body_Timed}.
-- @param signals Callable or read-indexable signal object.
-- @uint count Signal count.
-- @string pred Predicate name, as per @{WaitForMultipleSignals}.
-- @callable update Optional update logic, called as
-- update(time_state, signals, count, arg),
-- with _time\_state_ as per @{flow_bodies.Body_Timed}.
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn boolean The signals satisfied the predicate?
function M.WaitForMultipleSignals_Timed (signals, count, pred, update, arg, yvalue)
local pred_op = Setup(Config, pred)
return Body_Timed(update, pred_op, Config, signals, count, arg)
end
end
do
-- WaitForSignal* config --
local Config = {}
-- Helper to test key
local function Index (t, k)
return t[k]
end
--- Waits for a single signal to fire.
--
-- Built on top of @{flow_bodies.Body}.
-- @param signals Callable or read-indexable signal object. A signal has fired if
-- `signals(what)` or `signals[what]` is true.
-- @param what Signal to watch.
-- @callable update Optional update logic, called as
-- update(signals, what, arg).
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn boolean The signal fired?
function M.WaitForSignal (signals, what, update, arg, yvalue)
Config.yvalue = yvalue
return Body(update, Index, Config, signals, what, arg)
end
--- Timed variant of @{WaitForSignal}, built on top of @{flow_bodies.Body_Timed}.
-- @param signals Callable or read-indexable signal object.
-- @param what Signal to watch.
-- @callable update Optional update logic, called as
-- update(time_state, signals, what, arg),
-- with _time\_state_ as per @{flow_bodies.Body_Timed}.
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn boolean The signal fired?
function M.WaitForSignal_Timed (signals, what, update, arg, yvalue)
Config.yvalue = yvalue
return Body_Timed(update, Index, Config, signals, what, arg)
end
end
do
-- Helper to build ops that wait against a test
local function WaitPair (what, config)
M["Wait" .. what] = function(test, update, arg, yvalue)
config.yvalue = yvalue
return Body(update, test, config, arg)
end
M["Wait" .. what .. "_Timed"] = function(test, update, arg, use_time, yvalue)
config.yvalue = yvalue
config.use_time = not not use_time
return Body_Timed(update, test, config, arg)
end
end
--- Waits for a test to pass.
--
-- Built on top of @{flow_bodies.Body}.
-- @function WaitUntil
-- @callable test Test function, with the same signature as _update_. If it returns
-- true, the wait terminates.
-- @callable update Optional update logic, called as
-- update(arg).
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn boolean The test passed?
--- Timed variant of @{WaitUntil}, built on top of @{flow_bodies.Body_Timed}.
-- @function WaitUntil_Timed
-- @callable test Test function. If it returns true, the wait terminates.
-- @callable update Optional update logic, called as
-- update(time_state, arg),
-- with _time\_state_ as per @{flow_bodies.Body_Timed}.
-- @param arg Argument.
-- @bool use_time _test_ has the same signature as _update_? Otherwise, the _time\_state_
-- argument is omitted.
-- @param yvalue Optional value to yield.
-- @treturn boolean The test passed?
WaitPair("Until", {})
--- Waits for a test to fail.
--
-- Built on top of @{flow_bodies.Body}.
-- @function WaitWhile
-- @callable test Test function, with the same signature as _update_. If it returns
-- false, the wait terminates.
-- @callable update Optional update logic, called as
-- update(arg).
-- @param arg Argument.
-- @param yvalue Optional value to yield.
-- @treturn boolean The test failed?
--- Timed variant of @{WaitWhile}, built on top of @{flow_bodies.Body_Timed}.
-- @function WaitWhile_Timed
-- @callable test Test function. If it returns false, the wait terminates.
-- @callable update Optional update logic, called as
-- update(time_state, arg),
-- with _time\_state_ as per @{flow_bodies.Body_Timed}.
-- @param arg Argument.
-- @bool use_time _test_ has the same signature as _update_? Otherwise, the _time\_state_
-- argument is omitted.
-- @param yvalue Optional value to yield.
-- @treturn boolean The test failed?
WaitPair("While", { negate_done = true })
end
-- Export the module.
return M