diff options
author | sushant <sushant@bigbinary.com> | 2019-04-04 10:57:57 +0530 |
---|---|---|
committer | sushant <sushant@bigbinary.com> | 2019-04-04 10:57:57 +0530 |
commit | ca19b7f5d86aa590077766cbe8006f952b6d4296 (patch) | |
tree | 1cd0d9c08401b35a60f22c44470239759262ab69 /activesupport/lib/active_support | |
parent | d8d6bd5e63a9a4a6c06a4dde3c7137ee2be105fd (diff) | |
download | rails-ca19b7f5d86aa590077766cbe8006f952b6d4296.tar.gz rails-ca19b7f5d86aa590077766cbe8006f952b6d4296.tar.bz2 rails-ca19b7f5d86aa590077766cbe8006f952b6d4296.zip |
Added 'detach_from' to 'ActiveSupport::Subscriber' to detach a subscriber from a namespace.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/subscriber.rb | 61 |
1 files changed, 55 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/subscriber.rb b/activesupport/lib/active_support/subscriber.rb index f3e902f9dd..c3cd175a52 100644 --- a/activesupport/lib/active_support/subscriber.rb +++ b/activesupport/lib/active_support/subscriber.rb @@ -24,6 +24,10 @@ module ActiveSupport # After configured, whenever a "sql.active_record" notification is published, # it will properly dispatch the event (ActiveSupport::Notifications::Event) to # the +sql+ method. + # + # We can detach a subscriber as well: + # + # ActiveRecord::StatsSubscriber.detach_from(:active_record) class Subscriber class << self # Attach the subscriber to a namespace. @@ -40,6 +44,25 @@ module ActiveSupport end end + # Detach the subscriber from a namespace. + def detach_from(namespace, notifier = ActiveSupport::Notifications) + @namespace = namespace + @subscriber = find_attached_subscriber + @notifier = notifier + + return unless subscriber + + subscribers.delete(subscriber) + + # Remove event subscribers of all existing methods on the class. + subscriber.public_methods(false).each do |event| + remove_event_subscriber(event) + end + + # Reset notifier so that event subscribers will not add for new methods added to the class. + @notifier = nil + end + # Adds event subscribers for all new methods added to the class. def method_added(event) # Only public methods are added as subscribers, and only if a notifier @@ -58,15 +81,41 @@ module ActiveSupport attr_reader :subscriber, :notifier, :namespace def add_event_subscriber(event) # :doc: - return if %w{ start finish }.include?(event.to_s) + return if invalid_event?(event.to_s) - pattern = "#{event}.#{namespace}" + pattern = prepare_pattern(event) # Don't add multiple subscribers (eg. if methods are redefined). - return if subscriber.patterns.include?(pattern) + return if pattern_subscribed?(pattern) + + subscriber.patterns[pattern] = notifier.subscribe(pattern, subscriber) + end + + def remove_event_subscriber(event) # :doc: + return if invalid_event?(event.to_s) + + pattern = prepare_pattern(event) + + return unless pattern_subscribed?(pattern) + + notifier.unsubscribe(subscriber.patterns[pattern]) + subscriber.patterns.delete(pattern) + end + + def find_attached_subscriber + subscribers.find { |attached_subscriber| attached_subscriber.instance_of?(self) } + end + + def invalid_event?(event) + %w{ start finish }.include?(event.to_s) + end + + def prepare_pattern(event) + "#{event}.#{namespace}" + end - subscriber.patterns << pattern - notifier.subscribe(pattern, subscriber) + def pattern_subscribed?(pattern) + subscriber.patterns.key?(pattern) end end @@ -74,7 +123,7 @@ module ActiveSupport def initialize @queue_key = [self.class.name, object_id].join "-" - @patterns = [] + @patterns = {} super end |