Sha256: 702b3bce5bc134cf214c826072f3ca99b700c5431f32012fdbd5edde14212711

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module StimulusReflex
  class Broadcaster
    attr_reader :reflex, :logger, :operations
    delegate :cable_ready, :permanent_attribute_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.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

7 entries across 7 versions & 1 rubygems

Version Path
stimulus_reflex-3.4.2 lib/stimulus_reflex/broadcasters/broadcaster.rb
stimulus_reflex-3.4.1 lib/stimulus_reflex/broadcasters/broadcaster.rb
stimulus_reflex-3.4.0 lib/stimulus_reflex/broadcasters/broadcaster.rb
stimulus_reflex-3.4.0.pre9 lib/stimulus_reflex/broadcasters/broadcaster.rb
stimulus_reflex-3.4.0.pre8 lib/stimulus_reflex/broadcasters/broadcaster.rb
stimulus_reflex-3.4.0.pre7 lib/stimulus_reflex/broadcasters/broadcaster.rb
stimulus_reflex-3.4.0.pre6 lib/stimulus_reflex/broadcasters/broadcaster.rb