Class: Shouter::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/shouter/store.rb

Constant Summary collapse

@@listeners =
[]
@@mutex =
Mutex.new

Class Method Summary collapse

Class Method Details

.clearObject



34
35
36
37
38
# File 'lib/shouter/store.rb', line 34

def clear
  mutex.synchronize do
    @@listeners = []
  end
end

.inherited(subclass) ⇒ Object



15
16
17
# File 'lib/shouter/store.rb', line 15

def inherited(subclass)
  raise NoInheritanceAllowedError.new("#{self.class.to_s} is meant to be a singleton class and not to be inherited")
end

.listenersObject



46
47
48
# File 'lib/shouter/store.rb', line 46

def listeners
  @@listeners
end

.mutexObject



50
51
52
# File 'lib/shouter/store.rb', line 50

def mutex
  @@mutex
end

.notify(scope, event, args, &block) ⇒ Object



40
41
42
43
44
# File 'lib/shouter/store.rb', line 40

def notify(scope, event, args, &block)
  return if listeners.empty?

  listeners.each { |listener| listener.notify(scope, event, args, &block) }
end

.register(objects, options) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/shouter/store.rb', line 19

def register(objects, options)
  mutex.synchronize do
    objects.each do |object|
      subscribed_objects = @@listeners.map(&:object)
      @@listeners << Shouter::Listener.new(object, options) unless subscribed_objects.include?(object)
    end
  end
end

.unregister(objects) ⇒ Object



28
29
30
31
32
# File 'lib/shouter/store.rb', line 28

def unregister(objects)
  mutex.synchronize do
    [*objects].each { |object| listeners.delete_if { |listener| listener.object == object } }
  end
end