Sha256: b20bfe287a45144d14d16ae7b20d665d52c028c3edee0e6ff55705f5453462c3

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Firehose
  # TODO add support to broker for publishing, then abstract this out into a backend. A
  # broker will eventually be passed into a web server front-end to serve up the web requests.
  class Broker
    def consumers
      @consumers ||= Hash.new do |consumers, consumer_id|
        consumer = Firehose::Consumer.new(consumer_id)
        consumer.on_unsubscribe do
          consumers.delete consumer_id
        end
        consumers[consumer_id] = consumer
      end
    end

    # Don't like the [] syntax to get at consumers? No worries mate!
    def consumer(consumer_id)
      consumers[consumer_id]
    end

    # Gracefully unsubscribe all of the consumers and get rid of them from the consumers 
    # collection.
    def stop
      consumers.values.each(&:unsubscribe)
    end

    # Returns a hash of the connected consumers with the number of their subscriptions
    def stats
      consumers.inject({}) do |memo, (consumer_id, consumer)|
        memo[consumer_id] = { 'subscriptions' => consumer.subscriptions.keys }
        memo
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
firehose-0.0.16 lib/firehose/broker.rb