1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix subscribed with no pattern to subscribe all messages

This is a regression for #36184.

And also, add new `monotonic` argument to the last of the method
signature rather than the first.
This commit is contained in:
Ryuta Kamizono 2019-06-03 05:47:10 +09:00
parent c3ca9b00e3
commit 90d6237762
3 changed files with 27 additions and 11 deletions

View file

@ -231,18 +231,16 @@ module ActiveSupport
# ActiveSupport::Notifications.subscribe(/render/) do |event|
# @event = event
# end
def subscribe(*args, &block)
pattern, callback = *args
notifier.subscribe(pattern, callback, false, &block)
def subscribe(pattern = nil, callback = nil, &block)
notifier.subscribe(pattern, callback, monotonic: false, &block)
end
def monotonic_subscribe(*args, &block)
pattern, callback = *args
notifier.subscribe(pattern, callback, true, &block)
def monotonic_subscribe(pattern = nil, callback = nil, &block)
notifier.subscribe(pattern, callback, monotonic: true, &block)
end
def subscribed(callback, pattern, monotonic: false, &block)
subscriber = notifier.subscribe(pattern, callback, monotonic)
def subscribed(callback, pattern = nil, monotonic: false, &block)
subscriber = notifier.subscribe(pattern, callback, monotonic: monotonic)
yield
ensure
unsubscribe(subscriber)

View file

@ -20,8 +20,8 @@ module ActiveSupport
super
end
def subscribe(pattern = nil, callable = nil, monotonic = false, &block)
subscriber = Subscribers.new(monotonic, pattern, callable || block)
def subscribe(pattern = nil, callable = nil, monotonic: false, &block)
subscriber = Subscribers.new(pattern, callable || block, monotonic)
synchronize do
if String === pattern
@string_subscribers[pattern] << subscriber
@ -84,7 +84,7 @@ module ActiveSupport
end
module Subscribers # :nodoc:
def self.new(monotonic, pattern, listener)
def self.new(pattern, listener, monotonic)
subscriber_class = monotonic ? MonotonicTimed : Timed
if listener.respond_to?(:start) && listener.respond_to?(:finish)

View file

@ -113,6 +113,24 @@ module Notifications
assert_equal expected, events
end
def test_subscribed_all_messages
name = "foo"
name2 = name * 2
expected = [name, name2, name]
events = []
callback = lambda { |*_| events << _.first }
ActiveSupport::Notifications.subscribed(callback) do
ActiveSupport::Notifications.instrument(name)
ActiveSupport::Notifications.instrument(name2)
ActiveSupport::Notifications.instrument(name)
end
assert_equal expected, events
ActiveSupport::Notifications.instrument(name)
assert_equal expected, events
end
def test_subscribing_to_instrumentation_while_inside_it
# the repro requires that there are no evented subscribers for the "foo" event,
# so we have to duplicate some of the setup code