Class: AS::Notifications::Fanout

Inherits:
Object
  • Object
show all
Includes:
Mutex_m
Defined in:
lib/as/notifications/fanout.rb

Overview

This is a default queue implementation that ships with Notifications. It just pushes events to all registered log subscribers.

This class is thread safe. All methods are reentrant.

Defined Under Namespace

Modules: Subscribers

Instance Method Summary collapse

Constructor Details

#initializeFanout

Returns a new instance of Fanout.



12
13
14
15
16
# File 'lib/as/notifications/fanout.rb', line 12

def initialize
  @subscribers = []
  @listeners_for = {}
  super
end

Instance Method Details

#finish(name, id, payload) ⇒ Object



38
39
40
# File 'lib/as/notifications/fanout.rb', line 38

def finish(name, id, payload)
  listeners_for(name).each { |s| s.finish(name, id, payload) }
end

#listeners_for(name) ⇒ Object



46
47
48
49
50
# File 'lib/as/notifications/fanout.rb', line 46

def listeners_for(name)
  synchronize do
    @listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) }
  end
end

#listening?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/as/notifications/fanout.rb', line 52

def listening?(name)
  listeners_for(name).any?
end

#publish(name, *args) ⇒ Object



42
43
44
# File 'lib/as/notifications/fanout.rb', line 42

def publish(name, *args)
  listeners_for(name).each { |s| s.publish(name, *args) }
end

#start(name, id, payload) ⇒ Object



34
35
36
# File 'lib/as/notifications/fanout.rb', line 34

def start(name, id, payload)
  listeners_for(name).each { |s| s.start(name, id, payload) }
end

#subscribe(pattern = nil, callable = nil, &block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/as/notifications/fanout.rb', line 18

def subscribe(pattern = nil, callable = nil, &block)
  subscriber = Subscribers.new pattern, callable || block
  synchronize do
    @subscribers << subscriber
    @listeners_for.clear
  end
  subscriber
end

#unsubscribe(subscriber) ⇒ Object



27
28
29
30
31
32
# File 'lib/as/notifications/fanout.rb', line 27

def unsubscribe(subscriber)
  synchronize do
    @subscribers.reject! { |s| s.matches?(subscriber) }
    @listeners_for.clear
  end
end

#waitObject

This is a sync queue, so there is no waiting.



57
58
# File 'lib/as/notifications/fanout.rb', line 57

def wait
end