Sha256: 455e50453d9abff7972765174f1de4dc7851e9ea8a22564c25309a8c2bf0d1bb

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 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(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

3 entries across 3 versions & 2 rubygems

Version Path
faye-0.8.11 lib/faye/engines/connection.rb
face-faye-0.8.9 lib/faye/engines/connection.rb
faye-0.8.9 lib/faye/engines/connection.rb