Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DependentDeferrable #469

Merged
merged 1 commit into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,26 @@ Great sadness!
=> [:passed]
```

#### DependentDeferrable

`DependentDeferrable` depends on children deferrables. A `DependentDeferrable`
succeeds only when every child succeeds and fails immediately when any child
fails

```ruby
> d1 = EM::DefaultDeferrable.new
=> #<BubbleWrap::Reactor::DefaultDeferrable:0x10c713750>
> d2 = EM::DefaultDeferrable.new
=> #<BubbleWrap::Reactor::DefaultDeferrable:0x10370bb10>
> d = EM::DependentDeferrable.on(d1, d2)
=> #<BubbleWrap::Reactor::DependentDeferrable:0x106c17b80>
> d.callback {|a, b| puts "a: #{a} b: #{b}"}
=> [#<Proc:0x103075210>]
> d1.succeed 'one', 'one more'
> d2.succeed :two
a: ["one", "one more"] b: [:two]
```

#### ThreadAwareDeferrable

```ruby
Expand Down
8 changes: 8 additions & 0 deletions motion/reactor/deferrable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def timeout(seconds)
@deferred_timeout = Timer.new(seconds) {me.fail}
end

def deferred_status
@deferred_status ||= :unknown
end

def deferred_args
@deferred_args
end

end
end
end
27 changes: 27 additions & 0 deletions motion/reactor/dependent_deferrable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module BubbleWrap
module Reactor
class DependentDeferrable < DefaultDeferrable
# args are Deferrable(s) which the returned Deferrable depends on.
# returns a Deferrable that depends on args.
# which:
# succeeds only when every Deferrable in args succeeds
# fails immediately when any Deferrable in args fails
# Have to be careful that #deferred_args for DependentDeferrable is a list of #deferred_args from its children Deferrable(s).
def self.on(*args)
deferrable = self.new
@children_deferrables = args
@children_deferrables.each do |e|
e.callback do |result|
if @children_deferrables.all? {|a| a.deferred_status == :succeeded}
deferrable.succeed(*@children_deferrables.map(&:deferred_args))
end
end
e.errback do |result|
deferrable.fail(*e.deferred_args)
end
end
deferrable
end
end
end
end
93 changes: 93 additions & 0 deletions spec/motion/reactor/dependent_deferrable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
describe BubbleWrap::Reactor::DependentDeferrable do

before do
@subject = Class.new do
include BubbleWrap::Reactor::Deferrable
end
@d1 = @subject.new
@d2 = @subject.new
@object = BubbleWrap::Reactor::DependentDeferrable.on(@d1, @d2)
end

describe '.callback' do

it "calls the callback block with arguments after all children .succeed" do
args1 = [1, 2]
args2 = [3, 4]
@object.callback do |*passed_args|
passed_args.should.equal [args1, args2]
end
@d1.succeed *args1
@d2.succeed *args2
end

it "calls the callback block with arguments only after all children .succeed" do
execution_order = []
@object.callback do |*passed_args|
execution_order << 3
end
execution_order << 1
@d1.succeed :succeeds
execution_order << 2
@d2.succeed :succeeds
execution_order.should.equal [1, 2, 3]
end

it "calls the callback if even if the deferrable already succeeded" do
@d1.succeed :succeeded1
@d2.succeed :succeeded2
@object.callback do |*args|
NSLog "args: #{args} class: #{args.class}"
args[0].should.equal [:succeeded1]
args[1].should.equal [:succeeded2]
end
end

end

describe '.errback' do
it "calls the errback block after a child fails" do
args1 = [1, 2]
args2 = [3, 4]
@object.errback do |*passed_args|
passed_args.should.equal args1
end
@d1.fail *args1
end

it "calls the errback block immediately after a child fails" do
execution_order = []
@object.errback do |*passed_args|
execution_order << 2
end
execution_order << 1
@d1.fail :fail
execution_order << 3
@d2.fail :fail
execution_order.should.equal [1, 2, 3]
end

it "calls the errback block after a child fails even if others succeeds" do
args1 = [1, 2]
args2 = [3, 4]
callback_called = false
@object.callback do |*passed_args|
callback_called = true
end
@object.errback do |*passed_args|
passed_args.should.equal args2
end
@d1.succeed *args1
@d2.fail *args2
callback_called.should.equal false
end

it "calls the errback block even if the deferrable failed first" do
@d1.fail :err
@object.errback do |err|
err.should.equal :err
end
end
end

end