Skip to content

Commit

Permalink
Optimize Future#recover for the case when the receiving future is com…
Browse files Browse the repository at this point in the history
…pleted
  • Loading branch information
iconara committed Oct 31, 2014
1 parent 56db366 commit cc4f143
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/ione/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,19 +501,29 @@ def then(&block)
# @yieldreturn [Object] the value of the new future
# @return [Ione::Future] a new future representing a value recovered from the error
def recover(value=nil, &block)
f = CompletableFuture.new
on_complete do |v, e|
if e
begin
f.resolve(block ? block.call(e) : value)
rescue => e
f.fail(e)
if resolved?
self
elsif failed?
begin
Future.resolved(block ? block.call(@error) : value)
rescue => e
Future.failed(e)
end
else
f = CompletableFuture.new
on_complete do |v, e|
if e
begin
f.resolve(block ? block.call(e) : value)
rescue => e
f.fail(e)
end
else
f.resolve(v)
end
else
f.resolve(v)
end
f
end
f
end

# Returns a new future which represents either the value of the original
Expand Down

0 comments on commit cc4f143

Please sign in to comment.