Skip to content

Commit

Permalink
Optimize Future#then for the case when the receiving future is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed Oct 31, 2014
1 parent dea077e commit b08adea
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions lib/ione/future.rb
Expand Up @@ -432,30 +432,53 @@ def flat_map(&block)
# that will resolve to the transformed value.
# @return [Ione::Future] a new future representing the transformed value
def then(&block)
f = CompletableFuture.new
on_complete do |v, e|
if e
f.fail(e)
else
begin
fv = block.call(v)
if fv.respond_to?(:on_complete)
fv.on_complete do |vv, ee|
if ee
f.fail(ee)
else
f.resolve(vv)
end
if resolved?
begin
fv = block.call(@value)
if fv.respond_to?(:on_complete)
f = CompletableFuture.new
fv.on_complete do |v, e|
if e
f.fail(e)
else
f.resolve(v)
end
else
f.resolve(fv)
end
rescue => e
f
else
Future.resolved(fv)
end
rescue => e
Future.failed(e)
end
elsif failed?
self
else
f = CompletableFuture.new
on_complete do |v, e|
if e
f.fail(e)
else
begin
fv = block.call(v)
if fv.respond_to?(:on_complete)
fv.on_complete do |vv, ee|
if ee
f.fail(ee)
else
f.resolve(vv)
end
end
else
f.resolve(fv)
end
rescue => e
f.fail(e)
end
end
end
f
end
f
end

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

0 comments on commit b08adea

Please sign in to comment.