Class: Concurrent::Collection::CopyOnNotifyObserverSet Private
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_notify_observer_set.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
Class Method Summary
- .new ⇒ CopyOnNotifyObserverSet constructor Internal use only
Instance Method Summary
-
#add_observer(observer = nil, func = :update, &block) ⇒ Object
Internal use only
Adds an observer to this set.
-
#count_observers ⇒ Integer
Internal use only
Return the number of observers associated with this object.
-
#delete_observer(observer) ⇒ Object
Internal use only
Remove
observer
as an observer on this object so that it will no longer receive notifications. -
#delete_observers ⇒ Observable
Internal use only
Remove all observers associated with this object.
-
#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Internal use only
Notifies all registered observers with optional args and deletes them.
-
#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Internal use only
Notifies all registered observers with optional args.
- #duplicate_and_clear_observers private Internal use only
- #duplicate_observers private Internal use only
- #notify_to(observers, *args) private Internal use only
Synchronization::LockableObject
- Inherited
Constructor Details
.new ⇒ CopyOnNotifyObserverSet
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 14
def initialize super() synchronize { ns_initialize } 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.
# File 'lib/concurrent-ruby/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
#count_observers ⇒ Integer
Return the number of observers associated with this object.
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 55
def count_observers synchronize { @observers.count } end
#delete_observer(observer) ⇒ Object
Remove observer
as an observer on this object so that it will no longer receive notifications.
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 39
def delete_observer(observer) synchronize do @observers.delete(observer) observer end end
#delete_observers ⇒ Observable
Remove all observers associated with this object.
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 47
def delete_observers synchronize do @observers.clear self end end
#duplicate_and_clear_observers (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 86
def duplicate_and_clear_observers synchronize do observers = @observers.dup @observers.clear observers end end
#duplicate_observers (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 94
def duplicate_observers synchronize { @observers.dup } end
#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Notifies all registered observers with optional args and deletes them.
# File 'lib/concurrent-ruby/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
#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Notifies all registered observers with optional args
# File 'lib/concurrent-ruby/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
#notify_to(observers, *args) (private)
# File 'lib/concurrent-ruby/concurrent/collection/copy_on_notify_observer_set.rb', line 98
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