Sha256: cd5f5a177dd2979980767fd57da9841da2498639a4587326cb1c66423311fcf7

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 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 = 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

    private

    def with_mutex
      @mutex.synchronize { yield }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-2.0.0.rc1 lib/wisper/global_listeners.rb