Class: Wisper::GlobalListeners
- Inherits:
-
Object
- Object
- Wisper::GlobalListeners
- Includes:
- Singleton
- Defined in:
- lib/wisper/global_listeners.rb
Class Method Summary collapse
- .clear ⇒ Object
- .listeners ⇒ Object
- .registrations ⇒ Object
- .subscribe(*listeners, **options) ⇒ Object
- .unsubscribe(*listeners) ⇒ Object
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize ⇒ GlobalListeners
constructor
A new instance of GlobalListeners.
- #listeners ⇒ Object
- #registrations ⇒ Object
- #subscribe(*listeners, **options) ⇒ Object
- #unsubscribe(*listeners) ⇒ Object
Constructor Details
#initialize ⇒ GlobalListeners
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
.clear ⇒ Object
62 63 64 |
# File 'lib/wisper/global_listeners.rb', line 62 def self.clear instance.clear end |
.listeners ⇒ Object
58 59 60 |
# File 'lib/wisper/global_listeners.rb', line 58 def self.listeners instance.listeners end |
.registrations ⇒ Object
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, **) instance.subscribe(*listeners, **) 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
#clear ⇒ Object
42 43 44 |
# File 'lib/wisper/global_listeners.rb', line 42 def clear with_mutex { @registrations.clear } end |
#listeners ⇒ Object
38 39 40 |
# File 'lib/wisper/global_listeners.rb', line 38 def listeners registrations.map(&:listener).freeze end |
#registrations ⇒ Object
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, **) with_mutex do listeners.each do |listener| @registrations << ObjectRegistration.new(listener, **) 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 |