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.
@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_write_observer_set.rb, line 25 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
@return [Integer] the observers count
# File lib/concurrent/collection/copy_on_write_observer_set.rb, line 64 def count_observers observers.count end
@param [Object] observer the observer to remove @return [Object] the deleted observer
# File lib/concurrent/collection/copy_on_write_observer_set.rb, line 47 def delete_observer(observer) synchronize do new_observers = @observers.dup new_observers.delete(observer) @observers = new_observers observer end end
Deletes all observers @return [CopyOnWriteObserverSet] self
# File lib/concurrent/collection/copy_on_write_observer_set.rb, line 58 def delete_observers self.observers = {} self 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_write_observer_set.rb, line 80 def notify_and_delete_observers(*args, &block) old = clear_observers_and_return_old notify_to(old, *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_write_observer_set.rb, line 71 def notify_observers(*args, &block) notify_to(observers, *args, &block) self end
Generated with the Darkfish Rdoc Generator 2.