Class: Garcon::CopyOnNotifyObserverSet

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodie/observable.rb

Overview

A thread safe observer set implemented using copy-on-read approach. Observers are added and removed from a thread safe collection; every time a notification is required the internal data structure is copied to prevent concurrency issues

Instance Method Summary collapse

Constructor Details

#initializeCopyOnNotifyObserverSet

Returns a new instance of CopyOnNotifyObserverSet.



51
52
53
54
# File 'lib/hoodie/observable.rb', line 51

def initialize
  @mutex = Mutex.new
  @observers = {}
end

Instance Method Details

#add_observer(observer = nil, func = :update, &block) ⇒ Object

Adds an observer to this set, if a block is passed, the observer will be created by this method and no other params should be passed.

Parameters:

  • observer (Object) (defaults to: nil)

    the observer to add

  • func (Symbol) (defaults to: :update)

    the function to call on the observer during notification. Default is :update

Returns:

  • (Object)

    the added observer



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hoodie/observable.rb', line 68

def add_observer(observer = nil, func = :update, &block)
  if observer.nil? && block.nil?
    raise ArgumentError, 'should pass observer as a first argument or block'
  elsif observer && block
    raise ArgumentError, 'cannot provide both an observer and a block'
  end

  if block
    observer = block
    func = :call
  end

  begin
    @mutex.lock
    @observers[observer] = func
  ensure
    @mutex.unlock
  end
  observer
end

#count_observersInteger

Returns the observers count.

Returns:

  • (Integer)

    the observers count



113
114
115
116
117
118
# File 'lib/hoodie/observable.rb', line 113

def count_observers
  @mutex.lock
  result = @observers.count
  @mutex.unlock
  result
end

#delete_observer(observer) ⇒ Object

Returns the deleted observer.

Parameters:

  • observer (Object)

    the observer to remove

Returns:

  • (Object)

    the deleted observer



93
94
95
96
97
98
# File 'lib/hoodie/observable.rb', line 93

def delete_observer(observer)
  @mutex.lock
  @observers.delete(observer)
  @mutex.unlock
  observer
end

#delete_observersCopyOnWriteObserverSet

Deletes all observers

Returns:



104
105
106
107
108
109
# File 'lib/hoodie/observable.rb', line 104

def delete_observers
  @mutex.lock
  @observers.clear
  @mutex.unlock
  self
end

#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet

Notifies all registered observers with optional args and deletes them.

Parameters:

  • args (Object)

    arguments to be passed to each observer

Returns:



140
141
142
143
144
# File 'lib/hoodie/observable.rb', line 140

def notify_and_delete_observers(*args, &block)
  observers = duplicate_and_clear_observers
  notify_to(observers, *args, &block)
  self
end

#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet

Notifies all registered observers with optional args.

Parameters:

  • args (Object)

    arguments to be passed to each observer

Returns:



127
128
129
130
131
# File 'lib/hoodie/observable.rb', line 127

def notify_observers(*args, &block)
  observers = duplicate_observers
  notify_to(observers, *args, &block)
  self
end