Sha256: 176992e587b8227f3309bf343ea5f4b40a1e3f009a6eb35f7157431ffff6ea55

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module StimulusReflex
  class Broadcaster
    include CableReady::Broadcaster

    attr_reader :reflex, :logger, :operations
    delegate :permanent_attribute_name, :stream_name, to: :reflex

    def initialize(reflex)
      @reflex = reflex
      @logger = Rails.logger if defined?(Rails.logger)
      @operations = []
    end

    def nothing?
      false
    end

    def page?
      false
    end

    def selector?
      false
    end

    def broadcast_message(subject:, body: nil, data: {}, error: nil)
      logger.error "\e[31m#{body}\e[0m" if subject == "error"
      operations << ["document", :dispatch_event]
      cable_ready[stream_name].dispatch_event(
        name: "stimulus-reflex:server-message",
        detail: {
          reflexId: data["reflexId"],
          stimulus_reflex: data.merge(
            morph: to_sym,
            server_message: {subject: subject, body: error&.to_s}
          )
        }
      )
      cable_ready.broadcast
    end

    # abstract method to be implemented by subclasses
    def broadcast(*args)
      raise NotImplementedError
    end

    # abstract method to be implemented by subclasses
    def to_sym
      raise NotImplementedError
    end

    # abstract method to be implemented by subclasses
    def to_s
      raise NotImplementedError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stimulus_reflex-3.4.0.pre5 lib/stimulus_reflex/broadcasters/broadcaster.rb