Sha256: 2305a9a2cecd30b08ae59fc29f9b8e77a04a76f20735bd14979f318468526fb2

Contents?: true

Size: 1.22 KB

Versions: 129

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module ActionCable
  module Connection
    # Allows us to buffer messages received from the WebSocket before the Connection has been fully initialized, and is ready to receive them.
    class MessageBuffer # :nodoc:
      def initialize(connection)
        @connection = connection
        @buffered_messages = []
      end

      def append(message)
        if valid? message
          if processing?
            receive message
          else
            buffer message
          end
        else
          connection.logger.error "Couldn't handle non-string message: #{message.class}"
        end
      end

      def processing?
        @processing
      end

      def process!
        @processing = true
        receive_buffered_messages
      end

      private
        attr_reader :connection
        attr_reader :buffered_messages

        def valid?(message)
          message.is_a?(String)
        end

        def receive(message)
          connection.receive message
        end

        def buffer(message)
          buffered_messages << message
        end

        def receive_buffered_messages
          receive buffered_messages.shift until buffered_messages.empty?
        end
    end
  end
end

Version data entries

129 entries across 124 versions & 10 rubygems

Version Path
actioncable-7.1.5 lib/action_cable/connection/message_buffer.rb
actioncable-7.1.4.2 lib/action_cable/connection/message_buffer.rb
actioncable-7.0.8.6 lib/action_cable/connection/message_buffer.rb
actioncable-6.1.7.10 lib/action_cable/connection/message_buffer.rb
actioncable-6.1.7.9 lib/action_cable/connection/message_buffer.rb
actioncable-7.1.4.1 lib/action_cable/connection/message_buffer.rb
actioncable-7.0.8.5 lib/action_cable/connection/message_buffer.rb
actioncable-7.1.4 lib/action_cable/connection/message_buffer.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actioncable-7.1.3.4/lib/action_cable/connection/message_buffer.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actioncable-7.0.8.4/lib/action_cable/connection/message_buffer.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/connection/message_buffer.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/connection/message_buffer.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/connection/message_buffer.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actioncable-7.1.3.4/lib/action_cable/connection/message_buffer.rb
actioncable-7.1.3.4 lib/action_cable/connection/message_buffer.rb
actioncable-7.0.8.4 lib/action_cable/connection/message_buffer.rb
actioncable-6.1.7.8 lib/action_cable/connection/message_buffer.rb
actioncable-7.1.3.2 lib/action_cable/connection/message_buffer.rb
actioncable-7.1.3.1 lib/action_cable/connection/message_buffer.rb
actioncable-7.0.8.1 lib/action_cable/connection/message_buffer.rb