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:
parent
c3ca9b00e3
commit
90d6237762
3 changed files with 27 additions and 11 deletions
|
@ -231,18 +231,16 @@ module ActiveSupport
|
||||||
# ActiveSupport::Notifications.subscribe(/render/) do |event|
|
# ActiveSupport::Notifications.subscribe(/render/) do |event|
|
||||||
# @event = event
|
# @event = event
|
||||||
# end
|
# end
|
||||||
def subscribe(*args, &block)
|
def subscribe(pattern = nil, callback = nil, &block)
|
||||||
pattern, callback = *args
|
notifier.subscribe(pattern, callback, monotonic: false, &block)
|
||||||
notifier.subscribe(pattern, callback, false, &block)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def monotonic_subscribe(*args, &block)
|
def monotonic_subscribe(pattern = nil, callback = nil, &block)
|
||||||
pattern, callback = *args
|
notifier.subscribe(pattern, callback, monotonic: true, &block)
|
||||||
notifier.subscribe(pattern, callback, true, &block)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def subscribed(callback, pattern, monotonic: false, &block)
|
def subscribed(callback, pattern = nil, monotonic: false, &block)
|
||||||
subscriber = notifier.subscribe(pattern, callback, monotonic)
|
subscriber = notifier.subscribe(pattern, callback, monotonic: monotonic)
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
unsubscribe(subscriber)
|
unsubscribe(subscriber)
|
||||||
|
|
|
@ -20,8 +20,8 @@ module ActiveSupport
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def subscribe(pattern = nil, callable = nil, monotonic = false, &block)
|
def subscribe(pattern = nil, callable = nil, monotonic: false, &block)
|
||||||
subscriber = Subscribers.new(monotonic, pattern, callable || block)
|
subscriber = Subscribers.new(pattern, callable || block, monotonic)
|
||||||
synchronize do
|
synchronize do
|
||||||
if String === pattern
|
if String === pattern
|
||||||
@string_subscribers[pattern] << subscriber
|
@string_subscribers[pattern] << subscriber
|
||||||
|
@ -84,7 +84,7 @@ module ActiveSupport
|
||||||
end
|
end
|
||||||
|
|
||||||
module Subscribers # :nodoc:
|
module Subscribers # :nodoc:
|
||||||
def self.new(monotonic, pattern, listener)
|
def self.new(pattern, listener, monotonic)
|
||||||
subscriber_class = monotonic ? MonotonicTimed : Timed
|
subscriber_class = monotonic ? MonotonicTimed : Timed
|
||||||
|
|
||||||
if listener.respond_to?(:start) && listener.respond_to?(:finish)
|
if listener.respond_to?(:start) && listener.respond_to?(:finish)
|
||||||
|
|
|
@ -113,6 +113,24 @@ module Notifications
|
||||||
assert_equal expected, events
|
assert_equal expected, events
|
||||||
end
|
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
|
def test_subscribing_to_instrumentation_while_inside_it
|
||||||
# the repro requires that there are no evented subscribers for the "foo" event,
|
# the repro requires that there are no evented subscribers for the "foo" event,
|
||||||
# so we have to duplicate some of the setup code
|
# so we have to duplicate some of the setup code
|
||||||
|
|
Loading…
Reference in a new issue