Class: EventStoreSubscriptions::SubscriptionRevision
- Inherits:
-
Struct
- Object
- Struct
- EventStoreSubscriptions::SubscriptionRevision
- 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
-
#revision ⇒ Object
Returns the value of attribute revision.
-
#update_hooks ⇒ Object
readonly
Returns the value of attribute update_hooks.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Checks if revision property is absent.
-
#initialize ⇒ SubscriptionRevision
constructor
A new instance of SubscriptionRevision.
-
#present? ⇒ Boolean
Checks if revision property is set.
-
#register_update_hook(&blk) ⇒ void
Adds a handler that will be executed when the revision gets updates.
-
#to_option ⇒ Hash
Constructs a hash compatible for usage with EventStoreClient::GRPC::Client#subscribe_to_stream method.
-
#update(response) ⇒ Boolean
Updates the revision from GRPC response.
Constructor Details
#initialize ⇒ SubscriptionRevision
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
#revision ⇒ Object
Returns the value of attribute revision
6 7 8 |
# File 'lib/event_store_subscriptions/subscription_revision.rb', line 6 def revision @revision end |
#update_hooks ⇒ Object (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
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
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_option ⇒ Hash
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.
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.
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 |