Class: EventMachine::Synchrony::Iterator

Inherits:
EM::Iterator
  • Object
show all
Defined in:
lib/em-synchrony/iterator.rb

Instance Method Summary collapse

Instance Method Details

#each(foreach = nil, after = nil, &blk) ⇒ Object

synchronous iterator which will wait until all the jobs are done before returning. Unfortunately this means that you loose ability to choose concurrency on the fly (see iterator documentation in EM)


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

def each(foreach=nil, after=nil, &blk)
  fiber = Fiber.current

  fe = (foreach || blk)
  cb = Proc.new do
    after.call if after
    fiber.resume
  end

  Fiber.yield super(fe, cb)
end

#inject(obj, foreach = nil, after = nil, &block) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/em-synchrony/iterator.rb', line 35

def inject(obj, foreach = nil, after = nil, &block)
  if foreach and after
    super(obj, foreach, after)
  else
    fiber = Fiber.current
    result = nil

    after = Proc.new {|res| result = res; fiber.resume}
    super(obj, block, after)

    Fiber.yield
    result
  end
end

#map(&block) ⇒ Object


24
25
26
27
28
29
30
31
32
33
# File 'lib/em-synchrony/iterator.rb', line 24

def map(&block)
  fiber = Fiber.current
  result = nil

  after = Proc.new {|res| result = res; fiber.resume }
  super(block, after)

  Fiber.yield
  result
end