Skip to content

Commit

Permalink
Trigger listeners only for enabled plugin
Browse files Browse the repository at this point in the history
Previously any plugin that would subscribe to notifications would be
always triggered, which made it impossible to isolate plugin configs
  • Loading branch information
solnic committed Jun 26, 2021
1 parent 6abbc4e commit bd3a8c0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/rom/components/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def adapter
)
end

# @api public
# @api private
def apply_plugins
plugins.each do |plugin|
plugin.apply_to(constant)
Expand Down
22 changes: 22 additions & 0 deletions lib/rom/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,32 @@ def configure(*args)

# @api private
def finalize
attach_listeners
setup.finalize
self
end

# @api private
def attach_listeners
# Anything can attach globally to certain events, including plugins, so here
# we're making sure that only plugins that are enabled in thi configuration
# will be triggered
plugin_listeners = Notifications.listeners.slice(plugins.map(&:mod))
global_listeners = Notifications.listeners.except(plugin_listeners.keys)

listeners.update(global_listeners).update(plugin_listeners)
end

# @api private
def listeners
notifications.listeners
end

# @api private
def attach_plugins
plugins.select(&:attachable?).each { |plugin| plugin.attach_to(notifications) }
end

# Apply a plugin to the configuration
#
# @param [Mixed] plugin The plugin identifier, usually a Symbol
Expand Down
16 changes: 10 additions & 6 deletions lib/rom/support/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Publisher
#
# @api public
def subscribe(event_id, query = EMPTY_HASH, &block)
listeners[event_id] << [block, query]
listeners[self] << [event_id, block, query]
self
end

Expand All @@ -67,7 +67,11 @@ def subscribe(event_id, query = EMPTY_HASH, &block)
def trigger(event_id, payload = EMPTY_HASH)
event = events[event_id]

listeners[event.id].each do |(listener, query)|
event_listeners = listeners.values.flatten(1).group_by(&:first)

return unless event_listeners.key?(event_id)

event_listeners[event_id].each do |(_, listener, query)|
event.payload(payload).trigger(listener, query)
end
end
Expand Down Expand Up @@ -178,7 +182,7 @@ def self.listeners
#
# @api public
def self.event_bus(id)
EventBus.new(id, events: events.dup, listeners: listeners.dup)
EventBus.new(id, events: events.dup)
end

# Extension for objects that can listen to events
Expand All @@ -193,7 +197,7 @@ module Listener
#
# @api public
def subscribe(event_id, query = EMPTY_HASH, &block)
Notifications.listeners[event_id] << [block, query]
Notifications.listeners[self] << [event_id, block, query]
end
end

Expand Down Expand Up @@ -224,9 +228,9 @@ class EventBus
# @param [Hash] listeners A hash with listeners
#
# @api public
def initialize(id, events: EMPTY_HASH, listeners: LISTENERS_HASH.dup)
def initialize(id, events: EMPTY_HASH)
@id = id
@listeners = listeners
@listeners = LISTENERS_HASH.dup
@events = events
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/shared/changeset/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
RSpec.shared_context "changeset / database setup" do
include_context "changeset / db_uri"

let(:configuration) { ROM::Configuration.new(:sql, db_uri) }
let(:configuration) do
ROM::Configuration.new(:sql, db_uri) do |config|
config.plugin(:sql, relation: :auto_restrictions)
end
end

let(:conn) { configuration.gateways[:default].connection }

Expand Down
5 changes: 4 additions & 1 deletion spec/shared/repository/database.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "rom/sql"
RSpec.shared_context "repository / db_uri" do
let(:base_db_uri) do
ENV.fetch("BASE_DB_URI", "postgres@localhost/rom")
Expand All @@ -14,7 +15,9 @@
include_context "repository / db_uri"

let(:configuration) do
ROM::Configuration.new(:sql, db_uri)
ROM::Configuration.new(:sql, db_uri) do |config|
config.plugin(:sql, relation: :auto_restrictions)
end
end

let(:conn) do
Expand Down

0 comments on commit bd3a8c0

Please sign in to comment.