Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial proof of concept

  • Loading branch information...
commit 6e0fc5b180a297decefa3b9497ea9a3318d80de3 0 parents
@mloughran authored
Showing with 64 additions and 0 deletions.
  1. +33 −0 node.rb
  2. +31 −0 test.rb
33 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 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
+}
Please sign in to comment.
Something went wrong with that request. Please try again.