You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# These instance variables are critical to the state of the task.
66
66
# In the initialized state, the @block should be set, but the @fiber should be nil.
67
-
# In the running state, the @fiber should be set.
67
+
# In the running state, the @fiber should be set, and @block should be nil.
68
68
# In a finished state, the @block should be nil, and the @fiber should be nil.
69
69
@block=block
70
70
@fiber=nil
71
71
72
-
@status=:initialized
73
-
@finished=Promise.new
72
+
@promise=Promise.new
74
73
75
74
# Handle finished: parameter for backward compatibility:
76
75
casefinished
77
76
whenfalse
78
77
# `finished: false` suppresses warnings for expected task failures:
79
-
@finished.suppress_warnings!
78
+
@promise.suppress_warnings!
80
79
whennil
81
80
# `finished: nil` is the default, no special handling:
82
81
else
@@ -121,7 +120,7 @@ def annotation
121
120
122
121
# @returns [String] A description of the task and it's current status.
123
122
defto_s
124
-
"\#<#{self.description} (#{@status})>"
123
+
"\#<#{self.description} (#{self.status})>"
125
124
end
126
125
127
126
# @deprecated Prefer {Kernel#sleep} except when compatibility with `stable-v1` is required.
@@ -158,22 +157,22 @@ def finished?
158
157
159
158
# @returns [Boolean] Whether the task is running.
160
159
defrunning?
161
-
@status == :running
160
+
self.alive?
162
161
end
163
162
164
163
# @returns [Boolean] Whether the task failed with an exception.
165
164
deffailed?
166
-
@status == :failed
165
+
@promise.failed?
167
166
end
168
167
169
168
# @returns [Boolean] Whether the task has been stopped.
170
169
defstopped?
171
-
@status == :stopped
170
+
@promise.cancelled?
172
171
end
173
172
174
173
# @returns [Boolean] Whether the task has completed execution and generated a result.
175
174
defcompleted?
176
-
@status == :completed
175
+
@promise.completed?
177
176
end
178
177
179
178
# Alias for {#completed?}.
@@ -182,20 +181,32 @@ def complete?
182
181
end
183
182
184
183
# @attribute [Symbol] The status of the execution of the task, one of `:initialized`, `:running`, `:complete`, `:stopped` or `:failed`.
185
-
attr:status
184
+
defstatus
185
+
case@promise.resolved
186
+
when:cancelled
187
+
:stopped
188
+
when:failed
189
+
:failed
190
+
when:completed
191
+
:completed
192
+
whennil
193
+
self.running? ? :running : :initialized
194
+
end
195
+
end
186
196
187
197
# Begin the execution of the task.
188
198
#
189
199
# @raises [RuntimeError] If the task is already running.
190
200
defrun(*arguments)
191
-
if@status == :initialized
192
-
@status=:running
201
+
# Move from initialized to running by clearing @block
202
+
ifblock=@block
203
+
@block=nil
193
204
194
205
scheduledo
195
-
@block.call(self, *arguments)
206
+
block.call(self, *arguments)
196
207
rescue=>error
197
208
# I'm not completely happy with this overhead, but the alternative is to not log anything which makes debugging extremely difficult. Maybe we can introduce a debug wrapper which adds extra logging.
198
-
if !@finished.waiting?
209
+
unless@promise.waiting?
199
210
warn(self,"Task may have ended with unhandled exception.",exception: error)
200
211
end
201
212
@@ -242,13 +253,23 @@ def wait
242
253
raise"Cannot wait on own fiber!"ifFiber.current.equal?(@fiber)
243
254
244
255
# Wait for the task to complete - Promise handles all the complexity:
245
-
# It will either return the result or raise the exception automatically
246
-
@finished.wait
256
+
begin
257
+
@promise.wait
258
+
rescuePromise::Cancel
259
+
# For backward compatibility, stopped tasks return nil
260
+
returnnil
261
+
end
247
262
end
248
263
249
264
# Access the result of the task without waiting. May be nil if the task is not completed. Does not raise exceptions.
250
265
defresult
251
-
@finished.value
266
+
value=@promise.value
267
+
# For backward compatibility, return nil for stopped tasks
268
+
if@promise.cancelled?
269
+
nil
270
+
else
271
+
value
272
+
end
252
273
end
253
274
254
275
# Stop the task and all of its children.
@@ -386,26 +407,21 @@ def finish!
386
407
387
408
# State transition into the completed state.
388
409
defcompleted!(result)
389
-
@status=:completed
390
-
391
410
# Resolve the promise with the result:
392
-
@finished&.resolve(result)
411
+
@promise&.resolve(result)
393
412
end
394
413
395
414
# State transition into the failed state.
396
415
deffailed!(exception=false)
397
-
@status=:failed
398
-
399
416
# Reject the promise with the exception:
400
-
@finished&.reject(exception)
417
+
@promise&.reject(exception)
401
418
end
402
419
403
420
defstopped!
404
421
# Console.info(self, status:) {"Task #{self} was stopped with #{@children&.size.inspect} children!"}
0 commit comments