Skip to content

Commit

Permalink
Optimize Future#fallback for the case when the receiving future is co…
Browse files Browse the repository at this point in the history
…mpleted
  • Loading branch information
iconara committed Oct 31, 2014
1 parent cc4f143 commit 84cb1c1
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/ione/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,26 +549,36 @@ def recover(value=nil, &block)
# @return [Ione::Future] a new future representing a value recovered from the
# error
def fallback(&block)
f = CompletableFuture.new
on_complete do |v, e|
if e
begin
ff = block.call(e)
ff.on_complete do |vv, ee|
if ee
f.fail(ee)
else
f.resolve(vv)
if resolved?
self
elsif failed?
begin
block.call(@error)
rescue => e
Future.failed(e)
end
else
f = CompletableFuture.new
on_complete do |v, e|
if e
begin
ff = block.call(e)
ff.on_complete do |vv, ee|
if ee
f.fail(ee)
else
f.resolve(vv)
end
end
rescue => e
f.fail(e)
end
rescue => e
f.fail(e)
else
f.resolve(v)
end
else
f.resolve(v)
end
f
end
f
end
end

Expand Down

0 comments on commit 84cb1c1

Please sign in to comment.