aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/notifications.rb2
-rw-r--r--activesupport/lib/active_support/notifications/fanout.rb15
2 files changed, 13 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb
index fca2efd969..3f1fe64e9b 100644
--- a/activesupport/lib/active_support/notifications.rb
+++ b/activesupport/lib/active_support/notifications.rb
@@ -44,7 +44,7 @@ module ActiveSupport
class << self
attr_writer :notifier
- delegate :publish, :subscribe, :to => :notifier
+ delegate :publish, :subscribe, :unsubscribe, :to => :notifier
delegate :instrument, :to => :instrumenter
def notifier
diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb
index e08011e23f..cd60054862 100644
--- a/activesupport/lib/active_support/notifications/fanout.rb
+++ b/activesupport/lib/active_support/notifications/fanout.rb
@@ -5,6 +5,7 @@ module ActiveSupport
class Fanout
def initialize
@subscribers = []
+ @listeners_for = {}
end
def bind(pattern)
@@ -12,16 +13,22 @@ module ActiveSupport
end
def subscribe(pattern = nil, &block)
+ @listeners_for.clear
@subscribers << Subscriber.new(pattern, &block)
@subscribers.last
end
def unsubscribe(subscriber)
@subscribers.delete(subscriber)
+ @listeners_for.clear
end
- def publish(*args)
- @subscribers.each { |s| s.publish(*args) }
+ def publish(name, *args)
+ if listeners = @listeners_for[name]
+ listeners.each { |s| s.publish(name, *args) }
+ else
+ @listeners_for[name] = @subscribers.select { |s| s.publish(name, *args) }
+ end
end
# This is a sync queue, so there is not waiting.
@@ -53,7 +60,9 @@ module ActiveSupport
end
def publish(*args)
- push(*args) if matches?(args.first)
+ return unless matches?(args.first)
+ push(*args)
+ true
end
def drained?