Class: EventMachine::Synchrony::Multi

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em-synchrony/em-multi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMulti

Returns a new instance of Multi



8
9
10
11
# File 'lib/em-synchrony/em-multi.rb', line 8

def initialize
  @requests = {}
  @responses = {:callback => {}, :errback => {}}
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests



6
7
8
# File 'lib/em-synchrony/em-multi.rb', line 6

def requests
  @requests
end

#responsesObject (readonly)

Returns the value of attribute responses



6
7
8
# File 'lib/em-synchrony/em-multi.rb', line 6

def responses
  @responses
end

Instance Method Details

#add(name, conn) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/em-synchrony/em-multi.rb', line 13

def add(name, conn)
  raise 'Duplicate Multi key' if @requests.key? name

  fiber = Fiber.current
  conn.callback { @responses[:callback][name] = conn; check_progress(fiber) }
  conn.errback  { @responses[:errback][name]  = conn; check_progress(fiber) }

  @requests[name] = conn
end

#finished?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/em-synchrony/em-multi.rb', line 23

def finished?
  (@responses[:callback].size + @responses[:errback].size) == @requests.size
end

#performObject



27
28
29
# File 'lib/em-synchrony/em-multi.rb', line 27

def perform
  Fiber.yield unless finished?
end