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



62
63
64
# File 'lib/wisper/global_listeners.rb', line 62

def self.clear
  instance.clear
end

.listenersObject



58
59
60
# File 'lib/wisper/global_listeners.rb', line 58

def self.listeners
  instance.listeners
end

.registrationsObject



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

def self.registrations
  instance.registrations
end

.subscribe(*listeners, **options) ⇒ Object



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

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

.unsubscribe(*listeners) ⇒ Object



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

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

Instance Method Details

#clearObject



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

def clear
  with_mutex { @registrations.clear }
end

#listenersObject



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

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

#registrationsObject



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

def registrations
  with_mutex { @registrations }
end

#subscribe(*listeners, **options) ⇒ Object



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

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

#unsubscribe(*listeners) ⇒ Object



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

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