Skip to content

Commit

Permalink
Initial proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
mloughran committed Oct 21, 2009
0 parents commit 6e0fc5b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
33 changes: 33 additions & 0 deletions node.rb
@@ -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
31 changes: 31 additions & 0 deletions test.rb
@@ -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
}

0 comments on commit 6e0fc5b

Please sign in to comment.