class Garcon::CopyOnNotifyObserverSet

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

Public Class Methods

new() click to toggle source
# File lib/garcon/task/copy_on_notify_observer_set.rb, line 28
def initialize
  @mutex = Mutex.new
  @observers = {}
end

Public Instance Methods

add_observer(observer = nil, func = :update, &block) click to toggle source

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

@param [Object] observer

The observer to add

@param [Symbol] func

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

@return [Object]

the added observer
# File lib/garcon/task/copy_on_notify_observer_set.rb, line 45
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.new('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_observers() click to toggle source

@return [Integer]

the observers count
# File lib/garcon/task/copy_on_notify_observer_set.rb, line 91
def count_observers
  @mutex.lock
  result = @observers.count
  @mutex.unlock

  result
end
delete_observer(observer) click to toggle source

@param [Object] observer

the observer to remove

@return [Object]

the deleted observer
# File lib/garcon/task/copy_on_notify_observer_set.rb, line 71
def delete_observer(observer)
  @mutex.lock
  @observers.delete(observer)
  @mutex.unlock

  observer
end
delete_observers() click to toggle source

Deletes all observers @return [CopyOnWriteObserverSet] self

# File lib/garcon/task/copy_on_notify_observer_set.rb, line 81
def delete_observers
  @mutex.lock
  @observers.clear
  @mutex.unlock

  self
end
notify_and_delete_observers(*args, &block) click to toggle source

Notifies all registered observers with optional args and deletes them.

@param [Object] args

arguments to be passed to each observer

@return [CopyOnWriteObserverSet] self

# File lib/garcon/task/copy_on_notify_observer_set.rb, line 118
def notify_and_delete_observers(*args, &block)
  observers = duplicate_and_clear_observers
  notify_to(observers, *args, &block)

  self
end
notify_observers(*args, &block) click to toggle source

Notifies all registered observers with optional args

@param [Object] args

arguments to be passed to each observer

@return [CopyOnWriteObserverSet] self

# File lib/garcon/task/copy_on_notify_observer_set.rb, line 105
def notify_observers(*args, &block)
  observers = duplicate_observers
  notify_to(observers, *args, &block)

  self
end