Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
94f8e8c8
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
94f8e8c8
编写于
2月 07, 2019
作者:
Z
zvkemp
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use a proxy matcher for AS::N fanout
上级
b67d5c6d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
100 addition
and
3 deletion
+100
-3
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+4
-0
activesupport/lib/active_support/notifications.rb
activesupport/lib/active_support/notifications.rb
+9
-0
activesupport/lib/active_support/notifications/fanout.rb
activesupport/lib/active_support/notifications/fanout.rb
+35
-3
activesupport/test/notifications/evented_notification_test.rb
...vesupport/test/notifications/evented_notification_test.rb
+33
-0
activesupport/test/notifications_test.rb
activesupport/test/notifications_test.rb
+19
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
94f8e8c8
*
Revise
`ActiveSupport::Notifications.unsubscribe`
to correctly handle Regex or other multiple-pattern subscribers.
*Zach Kemp*
*
Add
`before_reset`
callback to
`CurrentAttributes`
and define
`after_reset`
as an alias of
`resets`
for symmetry.
*
Add
`before_reset`
callback to
`CurrentAttributes`
and define
`after_reset`
as an alias of
`resets`
for symmetry.
*Rosa Gutierrez*
*Rosa Gutierrez*
...
...
activesupport/lib/active_support/notifications.rb
浏览文件 @
94f8e8c8
...
@@ -153,6 +153,15 @@ module ActiveSupport
...
@@ -153,6 +153,15 @@ module ActiveSupport
#
#
# ActiveSupport::Notifications.unsubscribe("render")
# ActiveSupport::Notifications.unsubscribe("render")
#
#
# Subscribers using a regexp or other pattern-matching object will remain subscribed
# to all events that match their original pattern, unless those events match a string
# passed to `unsubscribe`:
#
# subscriber = ActiveSupport::Notifications.subscribe(/render/) { }
# ActiveSupport::Notifications.unsubscribe('render_template.action_view')
# subscriber.matches?('render_template.action_view') # => false
# subscriber.matches?('render_partial.action_view') # => true
#
# == Default Queue
# == Default Queue
#
#
# Notifications ships with a queue implementation that consumes and publishes events
# Notifications ships with a queue implementation that consumes and publishes events
...
...
activesupport/lib/active_support/notifications/fanout.rb
浏览文件 @
94f8e8c8
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
require
"mutex_m"
require
"mutex_m"
require
"concurrent/map"
require
"concurrent/map"
require
"set"
module
ActiveSupport
module
ActiveSupport
module
Notifications
module
Notifications
...
@@ -39,6 +40,7 @@ def unsubscribe(subscriber_or_name)
...
@@ -39,6 +40,7 @@ def unsubscribe(subscriber_or_name)
when
String
when
String
@string_subscribers
[
subscriber_or_name
].
clear
@string_subscribers
[
subscriber_or_name
].
clear
@listeners_for
.
delete
(
subscriber_or_name
)
@listeners_for
.
delete
(
subscriber_or_name
)
@other_subscribers
.
each
{
|
sub
|
sub
.
unsubscribe!
(
subscriber_or_name
)
}
else
else
pattern
=
subscriber_or_name
.
try
(
:pattern
)
pattern
=
subscriber_or_name
.
try
(
:pattern
)
if
String
===
pattern
if
String
===
pattern
...
@@ -113,11 +115,33 @@ def self.wrap_all(pattern, subscriber)
...
@@ -113,11 +115,33 @@ def self.wrap_all(pattern, subscriber)
end
end
end
end
class
Matcher
#:nodoc:
attr_reader
:pattern
,
:exclusions
def
self
.
wrap
(
pattern
)
return
pattern
if
String
===
pattern
new
(
pattern
)
end
def
initialize
(
pattern
)
@pattern
=
pattern
@exclusions
=
Set
.
new
end
def
unsubscribe!
(
name
)
exclusions
<<
-
name
if
pattern
===
name
end
def
===
(
name
)
pattern
===
name
&&
!
exclusions
.
include?
(
name
)
end
end
class
Evented
#:nodoc:
class
Evented
#:nodoc:
attr_reader
:pattern
attr_reader
:pattern
def
initialize
(
pattern
,
delegate
)
def
initialize
(
pattern
,
delegate
)
@pattern
=
pattern
@pattern
=
Matcher
.
wrap
(
pattern
)
@delegate
=
delegate
@delegate
=
delegate
@can_publish
=
delegate
.
respond_to?
(
:publish
)
@can_publish
=
delegate
.
respond_to?
(
:publish
)
end
end
...
@@ -137,11 +161,15 @@ def finish(name, id, payload)
...
@@ -137,11 +161,15 @@ def finish(name, id, payload)
end
end
def
subscribed_to?
(
name
)
def
subscribed_to?
(
name
)
@
pattern
===
name
pattern
===
name
end
end
def
matches?
(
name
)
def
matches?
(
name
)
@pattern
&&
@pattern
===
name
pattern
&&
pattern
===
name
end
def
unsubscribe!
(
name
)
pattern
.
unsubscribe!
(
name
)
end
end
end
end
...
@@ -204,6 +232,10 @@ def subscribed_to?(name)
...
@@ -204,6 +232,10 @@ def subscribed_to?(name)
true
true
end
end
def
unsubscribe!
(
*
)
false
end
alias
:matches?
:===
alias
:matches?
:===
end
end
end
end
...
...
activesupport/test/notifications/evented_notification_test.rb
浏览文件 @
94f8e8c8
...
@@ -84,6 +84,39 @@ def test_evented_listener_priority
...
@@ -84,6 +84,39 @@ def test_evented_listener_priority
[
:finish
,
"hi"
,
1
,
{}]
[
:finish
,
"hi"
,
1
,
{}]
],
listener
.
events
],
listener
.
events
end
end
def
test_listen_to_regexp
notifier
=
Fanout
.
new
listener
=
Listener
.
new
notifier
.
subscribe
(
/[a-z]*.world/
,
listener
)
notifier
.
start
(
"hi.world"
,
1
,
{})
notifier
.
finish
(
"hi.world"
,
2
,
{})
notifier
.
start
(
"hello.world"
,
1
,
{})
notifier
.
finish
(
"hello.world"
,
2
,
{})
assert_equal
[
[
:start
,
"hi.world"
,
1
,
{}],
[
:finish
,
"hi.world"
,
2
,
{}],
[
:start
,
"hello.world"
,
1
,
{}],
[
:finish
,
"hello.world"
,
2
,
{}]
],
listener
.
events
end
def
test_listen_to_regexp_with_exclusions
notifier
=
Fanout
.
new
listener
=
Listener
.
new
notifier
.
subscribe
(
/[a-z]*.world/
,
listener
)
notifier
.
unsubscribe
(
"hi.world"
)
notifier
.
start
(
"hi.world"
,
1
,
{})
notifier
.
finish
(
"hi.world"
,
2
,
{})
notifier
.
start
(
"hello.world"
,
1
,
{})
notifier
.
finish
(
"hello.world"
,
2
,
{})
assert_equal
[
[
:start
,
"hello.world"
,
1
,
{}],
[
:finish
,
"hello.world"
,
2
,
{}]
],
listener
.
events
end
end
end
end
end
end
end
activesupport/test/notifications_test.rb
浏览文件 @
94f8e8c8
...
@@ -128,6 +128,25 @@ def test_unsubscribing_by_name_leaves_the_other_subscriptions
...
@@ -128,6 +128,25 @@ def test_unsubscribing_by_name_leaves_the_other_subscriptions
assert_equal
[[
"named.subscription"
,
:foo
],
[
"named.subscription"
,
:foo
]],
@events
assert_equal
[[
"named.subscription"
,
:foo
],
[
"named.subscription"
,
:foo
]],
@events
end
end
def
test_unsubscribing_by_name_leaves_regexp_matched_subscriptions
@matched_events
=
[]
@notifier
.
subscribe
(
/subscription/
)
{
|*
args
|
@matched_events
<<
event
(
*
args
)
}
@notifier
.
publish
(
"named.subscription"
,
:before
)
@notifier
.
wait
[
@events
,
@named_events
,
@matched_events
].
each
do
|
collector
|
assert_includes
(
collector
,
[
"named.subscription"
,
:before
])
end
@notifier
.
unsubscribe
(
"named.subscription"
)
@notifier
.
publish
(
"named.subscription"
,
:after
)
@notifier
.
publish
(
"other.subscription"
,
:after
)
@notifier
.
wait
assert_includes
(
@events
,
[
"named.subscription"
,
:after
])
assert_includes
(
@events
,
[
"other.subscription"
,
:after
])
assert_includes
(
@matched_events
,
[
"other.subscription"
,
:after
])
assert_not_includes
(
@matched_events
,
[
"named.subscription"
,
:after
])
assert_not_includes
(
@named_events
,
[
"named.subscription"
,
:after
])
end
private
private
def
event
(
*
args
)
def
event
(
*
args
)
args
args
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录