Sha256: 6c1378a230d0a4f9d3745d22970b64427534845af2adfec321a69fe39653ba0a

Contents?: true

Size: 1.65 KB

Versions: 116

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require_dependency "renalware"

module Renalware
  module Broadcasting
    include Wisper::Publisher
    extend ActiveSupport::Concern

    class Subscriber
      attr_reader :klass_name, :async
      alias_method :async?, :async

      def initialize(klass_name, async: false)
        @klass_name = klass_name
        @async = async
      end

      # Note that when using the wisper-activejob gem (so we can use delayed_job for instance)
      # we subscribe the class not an instance, and the subscriber must have class
      # methods matching the event names it wants to handle.
      # https://github.com/krisleech/wisper-activejob
      def instance
        async? ? klass : klass.new
      end

      def klass
        klass_name.to_s.constantize
      end
    end

    # Subscribes any listeners configured in Renalware.config.broadcast_subscription_map
    # to the current instance.
    #
    # Example usage
    #
    #   class SomeServiceObject
    #     include Broadcasting
    #
    #     def call
    #       ..
    #     end
    #   end
    #
    #   SomeServiceObject.new(..).broadcasting_to_configured_subscribers.call(..)
    #
    # See https://github.com/krisleech/wisper
    #
    def broadcasting_to_configured_subscribers
      subscribers = Array(Renalware.config.broadcast_subscription_map[self.class.name])
      subscribers.each do |subscriber|
        # Support String subscribers eg a simple class name as well as Subscriber instances.
        subscriber = Subscriber.new(subscriber) unless subscriber.respond_to?(:klass)
        subscribe(subscriber.instance, async: subscriber.async?)
      end
      self
    end
  end
end

Version data entries

116 entries across 116 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.1.0 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.167 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.166 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.165 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.164 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.163 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.162 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.161 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.160 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.159 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.158 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.157 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.156 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.155 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.153 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.152 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.151 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.149 app/models/concerns/renalware/broadcasting.rb
renalware-core-2.0.148 app/models/concerns/renalware/broadcasting.rb