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



36
37
38
39
40
# File 'lib/shouter/store.rb', line 36

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



48
49
50
# File 'lib/shouter/store.rb', line 48

def listeners
  @@listeners
end

.mutexObject



52
53
54
# File 'lib/shouter/store.rb', line 52

def mutex
  @@mutex
end

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



42
43
44
45
46
# File 'lib/shouter/store.rb', line 42

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
27
28
# File 'lib/shouter/store.rb', line 19

def register(objects, options)
  mutex.synchronize do
    objects.each do |object|
      subscribed_objects = @@listeners.map(&:object)
      # Got through a builder class based on whether it is a sync or async listener
      # Should Return Shouter::Listener
      @@listeners << Shouter::Builder.register(object, options) unless subscribed_objects.include?(object)
    end
  end
end

.unregister(objects) ⇒ Object



30
31
32
33
34
# File 'lib/shouter/store.rb', line 30

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