-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCoroutine.ttslua
executable file
·261 lines (210 loc) · 8.42 KB
/
Coroutine.ttslua
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
require('ge_tts.License')
---@class ge_tts__Coroutine
local Coroutine = {}
---@param co thread
---@param onError nil | fun(message: string): void
local function resumeWithErrorHandling(co, onError)
local result, message = coroutine.resume(co)
if not result then
if onError then
(--[[---@not nil]] onError)(message)
else
error(message)
end
end
end
---@overload fun<R>(fn: (fun(resume: (fun(result: R): void)): void), timeout?: nil | number, onError?: nil | (fun(message: string): void), incorrectResumptionErrorMessage?: nil | string): boolean, R
---@overload fun(fn: (fun(resume: (fun(): void)): void), timeout?: nil | number, onError?: nil | (fun(message: string): void), incorrectResumptionErrorMessage?: nil | string): boolean
---@generic R
---@param fn fun(resume: (fun(result: R): void)): void
---@param timeout? nil | number @Timeout in seconds (optional).
---@param onError? nil | fun(message: string): void @A handler for any errors raised by the current coroutine after it has been resumed.
---@param incorrectResumptionErrorMessage? nil | string
---@return false | (true, R) @true followed by fn result if resume was called before the timeout, or false if the (optional) timeout was reached.
function Coroutine.yield(fn, timeout, onError, incorrectResumptionErrorMessage)
local co = coroutine.running()
local yielded = false
---@type nil | boolean
local resumed
---@type R
local result
---@type fun(userResult: R): void
local resume = function(userResult)
if resumed then
return -- Already resumed
end
result = userResult
resumed = true
if yielded then
-- If resume is called synchronously in fn, then we haven't (and won't) yield. Thus, there's no suspended
-- coroutine to resume.
resumeWithErrorHandling(co, onError)
end
end
---@type nil | number
local waitId
if timeout then
waitId = Wait.time(function()
if resumed then
return
end
waitId = nil
resumed = false
resumeWithErrorHandling(co, onError)
end, --[[---@not nil]] timeout)
end
fn(resume)
if not resumed then
yielded = true
coroutine.yield()
end
if waitId then
Wait.stop(--[[---@not nil]] waitId)
end
if not resumed then
if resumed == nil then
error(incorrectResumptionErrorMessage)
end
return false
end
return true, result
end
---@overload fun<T, R>(arr: T[], fn: (fun(resume: (fun(result: R): void), element: T, index: number): void), timeout: number, onError?: nil | (fun(message: string): void), incorrectResumptionErrorMessage?: nil | string): (true, R[]) | (false, table<number, R>)
---@overload fun<T, R>(arr: T[], fn: (fun(resume: (fun(result: R): void), element: T, index: number): void), timeout?: nil, onError?: nil | (fun(message: string): void), incorrectResumptionErrorMessage?: nil | string): true, R[]
---@overload fun<T>(arr: T[], fn: (fun(resume: (fun(): void), element: T, index: number): void), timeout?: nil | number, onError?: nil | (fun(message: string): void), incorrectResumptionErrorMessage?: nil | string): boolean
---@generic T
---@generic R
---@param arr T[]
---@param fn fun(resume: (fun(result: R): void), element: T, index: number): void
---@param timeout? nil | number @Timeout in seconds (optional).
---@param onError? nil | fun(message: string): void @A handler for any errors raised by the current coroutine after it has been resumed.
---@param incorrectResumptionErrorMessage? nil | string
---@return (true, R[]) | (false, table<number, R>) @true followed by fn result if resume was called before the timeout, or false if the (optional) timeout was reached.
function Coroutine.yieldAll(arr, fn, timeout, onError, incorrectResumptionErrorMessage)
local co = coroutine.running()
local count = #arr
if count == 0 then
return true, {}
end
local resultCount = 0
local yielded = false
---@type nil | boolean
local resumed
---@type nil | number
local waitId
if timeout then
Wait.time(function()
if resumed then
return
end
waitId = nil
resumed = false
resumeWithErrorHandling(co, onError)
end, --[[---@not nil]] timeout)
end
---@type table<number, true>
local elementsResumed = {}
---@type table<number, R>
local results = {}
for i, element in ipairs(arr) do
---@type fun(userResult: R): void
local resume = function(userResult)
if elementsResumed[i] then
return
end
elementsResumed[i] = true
results[i] = userResult
resultCount = resultCount + 1
if resultCount == count then
-- If resume is called synchronously in fn, then we haven't (and won't) yield. Thus, there's no suspended
-- coroutine to resume.
resumed = true
if yielded then
resumeWithErrorHandling(co, onError)
end
end
end
fn(resume, element, i)
end
if not resumed then
yielded = true
coroutine.yield()
end
if waitId then
Wait.stop(--[[---@not nil]] waitId)
end
if resumed == nil then
error(incorrectResumptionErrorMessage)
end
if resumed then
return true, --[[---@type R[] ]] results
end
return false, results
end
--- Yields from the current coroutine. Resumes once a condition is met or an optional timeout is reached.
---@overload fun(condition: fun(): boolean): true
---@overload fun(condition: (fun(): boolean), timeout: number): boolean
---@param condition fun(): boolean @Return true when the current coroutine should be resumed.
---@param timeout nil | number @Timeout in seconds (optional).
---@param onError nil | fun(message: string): void @A handler for any errors raised by the current coroutine after it has been resumed.
---@return boolean @True if the condition was met, or false if the (optional) timeout was reached.
function Coroutine.yieldCondition(condition, timeout, onError)
local co = coroutine.running()
---@type nil | boolean
local conditionMet
local resume = function()
conditionMet = true
resumeWithErrorHandling(co, onError)
end
if timeout then
Wait.condition(resume, condition, --[[---@not nil]] timeout, function()
conditionMet = false
resumeWithErrorHandling(co, onError)
end)
else
Wait.condition(resume, condition)
end
coroutine.yield()
if conditionMet == nil then
error("Coroutine.yieldCondition(): attempt to resume before Wait was completed!")
end
return --[[---@not nil]] conditionMet
end
--- Yields from the current coroutine, which will later be resumed after the specified number of frames have passed.
---@overload fun(frames: number): void
---@param frames number
---@param onError nil | fun(message: string): void @A handler for any errors raised by the current coroutine after it has been resumed.
function Coroutine.yieldFrames(frames, onError)
Coroutine.yield(
---@param resume fun(): void
function(resume)
Wait.frames(resume, frames)
end,
nil,
onError,
"Coroutine.yieldFrames(): attempt to resume before Wait was completed!"
)
end
--- Yields from the current coroutine, which will later be resumed after the specified number of seconds have passed.
---@overload fun(seconds: number): void
---@param seconds number
---@param onError nil | fun(message: string): void @A handler for any errors raised by the current coroutine after it has been resumed.
function Coroutine.yieldSeconds(seconds, onError)
Coroutine.yield(
---@param resume fun(): void
function(resume)
Wait.time(resume, seconds)
end,
nil,
onError,
"Coroutine.yieldSeconds(): attempt to resume before Wait was completed!"
)
end
--- Creates a coroutine from the specified function and immediately starts it, passing any provided arguments.
---@param func fun
---@vararg any
---@return boolean, any...
function Coroutine.start(func, ...)
return coroutine.resume(coroutine.create(func), ...)
end
return Coroutine