1
1
require 'thread'
2
2
3
+ require 'concurrent/ivar'
3
4
require 'concurrent/obligation'
4
5
require 'concurrent/executor/executor_options'
5
6
@@ -181,8 +182,7 @@ module Concurrent
181
182
# - `on_success { |result| ... }` is the same as `then {|result| ... }`
182
183
# - `rescue { |reason| ... }` is the same as `then(Proc.new { |reason| ... } )`
183
184
# - `rescue` is aliased by `catch` and `on_error`
184
- class Promise
185
- include Obligation
185
+ class Promise < IVar
186
186
include ExecutorOptions
187
187
188
188
# Initialize a new Promise with the provided options.
@@ -203,6 +203,7 @@ class Promise
203
203
# @see http://promises-aplus.github.io/promises-spec/
204
204
def initialize ( opts = { } , &block )
205
205
opts . delete_if { |k , v | v . nil? }
206
+ super ( IVar ::NO_VALUE , opts )
206
207
207
208
@executor = get_executor_from ( opts ) || Concurrent . global_io_executor
208
209
@args = get_arguments_from ( opts )
@@ -214,17 +215,13 @@ def initialize(opts = {}, &block)
214
215
@promise_body = block || Proc . new { |result | result }
215
216
@state = :unscheduled
216
217
@children = [ ]
217
-
218
- init_obligation
219
- set_deref_options ( opts )
220
218
end
221
219
222
220
# @return [Promise]
223
221
def self . fulfill ( value , opts = { } )
224
222
Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , true , value , nil ) }
225
223
end
226
224
227
-
228
225
# @return [Promise]
229
226
def self . reject ( reason , opts = { } )
230
227
Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , false , nil , reason ) }
@@ -444,17 +441,21 @@ def notify_child(child)
444
441
if_state ( :rejected ) { child . on_reject ( @reason ) }
445
442
end
446
443
444
+ # @!visibility private
445
+ def complete ( success , value , reason )
446
+ children_to_notify = mutex . synchronize do
447
+ set_state! ( success , value , reason )
448
+ @children . dup
449
+ end
450
+
451
+ children_to_notify . each { |child | notify_child ( child ) }
452
+ end
453
+
447
454
# @!visibility private
448
455
def realize ( task )
449
456
@executor . post do
450
457
success , value , reason = SafeTaskExecutor . new ( task ) . execute ( *@args )
451
-
452
- children_to_notify = mutex . synchronize do
453
- set_state! ( success , value , reason )
454
- @children . dup
455
- end
456
-
457
- children_to_notify . each { |child | notify_child ( child ) }
458
+ complete ( success , value , reason )
458
459
end
459
460
end
460
461
0 commit comments