Skip to content

Commit

Permalink
Changed Promise API to conform to execute-method pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mighe committed Feb 22, 2014
1 parent 8efce41 commit 78986e2
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 73 deletions.
27 changes: 22 additions & 5 deletions lib/concurrent/promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,30 @@ def initialize(*args, &block)

@lock = Mutex.new
@handler = block || Proc.new{|result| result }
@state = :pending
@value = nil
@reason = nil
@state = :unscheduled
@rescued = false
@children = []
@rescuers = []
@args = args

init_obligation
realize(*args) if root?
end

# @return [Promise]
def execute
if root?
if compare_and_set_state(:pending, :unscheduled)
@chain.each { |c| c.state = :pending }
realize(*@args)
end
else
parent.execute
end
self
end

def self.execute(*args, &block)
new(*args, &block).execute
end

def rescued?
Expand All @@ -57,13 +72,15 @@ def rescued?
#
# @return [Promise] the new promise
def then(&block)
block = Proc.new{ |result| result } if block.nil?

child = @lock.synchronize do
block = Proc.new{|result| result } if block.nil?
@children << Promise.new(self, &block)
@children.last.on_reject(@reason) if rejected?
push(@children.last)
@children.last
end

return child
end

Expand Down
12 changes: 6 additions & 6 deletions spec/concurrent/event_machine_defer_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module Concurrent
EventMachine.run do

@a = @b = @c = nil
p = Promise.new(1, 2, 3) do |a, b, c|
p = Promise.execute(1, 2, 3) do |a, b, c|
@a, @b, @c = a, b, c
end
sleep(0.1)
Expand All @@ -155,7 +155,7 @@ module Concurrent

EventMachine.run do
@expected = nil
Promise.new(10){|a| a * 2 }.then{|result| @expected = result}
Promise.new(10){|a| a * 2 }.then{|result| @expected = result}.execute
sleep(0.1)
@expected.should eq 20

Expand All @@ -168,7 +168,7 @@ module Concurrent

EventMachine.run do

p = Promise.new(10){|a| a * 2 }.then{|result| result * 2}
p = Promise.execute(10){|a| a * 2 }.then{|result| result * 2}
sleep(0.1)
p.value.should eq 40

Expand All @@ -184,7 +184,7 @@ module Concurrent

EventMachine.run do

p = Promise.new{ raise StandardError.new('Boom!') }
p = Promise.execute{ raise StandardError.new('Boom!') }
sleep(0.1)
p.reason.should be_a(Exception)
p.reason.should.to_s =~ /Boom!/
Expand All @@ -200,7 +200,7 @@ module Concurrent
EventMachine.run do

@expected = nil
Promise.new{ raise StandardError }.
Promise.execute{ raise StandardError }.
on_error(StandardError){|ex| @expected = 1 }.
on_error(StandardError){|ex| @expected = 2 }.
on_error(StandardError){|ex| @expected = 3 }
Expand All @@ -217,7 +217,7 @@ module Concurrent
EventMachine.run do

@expected = nil
Promise.new{ raise StandardError }.
Promise.execute{ raise StandardError }.
on_error(ArgumentError){|ex| @expected = ex }.
on_error(LoadError){|ex| @expected = ex }.
on_error(Exception){|ex| @expected = ex }
Expand Down

0 comments on commit 78986e2

Please sign in to comment.