Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6e0fc5b
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'rubygems' | ||
require 'eventmachine' | ||
|
||
class Node | ||
attr_accessor :parent | ||
|
||
def initialize(&strategy) | ||
@children = [] | ||
@strategy = strategy | ||
end | ||
|
||
def <<(child) | ||
child.parent = self | ||
@children << child | ||
end | ||
|
||
def run | ||
deferrable = @strategy.call | ||
deferrable.callback do | ||
puts "Strategy succeeded" | ||
EM.next_tick(method(:run_children)) | ||
end | ||
deferrable.errback do | ||
puts "Strategy failed" | ||
end | ||
end | ||
|
||
def run_children | ||
@children.each do |child| | ||
child.run | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'node' | ||
|
||
def create_deferrable(id) | ||
deferrable = EM::DefaultDeferrable.new | ||
deferrable.callback do | ||
puts "Finished node #{id}" | ||
end | ||
EM::Timer.new(1) do | ||
deferrable.succeed | ||
end | ||
deferrable | ||
end | ||
|
||
EM.run { | ||
n1 = Node.new do | ||
create_deferrable(1) | ||
end | ||
|
||
n2 = Node.new do | ||
create_deferrable(2) | ||
end | ||
|
||
n3 = Node.new do | ||
create_deferrable(3) | ||
end | ||
|
||
n1 << n2 | ||
n1 << n3 | ||
|
||
n1.run | ||
} |