Class List Method List File List

# Class: Concurrent::Collection::CopyOnNotifyObserverSet Private

Inherits:
  Synchronization::LockableObject
  - Object
  - Synchronization::LockableObject
  - Concurrent::Collection::CopyOnNotifyObserverSet
  show all
Defined in:
  lib/concurrent/collection/copy_on_notify_observer_set.rb

## Overview

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

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)

- - (Object) add_observer(observer = nil, func = :update, &block) private

  Adds an observer to this set.

- - (Integer) count_observers private

  Return the number of observers associated with this object.

- - (Object) delete_observer(observer) private

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

- - (Observable) delete_observers private

  Remove all observers associated with this object.

- - (CopyOnNotifyObserverSet) initialize constructor private

  A new instance of CopyOnNotifyObserverSet.

- - (CopyOnWriteObserverSet) notify_and_delete_observers(*args, &block) private

  Notifies all registered observers with optional args and deletes them.

- - (CopyOnWriteObserverSet) notify_observers(*args, &block) private

  Notifies all registered observers with optional args.

## Constructor Details

### - (`CopyOnNotifyObserverSet`) initialize

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.

Returns a new instance of CopyOnNotifyObserverSet

    14
    15
    16
    17

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 14

    def initialize
      super()
      synchronize { ns_initialize }
    end

## Instance Method Details

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

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

    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 20

    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
        @observers[observer] = func
        observer
      end
    end

### - (`Integer`) count_observers

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.

Return the number of observers associated with this object.

Returns:

- (`Integer`) —

  the observers count

    55
    56
    57

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 55

    def count_observers
      synchronize { @observers.count }
    end

### - (`Object`) delete_observer(observer)

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.

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

    39
    40
    41
    42
    43
    44

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 39

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

### - (`Observable`) delete_observers

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.

Remove all observers associated with this object.

Returns:

- (`Observable`) —

  self

    47
    48
    49
    50
    51
    52

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 47

    def delete_observers
      synchronize do
        @observers.clear
        self
      end
    end

### - (`CopyOnWriteObserverSet`) notify_and_delete_observers(*args, &block)

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.

Notifies all registered observers with optional args and deletes them.

Parameters:

- args (`Object`) —

  arguments to be passed to each observer

Returns:

- (`CopyOnWriteObserverSet`) —

  self

    72
    73
    74
    75
    76

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 72

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

### - (`CopyOnWriteObserverSet`) notify_observers(*args, &block)

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.

Notifies all registered observers with optional args

Parameters:

- args (`Object`) —

  arguments to be passed to each observer

Returns:

- (`CopyOnWriteObserverSet`) —

  self

    62
    63
    64
    65
    66

    # File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 62

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