Module: SlowEnumeratorTools::Batcher

Defined in:
lib/slow_enumerator_tools/batcher.rb

Class Method Summary collapse

Class Method Details

.batch(enum) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/slow_enumerator_tools/batcher.rb', line 5

def self.batch(enum)
  queue = Queue.new

  t = SlowEnumeratorTools::Util.gen_collector_thread(enum, queue)

  Enumerator.new do |y|
    loop do
      res = []
      ended = false

      # pop first
      elem = queue.pop
      if SlowEnumeratorTools::Util::STOP_OK.equal?(elem)
        break
      elsif SlowEnumeratorTools::Util::STOP_ERR.equal?(elem)
        raise queue.pop
      end
      res << elem

      loop do
        # pop remaining
        begin
          elem = queue.pop(true)
        rescue ThreadError
          break
        end
        if SlowEnumeratorTools::Util::STOP_OK.equal?(elem)
          ended = true
          break
        elsif SlowEnumeratorTools::Util::STOP_ERR.equal?(elem)
          raise queue.pop
        end
        res << elem
      end

      y << res
      break if ended
    end

    t.join
  end.lazy
end