module AWS::Core::Collection::WithLimitAndNextToken

def _each_batch options = {}, &block

def _each_batch options = {}, &block
  limit = _extract_limit(options)
  batch_size = _extract_batch_size(options)
  next_token = _extract_next_token(options)
  total = 0  # count of items yeilded across all batches
  begin
    max = nil
    if limit or batch_size
      max = []
      max << (limit - total) if limit
      max << batch_size if batch_size
      max = max.min
    end
    batch = []
    next_token = _each_item(next_token, max, options.dup) do |item|
      total += 1
      batch << item
    end
    yield(batch)
  end until next_token.nil? or (limit and limit == total)
  next_token
end