Module: SlowEnumeratorTools::Bufferer

Defined in:
lib/slow_enumerator_tools/bufferer.rb

Class Method Summary collapse

Class Method Details

.buffer(enum, size) ⇒ Object



5
6
7
8
9
# File 'lib/slow_enumerator_tools/bufferer.rb', line 5

def self.buffer(enum, size)
  queue = SizedQueue.new(size)
  thread = SlowEnumeratorTools::Util.gen_collector_thread(enum, queue)
  gen_enumerator(queue, thread)
end

.gen_enumerator(queue, collector_thread) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slow_enumerator_tools/bufferer.rb', line 11

def self.gen_enumerator(queue, collector_thread)
  Enumerator.new do |y|
    loop do
      elem = queue.pop

      if SlowEnumeratorTools::Util::STOP_OK.equal?(elem)
        break
      elsif SlowEnumeratorTools::Util::STOP_ERR.equal?(elem)
        raise queue.pop
      end

      y << elem
    end
    collector_thread.join
  end.lazy
end