Skip to content

Commit 8af772c

Browse files
committed
Add join method on instance, with alias :+
1 parent de53f16 commit 8af772c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

lib/concurrent/next.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def promise(executor = :fast)
145145
def join(*futures)
146146
countdown = Concurrent::AtomicFixnum.new futures.size
147147
promise = OuterPromise.new(futures)
148-
futures.each { |future| future.add_callback :join, countdown, promise, *futures }
148+
futures.each { |future| future.add_callback :join_callback, countdown, promise, *futures }
149149
promise.future
150150
end
151151
end
@@ -345,6 +345,12 @@ def inspect
345345
"#{to_s[0..-2]} blocks:[#{blocks.map(&:to_s).join(', ')}]>"
346346
end
347347

348+
def join(*futures)
349+
ConcurrentNext.join self, *futures
350+
end
351+
352+
alias_method :+, :join
353+
348354
# @api private
349355
def complete(success, value, reason, raise = true) # :nodoc:
350356
callbacks = synchronize do
@@ -401,28 +407,24 @@ def set_promise_on_completion(promise)
401407
promise.complete success?, value, reason
402408
end
403409

404-
def join(countdown, promise, *futures)
410+
def join_callback(countdown, promise, *futures)
405411
if success?
406412
promise.success futures.map(&:value) if countdown.decrement.zero?
407413
else
408414
promise.try_fail reason
409415
end
410416
end
411417

412-
def with_promise(promise, &block)
413-
promise.evaluate_to &block
414-
end
415-
416418
def chain_callback(executor, promise, callback)
417-
with_async(executor) { with_promise(promise) { callback_on_completion callback } }
419+
with_async(executor) { promise.evaluate_to { callback_on_completion callback } }
418420
end
419421

420422
def then_callback(executor, promise, callback)
421-
with_async(executor) { with_promise(promise) { conditioned_callback callback } }
423+
with_async(executor) { promise.evaluate_to { conditioned_callback callback } }
422424
end
423425

424426
def rescue_callback(executor, promise, callback)
425-
with_async(executor) { with_promise(promise) { callback_on_failure callback } }
427+
with_async(executor) { promise.evaluate_to { callback_on_failure callback } }
426428
end
427429

428430
def with_async(executor)
@@ -725,6 +727,9 @@ def execute_once
725727
branch1 = head.then(&:succ).then(&:succ)
726728
branch2 = head.then(&:succ).then_delay(&:succ)
727729
result = ConcurrentNext.join(branch1, branch2).then { |b1, b2| b1 + b2 }
730+
# other variants
731+
result = branch1.join(branch2).then { |b1, b2| b1 + b2 }
732+
result = (branch1 + branch2).then { |b1, b2| b1 + b2 }
728733

729734
sleep 0.1
730735
p branch1.completed?, branch2.completed? # true, false

0 commit comments

Comments
 (0)