Class: Concurrent::Collection::CopyOnNotifyObserverSet Private
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::Collection::CopyOnNotifyObserverSet
- 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
-
#add_observer(observer = nil, func = :update, &block) ⇒ Object
private
Adds an observer to this set.
-
#count_observers ⇒ Integer
private
The observers count.
-
#delete_observer(observer) ⇒ Object
private
The deleted observer.
-
#delete_observers ⇒ CopyOnWriteObserverSet
private
Deletes all observers.
-
#initialize ⇒ CopyOnNotifyObserverSet
constructor
private
A new instance of CopyOnNotifyObserverSet.
-
#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet
private
Notifies all registered observers with optional args and deletes them.
-
#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet
private
Notifies all registered observers with optional args.
Methods inherited from Synchronization::Object
Constructor Details
#initialize ⇒ CopyOnNotifyObserverSet
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
#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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 26 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
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 the observers count.
63 64 65 |
# File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 63 def count_observers synchronize { @observers.count } end |
#delete_observer(observer) ⇒ 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.
Returns the deleted observer.
46 47 48 49 50 51 |
# File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 46 def delete_observer(observer) synchronize do @observers.delete(observer) observer end end |
#delete_observers ⇒ CopyOnWriteObserverSet
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.
Deletes all observers
55 56 57 58 59 60 |
# File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 55 def delete_observers synchronize do @observers.clear self end end |
#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet
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.
80 81 82 83 84 |
# File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 80 def notify_and_delete_observers(*args, &block) observers = duplicate_and_clear_observers notify_to(observers, *args, &block) self end |
#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet
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
70 71 72 73 74 |
# File 'lib/concurrent/collection/copy_on_notify_observer_set.rb', line 70 def notify_observers(*args, &block) observers = duplicate_observers notify_to(observers, *args, &block) self end |