Sha256: 91eb8a8c14f1acaa65aba1b8cc092989309e830ad098b786000751441c61faf8

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module SlowEnumeratorTools
  module Batcher
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slow_enumerator_tools-1.1.0 lib/slow_enumerator_tools/batcher.rb