Synchronization::Object
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
@api private
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
@param [Object] observer the observer to add @param [Symbol] func the function to call on the observer during notification.
Default is :update
@return [Object] the added observer
# 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
@return [Integer] the observers count
# File lib/concurrent/collection/copy_on_notify_observer_set.rb, line 63 def count_observers synchronize { @observers.count } end
@param [Object] observer the observer to remove @return [Object] the deleted observer
# File lib/concurrent/collection/copy_on_notify_observer_set.rb, line 46 def delete_observer(observer) synchronize do @observers.delete(observer) observer end end
Deletes all observers @return [CopyOnWriteObserverSet] self
# File lib/concurrent/collection/copy_on_notify_observer_set.rb, line 55 def delete_observers synchronize do @observers.clear self end end
Notifies all registered observers with optional args and deletes them.
@param [Object] args arguments to be passed to each observer @return [CopyOnWriteObserverSet] self
# 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
Notifies all registered observers with optional args @param [Object] args arguments to be passed to each observer @return [CopyOnWriteObserverSet] self
# 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
Generated with the Darkfish Rdoc Generator 2.