Sha256: dba91149d2a7f50410d90558dbe9eb8bb9a17d37121d60d1b05ad8ee9f1b78e5

Contents?: true

Size: 1.43 KB

Versions: 9

Compression:

Stored size: 1.43 KB

Contents

module Faye
  module Engine
    
    class Connection
      include EventMachine::Deferrable
      include Timeouts
      
      attr_accessor :socket
      
      def initialize(engine, id, options = {})
        @engine  = engine
        @id      = id
        @options = options
        @inbox   = Set.new
      end
      
      def deliver(message)
        return socket.send(Faye.to_json([message])) if socket
        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)
        
        begin_delivery_timeout
        begin_connection_timeout(timeout)
      end
      
      def flush!(force = false)
        release_connection!(force)
        set_deferred_status(:succeeded, @inbox.entries)
        @inbox = []
      end
      
    private
      
      def release_connection!(force = false)
        @engine.close_connection(@id) if force or socket.nil?
        remove_timeout(:connection)
        remove_timeout(:delivery)
      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

9 entries across 9 versions & 3 rubygems

Version Path
faye-0.8.5 lib/faye/engines/connection.rb
faye-0.8.4 lib/faye/engines/connection.rb
faye-0.8.3 lib/faye/engines/connection.rb
faye-0.8.2 lib/faye/engines/connection.rb
_bushido-faye-0.8.2 lib/faye/engines/connection.rb
_bushido-faye-0.8.1 lib/faye/engines/connection.rb
bushido-faye-0.8.1 lib/faye/engines/connection.rb
faye-0.8.1 lib/faye/engines/connection.rb
faye-0.8.0 lib/faye/engines/connection.rb