Class: Servus::Events::Bus
- Inherits:
-
Object
- Object
- Servus::Events::Bus
- Defined in:
- lib/servus/events/bus.rb
Overview
Central event bus for registering Event classes and dispatching events through configured routers.
The Bus maintains a registry mapping event names to their Event class definitions (one-to-one). On emit, it delegates to the configured routers to resolve invocations, deduplicates by key, and executes. ActiveSupport::Notifications wraps the dispatch cycle for the subscribe_all hook (e.g. forwarding to Eventus).
Class Method Summary collapse
-
.clear ⇒ void
Clears all registered events.
-
.emit(event_name, payload) ⇒ void
Emits an event through the configured routers.
-
.enable_logging! ⇒ void
Installs the internal event logger.
-
.event_for(name) ⇒ Class?
Returns the Event class registered for the given name.
-
.register_event(name, event_class) ⇒ void
Registers an Event class for a specific event name.
-
.subscribe_all {|event_name, payload, started_at:, finished_at:, id:| ... } ⇒ Object
Subscribes to all Servus event emissions.
Class Method Details
.clear ⇒ void
This method returns an undefined value.
Clears all registered events.
Useful for testing and development mode reloading.
146 147 148 |
# File 'lib/servus/events/bus.rb', line 146 def clear @events = nil end |
.emit(event_name, payload) ⇒ void
This method returns an undefined value.
Emits an event through the configured routers.
Collects invocations from all routers (in config array order), deduplicates by key (first wins), and executes each. The entire dispatch is wrapped in ActiveSupport::Notifications so that subscribe_all listeners receive timing information.
82 83 84 85 86 87 88 |
# File 'lib/servus/events/bus.rb', line 82 def emit(event_name, payload) ActiveSupport::Notifications.instrument(notification_name(event_name), payload) do resolve_invocations(event_name, payload) .uniq(&:key) .each(&:execute) end end |
.enable_logging! ⇒ void
This method returns an undefined value.
Installs the internal event logger. Called once at boot via the Railtie. Logs every event emission with its AS::Notifications correlation ID and dispatch duration.
Multiple subscribe_all subscriptions coexist — the app can add its own (e.g. for Eventus forwarding) independently.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/servus/events/bus.rb', line 127 def enable_logging! return if @logging_enabled subscribe_all do |event_name, payload, **| duration_ms = ([:finished_at] - [:started_at]) * 1000 Servus::Support::Logger.log_event(event_name, payload, event_id: [:id], duration_ms:) end @logging_enabled = true end |
.event_for(name) ⇒ Class?
Returns the Event class registered for the given name.
64 65 66 |
# File 'lib/servus/events/bus.rb', line 64 def event_for(name) events[name] end |
.register_event(name, event_class) ⇒ void
This method returns an undefined value.
Registers an Event class for a specific event name.
Each event name maps to exactly one Event class. Attempting to register a second class for the same name raises an error.
Event classes are typically registered automatically at boot time via the event_name DSL method or ensure_registered!.
48 49 50 51 52 53 54 |
# File 'lib/servus/events/bus.rb', line 48 def register_event(name, event_class) if events.key?(name) raise "Event :#{name} is already registered to #{events[name]}. Cannot register #{event_class}" end events[name] = event_class end |
.subscribe_all {|event_name, payload, started_at:, finished_at:, id:| ... } ⇒ Object
Subscribes to all Servus event emissions.
Yields the clean event name and payload as positional args, plus started_at, finished_at, and id as keyword args. Returns the subscription for manual unsubscribe.
112 113 114 115 116 117 |
# File 'lib/servus/events/bus.rb', line 112 def subscribe_all(&block) ActiveSupport::Notifications.subscribe(/^servus\.events\./) do |name, started, finished, id, payload| event_name = name.delete_prefix('servus.events.').to_sym block.call(event_name, payload, started_at: started, finished_at: finished, id: id) end end |