Skip to content

Commit

Permalink
Add option to not execute zipped promises
Browse files Browse the repository at this point in the history
This restores the default behavior for zipped promises and adds an
option (`execute`) to leave them unscheduled.
  • Loading branch information
davishmcclurg authored and pitr-ch committed Feb 24, 2018
1 parent 2e8027a commit d85aaf3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/concurrent/promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,13 @@ def flat_map(&block)
#
# @return [Promise<Array>]
def self.zip(*promises)
opts = promises.last.is_a?(::Hash) ? promises.pop : {}
opts = promises.last.is_a?(::Hash) ? promises.pop.dup : {}
opts[:executor] ||= ImmediateExecutor.new
zero = Promise.new(opts) { [] }
zero = if !opts.key?(:execute) || opts.delete(:execute)
fulfill([], opts)
else
Promise.new(opts) { [] }
end

promises.reduce(zero) do |p1, p2|
p1.flat_map do |results|
Expand Down
28 changes: 26 additions & 2 deletions spec/concurrent/promise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,21 @@ def get_ivar_from_args(opts)
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
let(:promise3) { Promise.new(executor: :immediate) { [3] } }

it 'does not execute the returned Promise' do
it 'executes the returned Promise by default' do
composite = promise1.zip(promise2, promise3)

expect(composite).to be_fulfilled
end

it 'executes the returned Promise when execute is true' do
composite = promise1.zip(promise2, promise3, execute: true)

expect(composite).to be_fulfilled
end

it 'does not execute the returned Promise when execute is false' do
composite = promise1.zip(promise2, promise3, execute: false)

expect(composite).to be_unscheduled
end

Expand Down Expand Up @@ -389,9 +401,21 @@ def get_ivar_from_args(opts)
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
let(:promise3) { Promise.new(executor: :immediate) { [3] } }

it 'does not execute the returned Promise' do
it 'executes the returned Promise by default' do
composite = Promise.zip(promise1, promise2, promise3)

expect(composite).to be_fulfilled
end

it 'executes the returned Promise when execute is true' do
composite = Promise.zip(promise1, promise2, promise3, execute: true)

expect(composite).to be_fulfilled
end

it 'does not execute the returned Promise when execute is false' do
composite = Promise.zip(promise1, promise2, promise3, execute: false)

expect(composite).to be_unscheduled
end

Expand Down

0 comments on commit d85aaf3

Please sign in to comment.