From 72a8e99441405d04d416b510fac6754926d827c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Reszke?= Date: Wed, 8 Feb 2023 16:20:32 +0100 Subject: [PATCH] Prefer lazy event store initialization ref: eb7381887f01733944f48ba4ae66b45c47e60187 --- .../lib/rails_event_store/async_handler_helpers.rb | 8 +++----- rails_event_store/spec/async_handler_helpers_spec.rb | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/rails_event_store/lib/rails_event_store/async_handler_helpers.rb b/rails_event_store/lib/rails_event_store/async_handler_helpers.rb index a509d7c6ed..5e1c5017c1 100644 --- a/rails_event_store/lib/rails_event_store/async_handler_helpers.rb +++ b/rails_event_store/lib/rails_event_store/async_handler_helpers.rb @@ -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 diff --git a/rails_event_store/spec/async_handler_helpers_spec.rb b/rails_event_store/spec/async_handler_helpers_spec.rb index 8dfd468fd3..06bce7df30 100644 --- a/rails_event_store/spec/async_handler_helpers_spec.rb +++ b/rails_event_store/spec/async_handler_helpers_spec.rb @@ -83,7 +83,7 @@ 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) @@ -91,7 +91,6 @@ def perform(_event) 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) @@ -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)