Class: RailsSimpleEventSourcing::EventBus

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_simple_event_sourcing/event_bus.rb

Class Method Summary collapse

Class Method Details

.dispatch(event) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rails_simple_event_sourcing/event_bus.rb', line 16

def dispatch(event)
  subscribers_for(event).each do |subscriber|
    subscriber.perform_later(event)
  rescue StandardError => e
    Rails.logger.error(
      "[RailsSimpleEventSourcing::EventBus] Failed to enqueue #{subscriber} for event ##{event.id}: #{e.message}"
    )
  end
end

.reset!Object



26
27
28
# File 'lib/rails_simple_event_sourcing/event_bus.rb', line 26

def reset!
  @subscriptions = Concurrent::Map.new { |h, k| h[k] = Concurrent::Array.new }
end

.subscribe(event_class, subscriber) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rails_simple_event_sourcing/event_bus.rb', line 8

def subscribe(event_class, subscriber)
  unless subscriber.is_a?(Class) && subscriber < ActiveJob::Base
    raise ArgumentError, "Subscriber must be an ActiveJob class, got #{subscriber}"
  end

  @subscriptions[event_class.to_s] << subscriber
end