Sha256: 3f4fe66596caf879ac20b9aef7497b4965b9ca417a6c2650491ee658cefdbb24

Contents?: true

Size: 1.3 KB

Versions: 16

Compression:

Stored size: 1.3 KB

Contents

module Lookbook
  class Websocket
    attr_reader :engine_mount_path, :server, :logger

    def initialize(engine_mount_path, logger: Lookbook.logger)
      @engine_mount_path = engine_mount_path
      @logger = logger

      logger.info "Initializing websocket"

      if Gem::Version.new(Rails.version) >= Gem::Version.new(6.0)
        @server = ActionCable::Server::Base.new config: cable_config
      else
        @server = ActionCable::Server::Base.new
        @server.config = cable_config
      end
    end

    def broadcast(message, payload = nil)
      logger.debug message.to_s
      server.broadcast(message.to_s, payload.to_h)
    end

    def mount_path
      "/cable"
    end

    def full_mount_path
      "#{engine_mount_path}/#{mount_path}".gsub("//", "/")
    end

    def mountable?
      true
    end

    alias_method :mounted?, :mountable?

    def self.noop
      NullWebsocket.new
    end

    protected

    def cable_config
      cable = ActionCable::Server::Configuration.new
      cable.cable = {adapter: "async"}.with_indifferent_access
      cable.mount_path = nil
      cable.connection_class = -> { Lookbook::Connection }
      cable.logger = logger
      cable
    end
  end

  class NullWebsocket < NullObject
    def mountable?
      false
    end
    alias_method :mounted?, :mountable?
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
lookbook-1.5.5 lib/lookbook/websocket.rb
lookbook-1.5.4 lib/lookbook/websocket.rb
lookbook-1.5.3 lib/lookbook/websocket.rb
lookbook-1.5.2 lib/lookbook/websocket.rb
lookbook-1.5.1 lib/lookbook/websocket.rb
lookbook-1.5.0 lib/lookbook/websocket.rb
lookbook-1.4.5 lib/lookbook/websocket.rb
lookbook-1.4.4 lib/lookbook/websocket.rb
lookbook-1.4.3 lib/lookbook/websocket.rb
lookbook-1.4.2 lib/lookbook/websocket.rb
lookbook-1.4.1 lib/lookbook/websocket.rb
lookbook-1.4.0 lib/lookbook/websocket.rb
lookbook-1.3.4 lib/lookbook/websocket.rb
lookbook-1.3.3 lib/lookbook/websocket.rb
lookbook-1.3.1 lib/lookbook/websocket.rb
lookbook-1.3.0 lib/lookbook/websocket.rb