Sha256: 5b6b279a429a061fedd8142f74f02d2c639ddc1950dd7685e5d4c9e9751ac6d7

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Loco
  class Sender
    class << self
      def call(recipient_s, payload = {})
        payload = with_idempotency_key(payload)
        recipients = recipient_s.is_a?(Array) ? recipient_s : [recipient_s]
        new.(recipients, payload)
      end

      private

      def with_idempotency_key(payload)
        hash = payload.clone
        hash[:loco] ||= {}
        hash[:loco][:idempotency_key] ||= hash[:idempotency_key] || SecureRandom.hex
        hash.delete(:idempotency_key)
        hash
      end
    end

    def initialize
      @uuids = []
    end

    def call(recipients, payload)
      recipients.each do |recipient|
        case recipient
        when String then broadcast_to(recipient, payload)
        when Hash then process_hash(recipient, payload)
        else find_and_broadcast_to(recipient, payload)
        end
      end
      payload[:loco][:idempotency_key]
    end

    private

    def process_hash(recipient, payload)
      if recipient.key?('token')
        find_and_broadcast_to(recipient['token'], payload)
      elsif recipient.key?('class')
        find_and_broadcast_to(recipient['class'].constantize, payload)
      end
    end

    def find_and_broadcast_to(recipient, payload)
      WsConnectionFinder.(recipient) do |uuid|
        broadcast_to(uuid, payload)
      end
    end

    def broadcast_to(uuid, payload)
      return if @uuids.include?(uuid)

      @uuids << uuid
      NotificationCenterChannel.broadcast_to(uuid, payload)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
loco-rails-7.0.0.pre lib/loco/sender.rb
loco-rails-6.2.0 lib/loco/sender.rb
loco-rails-6.1.0 lib/loco/sender.rb
loco-rails-6.0.0 lib/loco/sender.rb
loco-rails-5.0.0 lib/loco/sender.rb