@@ -203,18 +203,7 @@ class Promise < IVar
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 , &nil )
207
-
208
- @executor = Executor . executor_from_options ( opts ) || Concurrent . global_io_executor
209
- @args = get_arguments_from ( opts )
210
-
211
- @parent = opts . fetch ( :parent ) { nil }
212
- @on_fulfill = opts . fetch ( :on_fulfill ) { Proc . new { |result | result } }
213
- @on_reject = opts . fetch ( :on_reject ) { Proc . new { |reason | raise reason } }
214
-
215
- @promise_body = block || Proc . new { |result | result }
216
- @state = :unscheduled
217
- @children = [ ]
206
+ super ( IVar ::NO_VALUE , opts . merge ( __promise_body_from_block__ : block ) , &nil )
218
207
end
219
208
220
209
# Create a new `Promise` and fulfill it immediately.
@@ -266,7 +255,7 @@ def execute
266
255
def set ( value = IVar ::NO_VALUE , &block )
267
256
raise PromiseExecutionError . new ( 'supported only on root promise' ) unless root?
268
257
check_for_block_or_value! ( block_given? , value )
269
- mutex . synchronize do
258
+ synchronize do
270
259
if @state != :unscheduled
271
260
raise MultipleAssignmentError
272
261
else
@@ -319,7 +308,7 @@ def then(rescuer = nil, &block)
319
308
on_reject : rescuer
320
309
)
321
310
322
- mutex . synchronize do
311
+ synchronize do
323
312
child . state = :pending if @state == :pending
324
313
child . on_fulfill ( apply_deref_options ( @value ) ) if @state == :fulfilled
325
314
child . on_reject ( @reason ) if @state == :rejected
@@ -447,6 +436,21 @@ def self.any?(*promises)
447
436
448
437
protected
449
438
439
+ def ns_initialize ( value , opts )
440
+ super
441
+
442
+ @executor = Executor . executor_from_options ( opts ) || Concurrent . global_io_executor
443
+ @args = get_arguments_from ( opts )
444
+
445
+ @parent = opts . fetch ( :parent ) { nil }
446
+ @on_fulfill = opts . fetch ( :on_fulfill ) { Proc . new { |result | result } }
447
+ @on_reject = opts . fetch ( :on_reject ) { Proc . new { |reason | raise reason } }
448
+
449
+ @promise_body = opts [ :__promise_body_from_block__ ] || Proc . new { |result | result }
450
+ @state = :unscheduled
451
+ @children = [ ]
452
+ end
453
+
450
454
# Aggregate a collection of zero or more promises under a composite promise,
451
455
# execute the aggregated promises and collect them into a standard Ruby array,
452
456
# call the given Ruby `Ennnumerable` predicate (such as `any?`, `all?`, `none?`,
@@ -472,7 +476,7 @@ def self.aggregate(method, *promises)
472
476
473
477
# @!visibility private
474
478
def set_pending
475
- mutex . synchronize do
479
+ synchronize do
476
480
@state = :pending
477
481
@children . each { |c | c . set_pending }
478
482
end
@@ -503,7 +507,7 @@ def notify_child(child)
503
507
504
508
# @!visibility private
505
509
def complete ( success , value , reason )
506
- children_to_notify = mutex . synchronize do
510
+ children_to_notify = synchronize do
507
511
set_state! ( success , value , reason )
508
512
@children . dup
509
513
end
@@ -528,7 +532,7 @@ def set_state!(success, value, reason)
528
532
529
533
# @!visibility private
530
534
def synchronized_set_state! ( success , value , reason )
531
- mutex . synchronize { set_state! ( success , value , reason ) }
535
+ synchronize { set_state! ( success , value , reason ) }
532
536
end
533
537
end
534
538
end
0 commit comments