Evented notifications take priority over Timed notifications

In cases where a notification subscriber includes methods to support
both Evented and Timed events, Evented should take priority over Timed.
This allows subscribers to be backwards compatible (older Rails only
allows Timed events) while defaulting to newer behavior.
This commit is contained in:
Eric Saxby 2012-08-11 17:39:20 -07:00
parent a513cc1862
commit 6fe36ba585
2 changed files with 23 additions and 3 deletions

View File

@ -59,10 +59,10 @@ module ActiveSupport
module Subscribers # :nodoc:
def self.new(pattern, listener)
if listener.respond_to?(:call)
subscriber = Timed.new pattern, listener
else
if listener.respond_to?(:start) and listener.respond_to?(:finish)
subscriber = Evented.new pattern, listener
else
subscriber = Timed.new pattern, listener
end
unless pattern

View File

@ -19,6 +19,12 @@ module ActiveSupport
end
end
class ListenerWithTimedSupport < Listener
def call(name, start, finish, id, payload)
@events << [:call, name, start, finish, id, payload]
end
end
def test_evented_listener
notifier = Fanout.new
listener = Listener.new
@ -62,6 +68,20 @@ module ActiveSupport
[:finish, 'hello', 1, {}],
], listener.events
end
def test_evented_listener_priority
notifier = Fanout.new
listener = ListenerWithTimedSupport.new
notifier.subscribe 'hi', listener
notifier.start 'hi', 1, {}
notifier.finish 'hi', 1, {}
assert_equal [
[:start, 'hi', 1, {}],
[:finish, 'hi', 1, {}]
], listener.events
end
end
end
end