Class: EventStoreSubscriptions::SubscriptionRevision

Inherits:
Struct
  • Object
show all
Defined in:
lib/event_store_subscriptions/subscription_revision.rb

Overview

This class is used to persist and update the revision when subscribing to the specific stream. Specific streams are streams which names differ from “$all”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSubscriptionRevision

Returns a new instance of SubscriptionRevision.



9
10
11
12
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 9

def initialize(*)
  super
  @update_hooks = []
end

Instance Attribute Details

#revisionObject

Returns the value of attribute revision

Returns:

  • (Object)

    the current value of revision



6
7
8
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 6

def revision
  @revision
end

#update_hooksObject (readonly)

Returns the value of attribute update_hooks.



7
8
9
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 7

def update_hooks
  @update_hooks
end

Instance Method Details

#empty?Boolean

Checks if revision property is absent

Returns:

  • (Boolean)


49
50
51
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 49

def empty?
  revision.nil?
end

#present?Boolean

Checks if revision property is set

Returns:

  • (Boolean)


55
56
57
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 55

def present?
  !empty?
end

#register_update_hook(&blk) ⇒ void

This method returns an undefined value.

Adds a handler that will be executed when the revision gets updates. You may add as many handlers as you want. Example:

```ruby
subscription_revision.register_update_hook do |position|
  # do something with the position. E.g. persist it somewhere
end
subscription_revision.register_update_hook do |position|
  # do something else with the position
end
```


43
44
45
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 43

def register_update_hook(&blk)
  update_hooks << blk
end

#to_optionHash

Constructs a hash compatible for usage with EventStoreClient::GRPC::Client#subscribe_to_stream method. You can pass it into :options keyword argument of that method to set the starting revision of the stream.

Returns:

  • (Hash)


63
64
65
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 63

def to_option
  { from_revision: revision }
end

#update(response) ⇒ Boolean

Updates the revision from GRPC response.

Parameters:

  • response (EventStore::Client::Streams::ReadResp)

    GRPC EventStore object. See its structure in the lib/event_store_client/adapters/grpc/generated/streams_pb.rb file in the ‘event_store_client` gem.

Returns:

  • (Boolean)

    whether the revision was updated



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 19

def update(response)
  return false unless response.event&.event

  # Updating revision value in memory first to prevent the situation when update hook fails and,
  # thus keeping the revision not up to date
  self.revision = response.event.event.stream_revision
  update_hooks.each do |handler|
    handler.call(self)
  end
  true
end