Method: Concurrent::Collection::CopyOnWriteObserverSet#add_observer

Defined in:
lib/concurrent/collection/copy_on_write_observer_set.rb

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/concurrent/collection/copy_on_write_observer_set.rb', line 19

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

  synchronize do
    new_observers = @observers.dup
    new_observers[observer] = func
    @observers = new_observers
    observer
  end
end