Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit 6fe36ba585d7d60d127b3ba75f923a48c132b3bb 1 parent a513cc1
Eric Saxby sax authored
6 activesupport/lib/active_support/notifications/fanout.rb
@@ -59,10 +59,10 @@ def wait
59 59
60 60 module Subscribers # :nodoc:
61 61 def self.new(pattern, listener)
62   - if listener.respond_to?(:call)
63   - subscriber = Timed.new pattern, listener
64   - else
  62 + if listener.respond_to?(:start) and listener.respond_to?(:finish)
65 63 subscriber = Evented.new pattern, listener
  64 + else
  65 + subscriber = Timed.new pattern, listener
66 66 end
67 67
68 68 unless pattern
20 activesupport/test/notifications/evented_notification_test.rb
@@ -19,6 +19,12 @@ def finish(name, id, payload)
19 19 end
20 20 end
21 21
  22 + class ListenerWithTimedSupport < Listener
  23 + def call(name, start, finish, id, payload)
  24 + @events << [:call, name, start, finish, id, payload]
  25 + end
  26 + end
  27 +
22 28 def test_evented_listener
23 29 notifier = Fanout.new
24 30 listener = Listener.new
@@ -62,6 +68,20 @@ def test_listen_to_everything
62 68 [:finish, 'hello', 1, {}],
63 69 ], listener.events
64 70 end
  71 +
  72 + def test_evented_listener_priority
  73 + notifier = Fanout.new
  74 + listener = ListenerWithTimedSupport.new
  75 + notifier.subscribe 'hi', listener
  76 +
  77 + notifier.start 'hi', 1, {}
  78 + notifier.finish 'hi', 1, {}
  79 +
  80 + assert_equal [
  81 + [:start, 'hi', 1, {}],
  82 + [:finish, 'hi', 1, {}]
  83 + ], listener.events
  84 + end
65 85 end
66 86 end
67 87 end

0 comments on commit 6fe36ba

Please sign in to comment.
Something went wrong with that request. Please try again.