Class: RailsEventSourcing::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-event-sourcing/dispatcher.rb

Overview

Dispatcher implementation used by Events on after save.

Defined Under Namespace

Classes: ReactorSet, RuleSet

Class Method Summary collapse

Class Method Details

.dispatch(event) ⇒ Object

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



24
25
26
27
28
# 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: []) ⇒ Object

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]


18
19
20
# 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

.rulesObject



30
31
32
# File 'lib/rails-event-sourcing/dispatcher.rb', line 30

def rules
  @@rules ||= RuleSet.new # rubocop:disable Style/ClassVars
end