Class: Concurrent::Collection::CopyOnWriteObserverSet Private
- Inherits:
-
Synchronization::LockableObject
- Object
- Synchronization::LockableObject
- Concurrent::Collection::CopyOnWriteObserverSet
- Defined in:
- lib/concurrent/collection/copy_on_write_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-write approach: every time an observer is added or removed the whole internal data structure is duplicated and replaced with a new one.
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.
-
- (CopyOnWriteObserverSet) initialize
constructor
private
A new instance of CopyOnWriteObserverSet.
-
- (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
- (CopyOnWriteObserverSet) 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 CopyOnWriteObserverSet
13 14 15 16 |
# File 'lib/concurrent/collection/copy_on_write_observer_set.rb', line 13 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.
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 |
- (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.
56 57 58 |
# File 'lib/concurrent/collection/copy_on_write_observer_set.rb', line 56 def count_observers 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.
40 41 42 43 44 45 46 47 |
# File 'lib/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 |
- (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.
50 51 52 53 |
# File 'lib/concurrent/collection/copy_on_write_observer_set.rb', line 50 def delete_observers self.observers = {} self 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.
72 73 74 75 76 |
# File 'lib/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 |
- (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
63 64 65 66 |
# File 'lib/concurrent/collection/copy_on_write_observer_set.rb', line 63 def notify_observers(*args, &block) notify_to(observers, *args, &block) self end |