Class: RailsAuditLog::Graphql::Subscriptions::Broadcaster
- Inherits:
-
Object
- Object
- RailsAuditLog::Graphql::Subscriptions::Broadcaster
- Defined in:
- lib/rails_audit_log/graphql/subscriptions/broadcaster.rb
Overview
Bridges ActiveSupport::Notifications events emitted by rails_audit_log to GraphQL subscription triggers.
Start it once in an initializer after the schema is defined:
Rails.application.config.after_initialize do
RailsAuditLog::Graphql::Subscriptions::Broadcaster.new(schema: MySchema).start
end
For each rails_audit_log.entry_created notification the broadcaster fires two subscription triggers:
-
auditLogEntryCreated(itemType:, itemId:) for record-specific subscribers
-
auditLogEntryCreated(actorId:) for actor-specific subscribers (when an actor is present on the entry)
Constant Summary collapse
- EVENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"rails_audit_log.entry_created"
Instance Method Summary collapse
-
#broadcast(entry) ⇒ void
Trigger GraphQL subscriptions for
entry. -
#initialize(schema:) ⇒ Broadcaster
constructor
A new instance of Broadcaster.
-
#start ⇒ void
Subscribe to
rails_audit_log.entry_creatednotifications and begin broadcasting to GraphQL subscribers. -
#stop ⇒ void
Unsubscribe from
ActiveSupport::Notifications.
Constructor Details
#initialize(schema:) ⇒ Broadcaster
Returns a new instance of Broadcaster.
26 27 28 29 |
# File 'lib/rails_audit_log/graphql/subscriptions/broadcaster.rb', line 26 def initialize(schema:) @schema = schema @subscriber = nil end |
Instance Method Details
#broadcast(entry) ⇒ void
This method returns an undefined value.
Trigger GraphQL subscriptions for entry. Fires both the record-scoped and actor-scoped variants.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rails_audit_log/graphql/subscriptions/broadcaster.rb', line 56 def broadcast(entry) @schema.subscriptions.trigger( "audit_log_entry_created", {item_type: entry.item_type, item_id: entry.item_id.to_s}, entry ) return unless entry.actor_id.present? @schema.subscriptions.trigger( "audit_log_entry_created", {actor_id: entry.actor_id.to_s}, entry ) end |
#start ⇒ void
This method returns an undefined value.
Subscribe to rails_audit_log.entry_created notifications and begin broadcasting to GraphQL subscribers. Idempotent — calling start a second time replaces the previous subscriber.
36 37 38 39 40 |
# File 'lib/rails_audit_log/graphql/subscriptions/broadcaster.rb', line 36 def start @subscriber = ActiveSupport::Notifications.subscribe(EVENT) do |*, payload| broadcast(payload[:entry]) end end |
#stop ⇒ void
This method returns an undefined value.
Unsubscribe from ActiveSupport::Notifications. After calling stop, no further subscription triggers will fire until #start is called again.
46 47 48 49 |
# File 'lib/rails_audit_log/graphql/subscriptions/broadcaster.rb', line 46 def stop ActiveSupport::Notifications.unsubscribe(@subscriber) if @subscriber @subscriber = nil end |