Module: RSMP::Notifier

Includes:
Inspect
Included in:
Proxy
Defined in:
lib/rsmp/collect/notifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspect

#inspector

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



5
6
7
# File 'lib/rsmp/collect/notifier.rb', line 5

def listeners
  @listeners
end

Instance Method Details

#add_listener(listener) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
# File 'lib/rsmp/collect/notifier.rb', line 38

def add_listener listener
  raise ArgumentError unless listener
  @listeners << listener unless @listeners.include? listener
end

#clear_deferred_notify(&block) ⇒ Object



19
20
21
# File 'lib/rsmp/collect/notifier.rb', line 19

def clear_deferred_notify &block
  @notify_queue = []
end

#deferred_notify(&block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rsmp/collect/notifier.rb', line 23

def deferred_notify &block
  was, @defer_notify = @defer_notify, true
  yield
  dequeue_notify
ensure
  @defer_notify = was
  @notify_queue = []
end

#dequeue_notifyObject



32
33
34
35
36
# File 'lib/rsmp/collect/notifier.rb', line 32

def dequeue_notify
  @notify_queue.each { |message| notify_without_defer message }
ensure
  @notify_queue = []
end

#distribute_error(error, options = {}) ⇒ Object



61
62
63
# File 'lib/rsmp/collect/notifier.rb', line 61

def distribute_error error, options={}
  @listeners.each { |listener| listener.notify_error error, options }
end

#initialize_distributorObject



13
14
15
16
17
# File 'lib/rsmp/collect/notifier.rb', line 13

def initialize_distributor
  @listeners = []
  @defer_notify = false
  @notify_queue = []
end

#inspectObject



9
10
11
# File 'lib/rsmp/collect/notifier.rb', line 9

def inspect
  "#<#{self.class.name}:#{self.object_id}, #{inspector(:@listeners)}>"
end

#notify(message) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
# File 'lib/rsmp/collect/notifier.rb', line 48

def notify message
  raise ArgumentError unless message
  if @defer_notify
    @notify_queue << message
  else
    notify_without_defer message
  end
end

#notify_without_defer(message) ⇒ Object



57
58
59
# File 'lib/rsmp/collect/notifier.rb', line 57

def notify_without_defer message
  @listeners.each { |listener| listener.notify message }
end

#remove_listener(listener) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
# File 'lib/rsmp/collect/notifier.rb', line 43

def remove_listener listener
  raise ArgumentError unless listener
  @listeners.delete listener
end