Class: EventStoreClient::GRPC::Commands::Streams::ReadPaginated

Inherits:
Command
  • Object
show all
Defined in:
lib/event_store_client/adapters/grpc/commands/streams/read_paginated.rb

Constant Summary collapse

RecordsLimitError =
Class.new(StandardError)
DEFAULT_READ_DIRECTION =
:Forwards

Instance Method Summary collapse

Methods inherited from Command

#connection_options, #initialize, #metadata, #request, #service, use_request, use_service

Constructor Details

This class inherits a constructor from EventStoreClient::GRPC::Commands::Command

Instance Method Details

#call(stream_name, options:, skip_deserialization:, skip_decryption:, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

  • EventStoreClient::GRPC::Commands::Streams::ReadPaginated.{EventStoreClient{EventStoreClient::GRPC{EventStoreClient::GRPC::Client{EventStoreClient::GRPC::Client#read_paginated}


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
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/event_store_client/adapters/grpc/commands/streams/read_paginated.rb', line 15

def call(stream_name, options:, skip_deserialization:, skip_decryption:, &blk)
  # TODO: Improve the implementation by extracting the pagination into a separate class to
  # allow persisting the pagination options(position, direction, max_count) among the
  # whole instance. This approach will allow us to get rid of passing paginate options
  # into private methods explicitly.
  position, direction, max_count = nil
  first_call = true
  Enumerator.new do |yielder|
    loop do
      response =
        Read.new(config: config, **connection_options).call(
          stream_name,
          options: options,
          skip_deserialization: true,
          skip_decryption: true
        ) do |opts|
          if first_call
            # Evaluate user-provided block only once
            yield opts if blk
            position = get_position(opts)
            direction = get_direction(opts)
            max_count = opts.count.to_i
            validate_max_count(max_count)
            first_call = false
          end

          paginate_options(opts, position)
        end
      processed_response =
        EventStoreClient::GRPC::Shared::Streams::ProcessResponses.
          new(config: config).
          call(
            response,
            skip_deserialization,
            skip_decryption
          )

      yielder << processed_response if processed_response.any?
      raise StopIteration if end_reached?(response, max_count)

      position = calc_next_position(response, direction, stream_name)
      raise StopIteration if position.negative?
    end
  end
end