blob: a9aa5464e9caed8a0337a6787261078476a8afb3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
module ActiveSupport
module Notifications
# This is a default queue implementation that ships with Notifications.
# It just pushes events to all registered log subscribers.
class Fanout
def initialize
@subscribers = []
@listeners_for = {}
end
def subscribe(pattern = nil, block = Proc.new)
subscriber = Subscriber.new(pattern, block).tap do |s|
@subscribers << s
end
@listeners_for.clear
subscriber
end
def unsubscribe(subscriber)
@subscribers.reject! {|s| s.matches?(subscriber)}
@listeners_for.clear
end
def publish(name, *args)
listeners_for(name).each { |s| s.publish(name, *args) }
end
def listeners_for(name)
@listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) }
end
def listening?(name)
listeners_for(name).any?
end
# This is a sync queue, so there is no waiting.
def wait
end
class Subscriber #:nodoc:
def initialize(pattern, delegate)
@pattern = pattern
@delegate = delegate
end
def publish(message, *args)
@delegate.call(message, *args)
end
def subscribed_to?(name)
!@pattern || @pattern === name.to_s
end
def matches?(subscriber_or_name)
self === subscriber_or_name ||
@pattern && @pattern === subscriber_or_name
end
end
end
end
end
|