Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

# add em-defer

  • Loading branch information...
commit ab78cb2642678f9f3336cdff859ca0828daba6da 1 parent 8795b83
@kostya kostya authored
Showing with 49 additions and 1 deletion.
  1. +1 −1  README.md
  2. +8 −0 lib/em-synchrony.rb
  3. +40 −0 spec/defer_spec.rb
View
2  README.md
@@ -8,7 +8,7 @@ Collection of convenience classes and primitives to help untangle evented code,
* Fiber aware Multi-request interface for any callback enabled clients
* Fiber aware TCPSocket replacement, powered by EventMachine
* Fiber aware Thread, Mutex, ConditionVariable clases
- * Fiber aware sleep
+ * Fiber aware sleep, defer
Supported clients:
View
8 lib/em-synchrony.rb
@@ -98,6 +98,14 @@ def self.add_periodic_timer(interval, &blk)
def self.next_tick(&blk)
EM.next_tick { Fiber.new { blk.call }.resume }
end
+
+ # Fiber-aware EM.defer
+ #
+ def self.defer op = nil, &blk
+ fiber = Fiber.current
+ EM.defer(op || blk, lambda{ |result| fiber.resume(result) })
+ Fiber.yield
+ end
# Routes to EM::Synchrony::Keyboard
#
View
40 spec/defer_spec.rb
@@ -0,0 +1,40 @@
+require "spec/helper/all"
+
+describe EventMachine::Synchrony do
+
+ it "defer: simple" do
+ EM.synchrony do
+ x = 1
+
+ result = EM::Synchrony.defer do
+ x = 2
+ sleep 0.1
+ 3
+ end
+
+ result.should == 3
+ x.should == 2
+
+ EM.stop
+ end
+ end
+
+ it "defer: with lambda" do
+ EM.synchrony do
+
+ x = 1
+
+ op = lambda do
+ sleep 0.1
+ x += 1
+ 3
+ end
+
+ EM::Synchrony.defer(op).should == 3
+ x.should == 2
+
+ EM.stop
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.