Sha256: 1ede241ba677abb84fa3465d52f7263c609bfcdac64b44779422350eb09dca94
Contents?: true
Size: 1.16 KB
Versions: 2
Compression:
Stored size: 1.16 KB
Contents
require 'singleton' # Handles global subscriptions module Wisper class GlobalListeners include Singleton def initialize @registrations = Set.new @mutex = Mutex.new end 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 def registrations with_mutex { @registrations } end def listeners registrations.map(&:listener).freeze end def clear with_mutex { @registrations.clear } end def self.subscribe(*listeners) instance.subscribe(*listeners) end def self.registrations instance.registrations end def self.listeners instance.listeners end def self.clear instance.clear end def self.add_listener(listener, options = {}) # deprecated warn "[DEPRECATION] use `subscribe` instead of `add_listener`" subscribe(listener, options) end private def with_mutex @mutex.synchronize { yield } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
wisper-1.6.1 | lib/wisper/global_listeners.rb |
wisper-1.6.0 | lib/wisper/global_listeners.rb |