Sha256: dc9cf04444e14331abc53094f6b603263ac76b6cb4b502f8a145db5953917970

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module CableReady
  # This class is a thread local singleton: CableReady::Channels.instance
  # SEE: https://github.com/socketry/thread-local/tree/master/guides/getting-started
  class Channels
    include Compoundable
    extend Thread::Local

    def initialize
      @channels = {}
    end

    def [](*keys)
      keys.select!(&:itself)
      identifier = keys.many? || (keys.one? && keys.first.is_a?(ActiveRecord::Base)) ? compound(keys) : keys.pop
      @channels[identifier] ||= CableReady::Channel.new(identifier)
    end

    def broadcast(*identifiers, clear: true)
      @channels.values
        .reject { |channel| identifiers.any? && identifiers.exclude?(channel.identifier) }
        .select { |channel| channel.identifier.is_a?(String) }
        .tap do |channels|
          channels.each { |channel| @channels[channel.identifier].broadcast(clear: clear) }
          channels.each { |channel| @channels.except!(channel.identifier) if clear }
        end
    end

    def broadcast_to(model, *identifiers, clear: true)
      @channels.values
        .reject { |channel| identifiers.any? && identifiers.exclude?(channel.identifier) }
        .reject { |channel| channel.identifier.is_a?(String) }
        .tap do |channels|
          channels.each { |channel| @channels[channel.identifier].broadcast_to(model, clear: clear) }
          channels.each { |channel| @channels.except!(channel.identifier) if clear }
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cable_ready-5.0.0.pre3 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre2 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre1 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre0 lib/cable_ready/channels.rb