lib/em-synchrony.rb in em-synchrony-0.3.0.beta.1 vs lib/em-synchrony.rb in em-synchrony-1.0.0
- old
+ new
@@ -10,10 +10,11 @@
require "em-synchrony/thread"
require "em-synchrony/em-multi"
require "em-synchrony/tcpsocket"
require "em-synchrony/connection_pool"
+require "em-synchrony/keyboard"
require "em-synchrony/iterator" if EventMachine::VERSION > '0.12.10'
module EventMachine
# A convenience method for wrapping EM.run body within
@@ -42,20 +43,43 @@
# either succeeds or fails. You do not need to patch or modify the
# Deferrable object, simply pass it to EM::Synchrony.sync
#
def self.sync(df)
f = Fiber.current
- df.callback { |r| f.resume(r) }
- df.errback { |r| f.resume(r) }
+ xback = proc {|r|
+ if f == Fiber.current
+ return r
+ else
+ f.resume r
+ end
+ }
+ df.callback &xback
+ df.errback &xback
Fiber.yield
end
# a Fiber-aware sleep function using an EM timer
- def self.sleep( secs )
+ def self.sleep(secs)
fiber = Fiber.current
- EM::Timer.new(secs) { fiber.resume }
+ EM::Timer.new(secs) { fiber.resume }
Fiber.yield
end
- end
+ def self.add_timer(interval, &blk)
+ EM.add_timer(interval) do
+ Fiber.new { blk.call }.resume
+ end
+ end
+
+ def self.add_periodic_timer(interval, &blk)
+ EM.add_periodic_timer(interval) do
+ Fiber.new { blk.call }.resume
+ end
+ end
+
+ # routes to EM::Synchrony::Keyboard
+ def self.gets
+ EventMachine::Synchrony::Keyboard.new.gets
+ end
+ end
end