Sha256: 1d7712c4614f74bb46638b528546d55f566b052993e0d6ae1b9022f10b8daa02

Contents?: true

Size: 1.29 KB

Versions: 26

Compression:

Stored size: 1.29 KB

Contents

require 'ably/modules/event_emitter.rb'

module Ably::Util
  # PubSub class provides methods to publish & subscribe to events, with methods and naming
  # intentionally different to EventEmitter as it is intended for private message handling
  # within the client library.
  #
  # @example
  #   class Channel
  #     def messages
  #       @messages ||= PubSub.new
  #     end
  #   end
  #
  #   channel = Channel.new
  #   channel.messages.subscribe(:event) { |name| puts "Event message #{name} received" }
  #   channel.messages.publish :event, "Test"
  #   #=> "Event message Test received"
  #   channel.messages.remove :event
  #
  class PubSub
    include Ably::Modules::EventEmitter

    # Ensure new PubSub object does not share class instance variables
    def self.new(options = {})
      Class.new(PubSub).allocate.tap do |pub_sub_object|
        pub_sub_object.send(:initialize, options)
      end
    end

    def inspect
      "<#PubSub: @event_emitter_coerce_proc: #{self.class.event_emitter_coerce_proc.inspect}\n @callbacks: #{callbacks}>"
    end

    def initialize(options = {})
      self.class.instance_eval do
        configure_event_emitter options

        alias_method :subscribe, :unsafe_on
        alias_method :publish, :emit
        alias_method :unsubscribe, :off
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
ably-rest-0.8.15 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-0.8.15 lib/ably/util/pub_sub.rb
ably-rest-0.9.3 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-rest-0.9.2 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-rest-0.9.1 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-rest-0.9.0 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-rest-0.8.14 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-0.8.14 lib/ably/util/pub_sub.rb
ably-rest-0.8.13 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-0.8.13 lib/ably/util/pub_sub.rb
ably-0.8.12 lib/ably/util/pub_sub.rb
ably-0.8.11 lib/ably/util/pub_sub.rb
ably-0.8.10 lib/ably/util/pub_sub.rb
ably-rest-0.8.9 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-0.8.9 lib/ably/util/pub_sub.rb
ably-0.8.8 lib/ably/util/pub_sub.rb
ably-0.8.7 lib/ably/util/pub_sub.rb
ably-0.8.6 lib/ably/util/pub_sub.rb
ably-rest-0.8.6 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb
ably-rest-0.8.5 lib/submodules/ably-ruby/lib/ably/util/pub_sub.rb