From fc0882ba5a0f18281736859718252042b15614ad Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Mon, 1 Mar 2010 17:33:59 -0800 Subject: Optimize AS::Notifications to remember which subscribers don't match and not run them. This will allow notifications that are only useful in dev or testing to run efficiently in production. --- activesupport/lib/active_support/notifications/fanout.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support/notifications') 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? -- cgit v1.2.3