Class: Celluloid::Notifications::Fanout

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

Constant Summary

Constants included from Celluloid

BARE_OBJECT_WARNING_MESSAGE, DeadActorError, Error, LINKING_TIMEOUT, Celluloid::NotActorError, OWNER_IVAR, TIMER_QUANTUM, TimeoutError, VERSION

Instance Method Summary collapse

Methods included from Celluloid

#abort, actor?, #after, #async, boot, #call_chain_id, cores, #current_actor, #defer, detect_recursion, #every, exception_handler, #exclusive, #exclusive?, #future, included, init, #link, #linked_to?, #links, mailbox, #monitor, #monitoring?, #receive, register_shutdown, running?, shutdown, #signal, #sleep, stack_dump, start, suspend, #tasks, #terminate, #timeout, #unlink, #unmonitor, uuid, version, #wait

Constructor Details

#initializeFanout

Returns a new instance of Fanout.


23
24
25
26
# File 'lib/celluloid/notifications.rb', line 23

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

Instance Method Details

#listeners_for(pattern) ⇒ Object


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

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

#listening?(pattern) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#prune(actor, reason = nil) ⇒ Object


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

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

#publish(pattern, *args) ⇒ Object


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

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

#subscribe(actor, pattern, method) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/celluloid/notifications.rb', line 28

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


37
38
39
40
# File 'lib/celluloid/notifications.rb', line 37

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