Class: Celluloid::Notifications::Fanout

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/celluloid/notifications.rb

Constant Summary

Constants included from Celluloid

CPUCounter, Links, Logger, Properties, Registry, StackDump, ThreadHandle, UUID

Instance Method Summary collapse

Methods included from Celluloid

publish

Constructor Details

#initializeFanout

Returns a new instance of Fanout.



28
29
30
31
# File 'lib/celluloid/notifications.rb', line 28

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

Instance Method Details

#listeners_for(pattern) ⇒ Object



51
52
53
# File 'lib/celluloid/notifications.rb', line 51

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

#listening?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/celluloid/notifications.rb', line 55

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

#prune(actor, _reason = nil) ⇒ Object



59
60
61
62
# File 'lib/celluloid/notifications.rb', line 59

def prune(actor, _reason=nil)
  @subscribers.reject! { |s| s.actor == actor }
  @listeners_for.clear
end

#publish(pattern, *args) ⇒ Object



47
48
49
# File 'lib/celluloid/notifications.rb', line 47

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

#subscribe(actor, pattern, method) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/celluloid/notifications.rb', line 33

def subscribe(actor, pattern, method)
  subscriber = Subscriber.new(actor, pattern, method).tap do |s|
    @subscribers << s
  end
  link actor
  @listeners_for.clear
  subscriber
end

#unsubscribe(subscriber) ⇒ Object



42
43
44
45
# File 'lib/celluloid/notifications.rb', line 42

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