Module: Bacon::EMSpec

Defined in:
lib/bacon/ext/em.rb

Overview

:nodoc:

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.context_fiberObject

Returns the value of attribute context_fiber.



15
16
17
# File 'lib/bacon/ext/em.rb', line 15

def context_fiber
  @context_fiber
end

Instance Method Details

#doneObject



40
41
42
43
# File 'lib/bacon/ext/em.rb', line 40

def done
  EM.cancel_timer(@timeout)
  wakeup
end

#new_fiber(&block) ⇒ Object



45
46
47
# File 'lib/bacon/ext/em.rb', line 45

def new_fiber(&block)
  Fiber.new(&block).resume
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bacon/ext/em.rb', line 49

def run(*)
  if EMSpec.context_fiber == Fiber.current
    super
  else
    EM::run do
      EM::error_handler do |err|
        ::Bacon::store_error(err, "(EM Loop)")
      end
      
      new_fiber do
        EMSpec.context_fiber = Fiber.current
        begin
          super
          EM::stop_event_loop
        ensure
          EMSpec.context_fiber = nil
        end
      end
      
    end
    
  end
end

#wait(timeout = 0.1, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bacon/ext/em.rb', line 28

def wait(timeout = 0.1, &block)
  @waiting_fiber = Fiber.current
  EM::cancel_timer(@timeout)
  @timeout = EM::add_timer(timeout, &method(:wakeup))
  
  Fiber.yield
  
  @waiting_fiber = nil
  
  block.call if block
end

#wakeupObject



18
19
20
21
22
23
24
25
26
# File 'lib/bacon/ext/em.rb', line 18

def wakeup
  # this should be enough but for some reason the reactor
  # idles for 20 seconds on EM::stop before really exiting
  # @waiting_fiber.resume
  
  if @waiting_fiber
    EM::next_tick { @waiting_fiber.resume }
  end
end