Skip to content

Commit

Permalink
Prefer lazy event store initialization
Browse files Browse the repository at this point in the history
ref: eb73818
  • Loading branch information
lukaszreszke committed Feb 8, 2023
1 parent cab0cff commit 72a8e99
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ def self.with_defaults
end

def self.with(
event_store: Rails.configuration.event_store,
event_store_locator: nil,
serializer: RubyEventStore::Serializers::YAML
serializer: RubyEventStore::Serializers::YAML,
event_store_locator: -> { Rails.configuration.event_store }
)
Module.new do
define_method :perform do |payload|
event_store = event_store_locator.call if event_store_locator
super(event_store.deserialize(serializer: serializer, **payload.transform_keys(&:to_sym)))
super(event_store_locator.call.deserialize(serializer: serializer, **payload.transform_keys(&:to_sym)))
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions rails_event_store/spec/async_handler_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ def perform(_event)
end

specify "with specified event store" do
HandlerWithAnotherEventStore.prepend RailsEventStore::AsyncHandler.with(event_store: another_event_store)
HandlerWithAnotherEventStore.prepend RailsEventStore::AsyncHandler.with(event_store_locator: -> { another_event_store} )
event_store.subscribe_to_all_events(HandlerWithAnotherEventStore)
event_store.publish(ev = RubyEventStore::Event.new)
expect($queue.pop).to eq(ev)
end

specify "with specified event store locator" do
HandlerWithEventStoreLocator.prepend RailsEventStore::AsyncHandler.with(
event_store: nil,
event_store_locator: -> { another_event_store }
)
another_event_store.subscribe_to_all_events(HandlerWithEventStoreLocator)
Expand All @@ -101,7 +100,7 @@ def perform(_event)

specify "with specified serializer" do
HandlerWithSpecifiedSerializer.prepend RailsEventStore::AsyncHandler.with(
event_store: json_event_store,
event_store_locator: -> { json_event_store },
serializer: JSON
)
json_event_store.subscribe_to_all_events(HandlerWithSpecifiedSerializer)
Expand Down

0 comments on commit 72a8e99

Please sign in to comment.