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.



9
10
11
12
# File 'lib/wisper/global_listeners.rb', line 9

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

Class Method Details

.add_listener(listener, options = {}) ⇒ Object

deprecated



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

def self.add_listener(listener, options = {}) # deprecated
  warn "[DEPRECATION] use `subscribe` instead of `add_listener`"
  subscribe(listener, options)
end

.clearObject



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

def self.clear
  instance.clear
end

.listenersObject



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

def self.listeners
  instance.listeners
end

.registrationsObject



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

def self.registrations
  instance.registrations
end

.subscribe(*listeners) ⇒ Object



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

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

Instance Method Details

#clearObject



33
34
35
# File 'lib/wisper/global_listeners.rb', line 33

def clear
  with_mutex { @registrations.clear }
end

#listenersObject



29
30
31
# File 'lib/wisper/global_listeners.rb', line 29

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

#registrationsObject



25
26
27
# File 'lib/wisper/global_listeners.rb', line 25

def registrations
  with_mutex { @registrations }
end

#subscribe(*listeners) ⇒ Object



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

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