class RailsEventSourcing::Dispatcher

Dispatcher implementation used by Events on after save.

Public Class Methods

dispatch(event) click to toggle source

Dispatches events to matching Reactors once. Called by all events after they are created.

# File lib/rails-event-sourcing/dispatcher.rb, line 24
def dispatch(event)
  reactors = rules.for(event)
  reactors.sync.each { |reactor| reactor.call(event) }
  reactors.async.each { |reactor| RailsEventSourcing::ReactorJob.perform_later(event, reactor.to_s) }
end
on(*events, trigger: [], async: []) click to toggle source

Register Reactors to Events.

  • Reactors registered with ‘trigger` will be triggered synchronously

  • Reactors registered with ‘async` will be triggered asynchronously via a ActiveJob

Example:

on SomeEvent, trigger: ->(item) { puts "Callable block on #{item.id}" }
on BaseEvent, trigger: LogEvent, async: TrackEvent
on PledgeCancelled, PaymentFailed, async: [NotifyAdmin, CreateTask]
on [PledgeCancelled, PaymentFailed], async: [NotifyAdmin, CreateTask]
# File lib/rails-event-sourcing/dispatcher.rb, line 18
def on(*events, trigger: [], async: [])
  rules.register(events: events.flatten, sync: Array(trigger), async: Array(async))
end
rules() click to toggle source
# File lib/rails-event-sourcing/dispatcher.rb, line 30
def rules
  @@rules ||= RuleSet.new # rubocop:disable Style/ClassVars
end