Permalink
Browse files

add Fiber-aware EM.next_tick convenience function, closes #59

  • Loading branch information...
igrigorik committed Sep 30, 2011
1 parent e1ab523 commit b6f90f52183006ef8a2d07ebe7aa0be16477727b
Showing with 35 additions and 14 deletions.
  1. +10 −4 lib/em-synchrony.rb
  2. +25 −10 spec/synchrony_spec.rb
View
@@ -59,7 +59,7 @@ def self.sync(df)
end
# Fiber-aware sleep function using an EM timer
- #
+ #
# Execution is stopped for specified amount of seconds
# and then automatically resumed (just like regular sleep)
# except without locking the reactor thread
@@ -70,8 +70,8 @@ def self.sleep(secs)
Fiber.yield
end
- # Fiber-aware EventMachine timer: wraps the passed in
- # block within a new fiber context such that you can
+ # Fiber-aware EventMachine timer: wraps the passed in
+ # block within a new fiber context such that you can
# continue using synchrony methods
#
def self.add_timer(interval, &blk)
@@ -89,7 +89,13 @@ def self.add_periodic_timer(interval, &blk)
Fiber.new { blk.call }.resume
end
end
-
+
+ # Fiber-aware EM.next_tick convenience function
+ #
+ def self.next_tick(&blk)
+ EM.next_tick { Fiber.new { blk.call }.resume }
+ end
+
# Routes to EM::Synchrony::Keyboard
#
def self.gets
View
@@ -1,14 +1,29 @@
require "helper/all"
-describe EM::Synchrony, "#sync" do
- it "returns immediately if the syncee already succeeded" do
- args = stub("args")
-
- Fiber.new {
- df = EM::DefaultDeferrable.new
- df.succeed args
-
- EM::Synchrony.sync(df).should == args
- }.resume
+describe EM::Synchrony do
+ describe "#sync" do
+ it "returns immediately if the syncee already succeeded" do
+ args = stub("args")
+
+ Fiber.new {
+ df = EM::DefaultDeferrable.new
+ df.succeed args
+
+ EM::Synchrony.sync(df).should == args
+ }.resume
+ end
+ end
+
+ describe "#next_tick" do
+ it "should wrap next_tick into a Fiber context" do
+ Fiber.new {
+ df = EM::DefaultDeferrable.new
+
+ EM::Synchrony.next_tick do
+ df.succeed args
+ EM::Synchrony.sync(df).should == args
+ end
+ }.resume
+ end
end
end

0 comments on commit b6f90f5

Please sign in to comment.