Class: Wisper::GlobalListeners

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wisper/global_listeners.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlobalListeners

Returns a new instance of GlobalListeners.


11
12
13
14
# File 'lib/wisper/global_listeners.rb', line 11

def initialize
  @registrations = Set.new
  @mutex         = Mutex.new
end

Class Method Details

.clearObject


64
65
66
# File 'lib/wisper/global_listeners.rb', line 64

def self.clear
  instance.clear
end

.listenersObject


60
61
62
# File 'lib/wisper/global_listeners.rb', line 60

def self.listeners
  instance.listeners
end

.registrationsObject


56
57
58
# File 'lib/wisper/global_listeners.rb', line 56

def self.registrations
  instance.registrations
end

.subscribe(*listeners) ⇒ Object


48
49
50
# File 'lib/wisper/global_listeners.rb', line 48

def self.subscribe(*listeners)
  instance.subscribe(*listeners)
end

.unsubscribe(*listeners) ⇒ Object


52
53
54
# File 'lib/wisper/global_listeners.rb', line 52

def self.unsubscribe(*listeners)
  instance.unsubscribe(*listeners)
end

Instance Method Details

#clearObject


44
45
46
# File 'lib/wisper/global_listeners.rb', line 44

def clear
  with_mutex { @registrations.clear }
end

#listenersObject


40
41
42
# File 'lib/wisper/global_listeners.rb', line 40

def listeners
  registrations.map(&:listener).freeze
end

#registrationsObject


36
37
38
# File 'lib/wisper/global_listeners.rb', line 36

def registrations
  with_mutex { @registrations }
end

#subscribe(*listeners) ⇒ Object


16
17
18
19
20
21
22
23
24
25
# File 'lib/wisper/global_listeners.rb', line 16

def subscribe(*listeners)
  options = listeners.last.is_a?(Hash) ? listeners.pop : {}

  with_mutex do
    listeners.each do |listener|
      @registrations << ObjectRegistration.new(listener, options)
    end
  end
  self
end

#unsubscribe(*listeners) ⇒ Object


27
28
29
30
31
32
33
34
# File 'lib/wisper/global_listeners.rb', line 27

def unsubscribe(*listeners)
  with_mutex do
    @registrations.delete_if do |registration|
      listeners.include?(registration.listener)
    end
  end
  self
end