Class: Concurrent::CopyOnWriteObserverSet

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/atomic/copy_on_write_observer_set.rb

Overview

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

Constructor Details

#initializeCopyOnWriteObserverSet

Returns a new instance of CopyOnWriteObserverSet.



8
9
10
11
# File 'lib/concurrent/atomic/copy_on_write_observer_set.rb', line 8

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

Parameters:

  • observer (Object) (defaults to: nil)

    the observer to add

  • func (Symbol) (defaults to: :update)

    the function to call on the observer during notification. Default is :update

Returns:

  • (Object)

    the added observer



18
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_write_observer_set.rb', line 18

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
    new_observers = @observers.dup
    new_observers[observer] = func
    @observers = new_observers
    observer
  ensure
    @mutex.unlock
  end
end

#count_observersInteger

Returns the observers count.

Returns:

  • (Integer)

    the observers count



62
63
64
# File 'lib/concurrent/atomic/copy_on_write_observer_set.rb', line 62

def count_observers
  observers.count
end

#delete_observer(observer) ⇒ Object

Returns the deleted observer.

Parameters:

  • observer (Object)

    the observer to remove

Returns:

  • (Object)

    the deleted observer



43
44
45
46
47
48
49
50
51
# File 'lib/concurrent/atomic/copy_on_write_observer_set.rb', line 43

def delete_observer(observer)
  @mutex.lock
  new_observers = @observers.dup
  new_observers.delete(observer)
  @observers = new_observers
  observer
ensure
  @mutex.unlock
end

#delete_observersCopyOnWriteObserverSet

Deletes all observers

Returns:



55
56
57
58
# File 'lib/concurrent/atomic/copy_on_write_observer_set.rb', line 55

def delete_observers
  self.observers = {}
  self
end

#notify_and_delete_observers(*args, &block) ⇒ CopyOnWriteObserverSet

Notifies all registered observers with optional args and deletes them.

Parameters:

  • args (Object)

    arguments to be passed to each observer

Returns:



78
79
80
81
82
# File 'lib/concurrent/atomic/copy_on_write_observer_set.rb', line 78

def notify_and_delete_observers(*args, &block)
  old = clear_observers_and_return_old
  notify_to(old, *args, &block)
  self
end

#notify_observers(*args, &block) ⇒ CopyOnWriteObserverSet

Notifies all registered observers with optional args

Parameters:

  • args (Object)

    arguments to be passed to each observer

Returns:



69
70
71
72
# File 'lib/concurrent/atomic/copy_on_write_observer_set.rb', line 69

def notify_observers(*args, &block)
  notify_to(observers, *args, &block)
  self
end