Class: Concurrent::CopyOnNotifyObserverSet
- Inherits:
-
Object
- Object
- Concurrent::CopyOnNotifyObserverSet
- Defined in:
- lib/concurrent/atomic/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
Instance Method Summary collapse
-
#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.
-
#count_observers ⇒ Integer
The observers count.
-
#delete_observer(observer) ⇒ Object
The deleted observer.
-
#delete_observers ⇒ CopyOnWriteObserverSet
Deletes all observers.
-
#initialize ⇒ CopyOnNotifyObserverSet
constructor
A new instance of CopyOnNotifyObserverSet.
-
#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Notifies all registered observers with optional args and deletes them.
-
#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Notifies all registered observers with optional args.
Constructor Details
#initialize ⇒ CopyOnNotifyObserverSet
Returns a new instance of CopyOnNotifyObserverSet.
9 10 11 12 |
# File 'lib/concurrent/atomic/copy_on_notify_observer_set.rb', line 9 def initialize @mutex = Mutex.new @observers = {} 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/concurrent/atomic/copy_on_notify_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 begin @mutex.lock @observers[observer] = func ensure @mutex.unlock end observer end |
#count_observers ⇒ Integer
Returns the observers count.
62 63 64 65 66 67 68 |
# File 'lib/concurrent/atomic/copy_on_notify_observer_set.rb', line 62 def count_observers @mutex.lock result = @observers.count @mutex.unlock result end |
#delete_observer(observer) ⇒ Object
Returns the deleted observer.
43 44 45 46 47 48 49 |
# File 'lib/concurrent/atomic/copy_on_notify_observer_set.rb', line 43 def delete_observer(observer) @mutex.lock @observers.delete(observer) @mutex.unlock observer end |
#delete_observers ⇒ CopyOnWriteObserverSet
Deletes all observers
53 54 55 56 57 58 59 |
# File 'lib/concurrent/atomic/copy_on_notify_observer_set.rb', line 53 def delete_observers @mutex.lock @observers.clear @mutex.unlock self end |
#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet
Notifies all registered observers with optional args and deletes them.
84 85 86 87 88 89 |
# File 'lib/concurrent/atomic/copy_on_notify_observer_set.rb', line 84 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
73 74 75 76 77 78 |
# File 'lib/concurrent/atomic/copy_on_notify_observer_set.rb', line 73 def notify_observers(*args, &block) observers = duplicate_observers notify_to(observers, *args, &block) self end |