Skip to content

Commit 38454bb

Browse files
committed
Merge pull request rails#15464 from tgxworld/improve_notifications_logic
Improvements to ActiveSupport::Notifications.
2 parents 14a1cb7 + a8439bc commit 38454bb

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

activesupport/lib/active_support/notifications.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ module ActiveSupport
141141
#
142142
# ActiveSupport::Notifications.unsubscribe(subscriber)
143143
#
144+
# You can also unsubscribe by passing the name of the subscriber object. Note
145+
# that this will unsubscribe all subscriptions with the given name:
146+
#
147+
# ActiveSupport::Notifications.unsubscribe("render")
148+
#
144149
# == Default Queue
145150
#
146151
# Notifications ships with a queue implementation that consumes and publishes events
@@ -173,8 +178,8 @@ def subscribed(callback, *args, &block)
173178
unsubscribe(subscriber)
174179
end
175180

176-
def unsubscribe(args)
177-
notifier.unsubscribe(args)
181+
def unsubscribe(subscriber_or_name)
182+
notifier.unsubscribe(subscriber_or_name)
178183
end
179184

180185
def instrumenter

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ def subscribe(pattern = nil, block = Proc.new)
2525
subscriber
2626
end
2727

28-
def unsubscribe(subscriber)
28+
def unsubscribe(subscriber_or_name)
2929
synchronize do
30-
@subscribers.reject! { |s| s.matches?(subscriber) }
30+
case subscriber_or_name
31+
when String
32+
@subscribers.reject! { |s| s.matches?(subscriber_or_name) }
33+
else
34+
@subscribers.delete(subscriber_or_name)
35+
end
36+
3137
@listeners_for.clear
3238
end
3339
end
@@ -97,12 +103,11 @@ def finish(name, id, payload)
97103
end
98104

99105
def subscribed_to?(name)
100-
@pattern === name.to_s
106+
@pattern === name
101107
end
102108

103-
def matches?(subscriber_or_name)
104-
self === subscriber_or_name ||
105-
@pattern && @pattern === subscriber_or_name
109+
def matches?(name)
110+
@pattern && @pattern === name
106111
end
107112
end
108113

0 commit comments

Comments
 (0)