Class: EventUtils::DeferredLoop

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/event_utils/evented_loop.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(main = false) ⇒ DeferredLoop

Returns a new instance of DeferredLoop.



17
18
19
20
21
# File 'lib/event_utils/evented_loop.rb', line 17

def initialize(main=false)
  self.class.main_loop.wait_for(self) unless main
  @success, @failure = 0, 0
  @waiting_for = []
end

Instance Attribute Details

#failureObject

Returns the value of attribute failure.



13
14
15
# File 'lib/event_utils/evented_loop.rb', line 13

def failure
  @failure
end

#successObject

Returns the value of attribute success.



13
14
15
# File 'lib/event_utils/evented_loop.rb', line 13

def success
  @success
end

Class Method Details

.main_loopObject



8
9
10
# File 'lib/event_utils/evented_loop.rb', line 8

def self.main_loop
  @main_loop ||= new(:main)
end

Instance Method Details

#child_returned(outcome) ⇒ Object

callback used by deferrables the loop is waiting for, it increases failure or success depending by the child outcome



33
34
35
36
# File 'lib/event_utils/evented_loop.rb', line 33

def child_returned(outcome)
  send(outcome.to_s + '=', send(outcome) + 1)
  terminate if complete?
end

#wait_for(deferrable) ⇒ Object

add a deferrable to the list of deferrables to wait for



39
40
41
42
43
# File 'lib/event_utils/evented_loop.rb', line 39

def wait_for(deferrable)
  @waiting_for << deferrable
  deferrable.callback { self.child_returned :success }
  deferrable.errback { self.child_returned :failure }
end

#waiting_for(*deferrables, &block) ⇒ Object

takes a list of deferrables to wait for before executing the callback



24
25
26
27
28
29
# File 'lib/event_utils/evented_loop.rb', line 24

def waiting_for(*deferrables, &block)
  deferrables.each { |d| wait_for(d) }.size
  callback  { block.call } if block_given?
  errback { raise DeferredChildFailure, "#{failure} deferred entities failed" }
  self
end