123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::Collection::CopyOnWriteObserverSet Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Concurrent::Synchronization::LockableObject
Defined in: lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb

Overview

A thread safe observer set implemented using copy-on-write approach: every time an observer is added or removed the whole internal data structure is duplicated and replaced with a new one.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Synchronization::LockableObject - Inherited

Constructor Details

.newCopyOnWriteObserverSet

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 13

def initialize
  super()
  synchronize { ns_initialize }
end

Instance Attribute Details

#observers (rw, private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 94

def observers
  synchronize { @observers }
end

#observers=(new_set) (rw, private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 98

def observers=(new_set)
  synchronize { @observers = new_set }
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

[ GitHub ]

  
# File 'lib/concurrent-ruby/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

#clear_observers_and_return_old (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 102

def clear_observers_and_return_old
  synchronize do
    old_observers = @observers
    @observers = {}
    old_observers
  end
end

#count_observersInteger

Return the number of observers associated with this object.

Returns:

  • (Integer)

    the observers count

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 56

def count_observers
  observers.count
end

#delete_observer(observer) ⇒ Object

Remove observer as an observer on this object so that it will no longer receive notifications.

Parameters:

  • observer (Object)

    the observer to remove

Returns:

  • (Object)

    the deleted observer

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 40

def delete_observer(observer)
  synchronize do
    new_observers = @observers.dup
    new_observers.delete(observer)
    @observers = new_observers
    observer
  end
end

#delete_observersObservable

Remove all observers associated with this object.

Returns:

  • (Observable)

    self

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 50

def delete_observers
  self.observers = {}
  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:

  • (CopyOnWriteObserverSet)

    self

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 72

def notify_and_delete_observers(*args, &block)
  old = clear_observers_and_return_old
  notify_to(old, *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:

  • (CopyOnWriteObserverSet)

    self

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 63

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

#notify_to(observers, *args) (private)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_write_observer_set.rb', line 86

def notify_to(observers, *args)
  raise ArgumentError.new('cannot give arguments and a block') if block_given? && !args.empty?
  observers.each do |observer, function|
    args = yield if block_given?
    observer.send(function, *args)
  end
end