Class: Wisper::TemporaryListeners

Inherits:
Object
  • Object
show all
Defined in:
lib/wisper/temporary_listeners.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registrationsObject



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

def self.registrations
  new.registrations
end

.subscribe(*listeners, &block) ⇒ Object



6
7
8
# File 'lib/wisper/temporary_listeners.rb', line 6

def self.subscribe(*listeners, &block)
  new.subscribe(*listeners, &block)
end

Instance Method Details

#registrationsObject



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

def registrations
  Thread.current[key] ||= Set.new
end

#subscribe(*listeners, &block) ⇒ Object



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

def subscribe(*listeners, &block)
  options = listeners.last.is_a?(Hash) ? listeners.pop : {}
  begin
    listeners.each { |listener| registrations << ObjectRegistration.new(listener, options) }
    yield
  ensure
    clear
  end
  self
end