Skip to content

Commit

Permalink
Add event_store_locator option to AsyncHandler
Browse files Browse the repository at this point in the history
Because eager loading of the event store is not suitable to
legacy applications. Howevery current event_store: option will be
removed only at RES 3.0.
  • Loading branch information
swistak35 committed Nov 27, 2021
1 parent 855acb9 commit eb73818
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ def self.with_defaults
with
end

def self.with(event_store: Rails.configuration.event_store, serializer: YAML)
def self.with(event_store: Rails.configuration.event_store, event_store_locator: nil, serializer: YAML)
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.symbolize_keys))
end
end
Expand Down
13 changes: 13 additions & 0 deletions rails_event_store/spec/async_handler_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def perform(event)
end
end

class HandlerWithEventStoreLocator < ActiveJob::Base
def perform(event)
$queue.push(event)
end
end

class HandlerWithSpecifiedSerializer < ActiveJob::Base
def perform(event)
$queue.push(event)
Expand Down Expand Up @@ -101,6 +107,13 @@ def verify(subscriber)
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)
another_event_store.publish(ev = RailsEventStore::Event.new)
expect($queue.pop).to eq(ev)
end

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

0 comments on commit eb73818

Please sign in to comment.