Sha256: fe258629459cf4b94e33ac3f510e37a418057cc20ba9d22516f7cc4e931678d0
Contents?: true
Size: 1.25 KB
Versions: 2
Compression:
Stored size: 1.25 KB
Contents
# Handles global subscriptions # @api private require 'singleton' module Wisper class GlobalListeners include Singleton def initialize @registrations = Set.new @mutex = Mutex.new end def subscribe(*listeners, **options) with_mutex do listeners.each do |listener| @registrations << ObjectRegistration.new(listener, **options) end end self end def unsubscribe(*listeners) with_mutex do @registrations.delete_if do |registration| listeners.include?(registration.listener) end end self end def registrations with_mutex { @registrations } end def listeners registrations.map(&:listener).freeze end def clear with_mutex { @registrations.clear } end def self.subscribe(*listeners, **options) instance.subscribe(*listeners, **options) end def self.unsubscribe(*listeners) instance.unsubscribe(*listeners) end def self.registrations instance.registrations end def self.listeners instance.listeners end def self.clear instance.clear end private def with_mutex @mutex.synchronize { yield } end end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
wisper-3.0.0.rc1 | lib/wisper/global_listeners.rb |
wisper-compat-4.0.0 | lib/wisper/global_listeners.rb |