Sha256: b3ccd338dcc8e1426696c8fd3b62338d520181f46e44a488d79a8002af758969

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 KB

Contents

module Faye
  module Engine
    
    class Connection
      include EventMachine::Deferrable
      include Timeouts
      
      attr_reader :id
      
      def initialize(engine, id, options = {})
        @engine  = engine
        @id      = id
        @options = options
        @inbox   = Set.new
      end
      
      def deliver(message)
        return unless @inbox.add?(message)
        begin_delivery_timeout
      end
      
      def connect(options, &block)
        options = options || {}
        timeout = options['timeout'] ? options['timeout'] / 1000.0 : @engine.timeout
        
        set_deferred_status(:deferred)
        
        callback(&block)
        return if @connected
        
        @connected = true
        
        begin_delivery_timeout
        begin_connection_timeout(timeout)
      end
      
      def flush!
        release_connection!
        set_deferred_status(:succeeded, @inbox.entries)
      end
      
    private
      
      def release_connection!
        @engine.close_connection(@id)
        remove_timeout(:connection)
        remove_timeout(:delivery)
        @connected = false
      end
      
      def begin_delivery_timeout
        return if @inbox.empty?
        add_timeout(:delivery, MAX_DELAY) { flush! }
      end
      
      def begin_connection_timeout(timeout)
        add_timeout(:connection, timeout) { flush! }
      end
    end
    
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
faye-0.7.2 lib/faye/engines/connection.rb
faye-0.6.8 lib/faye/engines/connection.rb
faye-0.7.1 lib/faye/engines/connection.rb
faye-0.7.0 lib/faye/engines/connection.rb
faye-0.6.7 lib/faye/engines/connection.rb
faye-0.6.6 lib/faye/engines/connection.rb
faye-0.6.5 lib/faye/engines/connection.rb
faye-0.6.4 lib/faye/engines/connection.rb
faye-0.6.3 lib/faye/engines/connection.rb
faye-0.6.2 lib/faye/engines/connection.rb
faye-0.6.1 lib/faye/engines/connection.rb
faye-0.6.0 lib/faye/engines/connection.rb