Class: RailsEventSourcing::Dispatcher
- Inherits:
-
Object
- Object
- RailsEventSourcing::Dispatcher
- 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
-
.dispatch(event) ⇒ Object
Dispatches events to matching Reactors once.
-
.on(*events, trigger: [], async: []) ⇒ Object
Register Reactors to Events.
- .rules ⇒ Object
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 |